Как вывести различное значение переменной для разных идентификаторов из ответа ajax? - PullRequest
1 голос
/ 29 марта 2019

Как получить другое значение переменной в разных идентификаторах из ответа ajax.

В моем программировании получить все ответы из файла test.php, но я хочу по-разному для обеих переменных.

Файл Index.php: -

 <html>
    <span> Picture : </span>
    <!-- In picture hint i want to print $varbilefirst value from test.php file  -->
    <span id="picturehintget"></span>
    <input type="button" id="<?php echo "8"; ?>" class="submitnone" name="ansclick" onclick="QuestionId(this.id)" value="Submit">
    <div> Demo : <p id="demoquiz"></p> </div>
   <!--In Demo i want to print $varbilesec value from test.php file  -->
    <script type="text/javascript">
    function QuestionId(obj)
    {
        var id = obj;
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function ()
        {
            if (this.readyState == 4 && this.status == 200)
            {
                document.getElementById("demoquiz").innerHTML = this.responseText;

                document.getElementById("picturehintget").innerHTML = this.responseText;
            } 
        };

        xhttp.open("GET", "test.php?id=" + id, true);
        xhttp.send();

    }
    </script> 
    </html>
    test.php file

    <?php
        $id = $_GET['id'];
        $varbilefirst = "For Picture Hint";
        echo $varbilefirst;
        $varbilesec = "For Demo Hint";
        echo $varbilesec;
    ?>

Я хочу совершенно другое значение переменной для разных идентификаторов.

 <span id="picturehintget">For Picture Hint</span>
<p id="demoquiz">For Demo Hint</p>

Ответы [ 2 ]

0 голосов
/ 29 марта 2019

Если я правильно понял ваш запрос, вы можете вернуть JSON из test.php следующим образом:

<?php
$id = $_GET['id'];
$myObj->demo= $varbilefirst;
$myObj->picture = $varbilesec; 
$myJSON = json_encode($myObj);
echo $myJSON;
?>

Внутри if of Javascript:

  if (this.readyState == 4 && this.status == 200)
            {
             var myObj = JSON.parse(this.responseText);
               document.getElementById("demoquiz").innerHTML = myObj.demo;
               document.getElementById("picturehintget").innerHTML = myObj.picture;
            } 
0 голосов
/ 29 марта 2019

Вы можете попробовать в файле php, используя массив

<?php
    $id = $_GET['id'];
    $varbilefirst = "For Picture Hint";
    $varbilesec = "For Demo Hint";
    echo json_encode(["varbilefirst"=>$varbilefirst, "varbilesec"=>$varbilesec]);
?>

В Javascript

<script type="text/javascript">
    function QuestionId(obj)
    {
        var id = obj;
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function ()
        {
            if (this.readyState == 4 && this.status == 200)
            {
                document.getElementById("demoquiz").innerHTML = this.responseText.varbilesec;

                document.getElementById("picturehintget").innerHTML = this.responseText.varbilefirst;
            } 
        };

        xhttp.open("GET", "test.php?id=" + id, true);
        xhttp.send();

    }
    </script> 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...