0

this is my php script from which am returning the value to the ajax calling it

<?php

$questionid=$_GET['qid'];
$answer=$_GET['clickedvalue'];
$dbconnect=mysqli_connect('localhost','root','','quiz')or die("Error Connecting to database");
$query="select answer from question_answer where id=$questionid";
    $result=mysqli_query($dbconnect,$query);
    while($rows=mysqli_fetch_array($result))
    {
        $dbanswer=$rows['answer'];  
    }


    //array values which will be passed to json 
    $result=array('correct'=>'Correct Answer',
                   'incorrect'=>'Incorrect Answer'
                );

    if($dbanswer==$answer)
        {
            //json to be passed to next page with key value pair
            echo json_encode(array('display_msg'=>$result['correct'],'points'=>'positive'));    
        }
    else{
            echo json_encode(array('display_msg'=>$result['incorrect'],'points'=>'negative'));
        }   

?>

and this is my ajax code

   $.ajax({
        url:'checkanswer.php',
        dataType:'json',
        data:{'clickedvalue':clickedvalue,'qid':qid},
        success:function(data){
        $this.find(".report").html(data.display_msg);
        $this.delay(1000).slideUp();
        }

        });

So my question is how do i store the value of data.points object that is passed from the php as a json in the javascript variable or is it not possible to store in javascript variable directly if yes how and if no what will be the way to get the value and store somewhere

2
  • store it where ? what you are trying to achieve ? Commented Aug 24, 2013 at 6:56
  • problem solved just trying to fetch the json object did some mistake so before Commented Aug 24, 2013 at 14:18

2 Answers 2

2

Just add a temp variable before calling ajax Some thing like this

var myTempVariable; //Temp JS variable to use somewhere else
$.ajax(
{
    url: 'checkanswer.php',
    dataType: 'json',
    data: 
    {
        'clickedvalue': clickedvalue,
        'qid': qid
    },
    success: function(data) {
        $this.find(".report").html(data.display_msg);
        $this.delay(1000).slideUp();
        myTempVariable = data; //assugn value to temp varaible
    }

});
Sign up to request clarification or add additional context in comments.

Comments

0

in your ajax success function :

var myVariable = data.points;

This might help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.