简体   繁体   English

从ajax调用使用js var更新php var

[英]update php var with js var from ajax call

I'm trying to update php $questNumber with the incremented javascript questNum with jQuery $.get() 我正在尝试使用jQuery $.get()用增加的javascript questNum更新php $questNumber

Console.log tells me that the js questNum var is being incremented correctly. Console.log告诉我js questNum var正在正确递增。

But echo "testing..." . $questNumber; 但是echo "testing..." . $questNumber; echo "testing..." . $questNumber; outputs 1 even after I've incremented the value on the JS side. 即使在JS端增加值后,输出仍为1 This means the updated value is not being sent to $questNumber to update the database, and return the appropriate new set of values to the javascript side. 这意味着未将更新后的值发送到$questNumber来更新数据库,并将相应的新值集返回到javascript端。

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:

<?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: 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>

Try this ajax format 试试这个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');        
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM