使用来自ajax调用的js var更新php var

时间:2014-02-01 06:02:42

标签: javascript php jquery ajax

我正在尝试使用增加的javascript $questNumber和jQuery questNum

更新php $.get()

Console.log告诉我js questNum var正在递增。

但即使我在JS方面增加了值,echo "testing..." . $questNumber;输出1。这意味着更新后的值不会发送到$questNumber来更新数据库,并将相应的新值集返回给javascript端。

JavaScript:

/*Check player sentence input to see if grammar is correct*/
function submitMe() {
    var input = document.getElementById('textBox').value;
    log(questNum);
    if ($.trim(input) == getSentence(questNum)) {
        $("#responseVerify").html("Great job");
        $("#textBox").val("").trigger("change");
        //post successful quest to Quests.php, which will increment questcount there, and get new vocab words
        questNum++;
        log(questNum);
        $.get("php/Quests.php", { "_questNum" : questNum},
            function() {
                $(".wordBank_Words").empty();
                $.each(wordsArray, function(key, value) {           
                    $(".wordBank_Words").append("<div class='bank-word' word='" + key + "' ><b>" + key + "</b>: " + value + "</div>");
                });
            });
        }
    else {
        $("#responseVerify").html("Keep going...");
    }
}

PHP:

<?php
    //if user's input is correct, increment task number, get next vocabulary
    include 'DbConnect.php';
    $questNumber = (isset($_GET['_questNum']) ? ($_GET['_questNum']) : 1);  
    echo "testing..." . $questNumber;

    $qry = 
     "SELECT t.*, v.* 
     FROM task t 
     INNER JOIN vocabtask vt ON (t.id = vt.taskid)
     INNER JOIN vocab v ON (v.id = vt.vocabid)
     WHERE vt.taskid = " . $questNumber;

    $sql = $mysqli->query($qry);

    $wordsArray = array();
    while ($row = $sql->fetch_assoc()) {
        echo $row['chinese'];
        $wordsArray[$row['chinese']] = $row['english'];
    }
    echo "testing..." . $questNumber;
    mysqli_close($mysqli);  
    echo "<script type='text/javascript'> var wordsArray = " . json_encode($wordsArray) . "; </script>";
?>

HTML:

        <!--ROW 3: RESPONSE-->
        <div class="row">
            <div class="span12">
                <!--Select a word shown and it gets added to the input box-->
                Create sentence:

                <input type="text" id="textBox"  value="" />
                <br/>
                <button onclick="submitMe()" id="testButton" >Submit Response </button>
                <br/>

                <i><span id="responseVerify"></span></i><br />
                <div class="wordBank_Headings">Word Bank:
                    <span class="wordBank_Words"></span>
                </div>
                <div class="wordBank_Headings">Hint:
                    <span class="wordBank_Hint"></span>
                </div>
                <div class="wordBank_Headings">New Words:
                    <span class="new"></span>
                </div>
            </div>
        </div>

1 个答案:

答案 0 :(得分:0)

试试这个ajax格式

$.ajax({                                  
        type: "GET",   //method
        url:"php/Quests.php", //your ajax page        
        data:"_questNum="+questNum,  //pass values to this data
        success: function(data){    //success function  
        alert('sasas');        
        }