简体   繁体   中英

PHP: Assigning an AJAX response value into PHP Variable

I've read all the articles but cant seem to get my ajax response into a PHP variable. Please can you advice. I want to assign rowid to a PHP variable.

$(document).on('click', '#updateid', function() { 
    var vallab = $('#idval').val(); 
    var rowid;
    $.ajax({
            url:'a.php',
            type: 'POST',
            async: false,
            data: {labid: vallab},
            success: function(data){
                // console.log(data);
                rowid = data;
            }
    });
    console.log(rowid);
    return rowid;
});

my a.php code is below

<?php 
# Fetch the variable if it's set.
$lab_id = (isset($_POST["labid"])) ? $_POST["labid"] : null; 

echo $lab_id;
?>

I am getting the response back with the id, and want to use it on that page I want to pass rowid into a PHP function so I need to get the value of rowid.

Please can you advice?

I cant seem to get my ajax response into a PHP variable

Well, the AJAX response came FROM a PHP file, right? So why don't you do whatever you need to do with the response right in that PHP file?

在此处输入图片说明

$.ajax({
        url:'THIS IS YOUR PHP FILE',
        type: 'POST',
        data: {THIS IS THE DATA YOU SEND TO PHP},
        success: function(data){
            console.log(data); //THIS IS THE RESPONSE YOU GET BACK
        }
});

You can't use it. Javascript is a scripting language which run in browser when the dom is loaded and elements are visible.

PHP is a serverside language and run on server before the page is loaded.

You need to understand the lifecycle of your application. Your php code executes once, it runs the full script from top to bottom when the page loads. At the point the script starts if can only access the post that came with the request (eg if you clicked submit on a form then the 'action' of the form receives the post). Any number of things can happen in your script, but once it's finished the php is gone, and so is the post (in basic terms). So you no longer have any access to the php which created this page.

Ajax allows you to update a section of your page - it sends a request to your sever and runs some php code - you must understand that this is a new and separate request, so the new post submission only exists in the lifecycle of this new execution and is in now way linked to the page that has already finished loading. Now you could ask Ajax to call your original script, but that wouldn't affect your page at all because the page does not reload. What you would get is a strange looking response which you (probably) couldn't do anything useful with.

Ajax allows small specific changes to the page, so when you get your response (which I assume you get in a format you want since you don't ask about it and you have a console.log) you then need to do something with jQuery/javascript. Instead of returning rowid write a javascript function like :

function printRowId(rowid) {
   $('#your html div id here').text('Row id is ' + rowid);
}

and then call it in your response:

$.ajax({
            url:'a.php',
            type: 'POST',
            async: false,
            data: {labid: vallab},
            success: function(data){
                // console.log(data);
                rowid = data;
            }
    });
    printRowId(rowid);
    return rowid;

You can use Ajax to update your data, update your database and then reflect the changes on the current page, but you cannot use it to pass directly to the php that has already finished executing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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