PHP + jQuery的 - 通过AJAX到PHP通价值和对证变量 [英] PHP + Jquery - pass value through ajax to php and check against variable

查看:138
本文介绍了PHP + jQuery的 - 通过AJAX到PHP通价值和对证变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能让下面的PHP + jQuery的工作 - 所有我想要的脚本做的是通过AJAX传递价值,并获得PHP来抓住它,检查它匹配,并添加1〜得分。

这是在code我已经写了:

 < PHP
$得分=1;

$ userAnswer = $ _ POST ['名称'];

如果($ _ POST ['名称'] ==145){
    $得分++;
}其他{
    //没做什么
}

回声$得分;

?>


<脚本类型=文/ JavaScript的>

$(文件)。就绪(函数(){

    $(#raaagh)。点击(函数(){

    VAR值=145;

    警报(值);

    $阿贾克斯({
        网址:processing.php',//这是当前文档
        键入:POST,
        数据:({名称:值}),
        成功:函数(){
            location.reload();
       }
    });


    });
});

< / SCRIPT>

&其中,P的id =raaagh> Ajax的客场&所述; / P>
 

感谢您的帮助,我已经改变了GET来POST在这两种情况下,并没有喜悦 - 还有别的错误

解决方案

首先:不要再回到黑暗时代...不使用相同的脚本生成HTML和一个Ajax请求做出响应。

我不能让你正在尝试做的任何意义......让我改变你的code所以它至少有一定的道理,并记录发生了什么事情。这似乎是问题是,你从你的成功处理程序调用location.reload的事实。

// ajax.php - 输出2,如果name参数为145,否则为1(????)

 < PHP
$得分=1;
$ userAnswer = $ _ POST ['名称'];
如果($ _ POST ['名称'] ==145){
    $得分++;
}
回声$得分;
?>
 

// test.html的

 <脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
    $(#raaagh)。点击(函数(){
        $阿贾克斯({
            网址:'ajax.php',//这是当前文档
            键入:POST,
            数据:({名:145}),
            成功:功能(数据){
                //你为什么刷新页面?这可能是你的bug
                // location.reload();

                //更换点击的段落内容
                //从Ajax调用的结果
                $(#raaagh)的HTML(数据);
            }
        });
    });
});

< / SCRIPT>

&其中,P的id =raaagh> Ajax的客场&所述; / P>
 

I can't get the following PHP + jQuery to work - all I want the script to do is pass the value through ajax, and get the php to grab it, check it matches and add 1 to score.

This is the code I've written:

<?php
$score = "1";

$userAnswer = $_POST['name'];

if ($_POST['name'] == "145"){
    $score++;
}else{
    //Do nothing
}

echo $score;

?>


<script type="text/javascript">

$(document).ready(function() {

    $("#raaagh").click(function(){

    var value = "145";

    alert(value);

    $.ajax({
        url: 'processing.php', //This is the current doc
        type: "POST",
        data: ({name: value}),
        success: function(){
            location.reload();
       }
    });


    });
});

</script>

<p id="raaagh">Ajax Away</p>

Thanks for the help, I've changed GET to POST in both instances, and no joy - there's something else wrong.

解决方案

First of all: Do not go back to the dark ages... don't use the same script to generate HTML and to respond to an ajax request.

I can't make any sense of what you are trying to do... Let me change your code so it at least makes some sense and document what's going on. It seems like the problem is the fact that you are calling location.reload from your success handler.

// ajax.php - Outputs 2 if the name parameter is 145, 1 otherwise (????)

<?php
$score = "1";    
$userAnswer = $_POST['name'];    
if ($_POST['name'] == "145"){
    $score++;
}       
echo $score;    
?>

// test.html

<script type="text/javascript">  
$(document).ready(function() {    
    $("#raaagh").click(function(){    
        $.ajax({
            url: 'ajax.php', //This is the current doc
            type: "POST",
            data: ({name: 145}),
            success: function(data){
                // Why were you reloading the page? This is probably your bug
                // location.reload();

                // Replace the content of the clicked paragraph
                // with the result from the ajax call
                $("#raaagh").html(data);
            }
        });        
    });
});

</script>

<p id="raaagh">Ajax Away</p>

这篇关于PHP + jQuery的 - 通过AJAX到PHP通价值和对证变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆