-1

I'm calling ajax from AddSomething.php file as follows

<script> 
    var dataValue; 
    $("#schedule").change(function () { 
        var value = $('#schedule option:selected').text(); 
        var ajaxCheck = $.ajax({ 
            url: 'processClg.php', 
            type: 'POST', 
            dataType: 'json', // processClg.php will return string means change to text 
            data: { id: value }, 
            success: function(data){ 
                dataValue = data; 
                console.log(dataValue); 
            } 
        }); 
    }); 
</script>

Getting response in Network as follows

[{
    "id": "2",
    "scheduleName": "abc",
    "subject": "Patho",
    "university": "xyz",
    "facultyName": "Dr",
    "scheduleStartDate": "2015-06-05",
    "scheduleEndDate": "2015-06-09"
}]

And in console it is showing [Object]

How can I assign the above values to the following PHP variables present in the same page

<?php
    $scheduleStartDate ='';//Here How can i assign respected ajax responsevalue
    $scheduleEndDate = '';//Here How can i assign respected ajax response value

    $qry = "SELECT * FROM `mytable`
        WHERE `university` LIKE ''//Here How can i assign value  
        ORDER BY `university` DESC,student_name ASC";
?>

processClg.php file code

<?php
    include "config.php";
    $qry = "SELECT * FROM `schedule` WHERE scheduleName LIKE '".$_POST['id']."'";
    $res = mysqli_query($link, $qry);
    //echo $res;

    while($row = mysqli_fetch_assoc($res))
        $test[] = $row; 

    echo json_encode($test);
?>
1
  • 4
    You can't assign the result of an Ajax call to PHP variables. The PHP has already run on the server, done it's bit and sent the HTML / JavaScript to the browser. At that point you can no longer use PHP as it's a server side language and will only run on the server. Ajax uses JavaScript, which is a client side language. It only runs after the PHP has finished and the server has sent the HTML / JavaScript to the users browser. JavaScript is executed in the clients browser. Commented Jun 12, 2015 at 10:51

3 Answers 3

2

You can't assign the result of an Ajax call to PHP variables. The PHP has already run on the server, done it's bit and sent the HTML / JavaScript to the browser. At that point you can no longer use PHP as it's a server side language and will only run on the server. Ajax uses JavaScript, which is a client side language. It only runs after the PHP has finished and the server has sent the HTML / JavaScript to the users browser. JavaScript is executed in the clients browser.

You need to rethink what you're doing and where you're doing it. If you need the results of the Ajax call to do something in PHP then do it in your processClg.php file. You do all your PHP first, then send a response to the browser to display something to the user. If the user doesn't need any sort of confirmation or acknowledgement then you can just send the Ajax request and not bother with a response.

It looks like you're trying to dynamically load some University information. What you should do is put this part:

 $scheduleStartDate ='';//Here How can i assign respected ajax responsevalue
 $scheduleEndDate = '';//Here How can i assign respected ajax response value

$qry = "SELECT * FROM `mytable`
    WHERE `university` LIKE ''//Here How can i assign value  
    ORDER BY `university` DESC,student_name ASC";

into processClg.php after the first query. Then you get the results of the query and pass them back to your HTML page, like this:

$results = array();
$qry = "SELECT * FROM `mytable`
    WHERE `university` LIKE ''  
    ORDER BY `university` DESC,student_name ASC";
$result = mysqli_query($link, $qry);
while ($row = mysqli_fetch_assoc($result))
{
    $results[] = $row;
}
echo json_encode($results);
Sign up to request clarification or add additional context in comments.

3 Comments

I know that PHP runs server side and JavaScript runs in client side but there should be means, Please suggest how can i achieve im my case
I'm calling ajax on select $("#schedule").change(function (){ And expecting to assign the response to where condition of the above query and to the PHP variables $scheduleStartDate = ''; and $scheduleEndDate = ''; Is there any way to achieve this please suggest me any method that, you know
@Nag, I did in my answer above. Move your query into processClg.php.
1

You can store ajax result in php variable by embedding ajax result div into php tag

<?php
 $result='<div id="result"></div>';// Here you embed div in php tag and store in php varible
?>

Comments

0

You cann't do this job as you think the way.

For that , you need to assign the return values to any hidden field form value and submit the form.

Also keep in mind for ajax response data keep in the js variable, you need to pass the parameter async:false

1 Comment

var result; $.ajax({ type: "POST", async: false, url: url, data: postdata, //dataType: "json", success: function (data) { result= data; } }); result = jQuery.parseJSON(result); $('#startDate').val(result.startDate); $('#endDate').val(result.endDate); The Form will like that: <form method="post" name="frmDate" action=""> <input type="hidden" name="startDate" id="startDate" value=""> <input type="hidden" name="endDate" id="endDate" value=""> </form>

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.