0

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?

Wiimm
  • 2,971
  • 1
  • 15
  • 25
edcoder
  • 503
  • 1
  • 5
  • 19
  • show `a.php` file code and can you tell me where is your PHP variable in above js code? – M.Hemant May 08 '19 at 08:33
  • 1
    Seems like a classic problem of not knowing the difference between what is executed on the server (PHP code), and what is done on the client (Javascript code). – KIKO Software May 08 '19 at 08:35
  • 1
    I am a little confused by your question! To pass `rowid` to a PHP function you will have to call another AJAX call. – RiggsFolly May 08 '19 at 08:39
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Zain Farooq May 08 '19 at 08:56

3 Answers3

1

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?

enter image description here

$.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
        }
});
oliver_siegel
  • 1,666
  • 3
  • 22
  • 37
  • Thank you Olli, yes, thats what I have done now. I used my a.php to process the data. I thought there would be some way to pass it back into the php file that I was working on hoping that I can build a SPA application. But this seems the quickest. Thank you for describing it well. – edcoder May 08 '19 at 11:09
  • Either you can rewrite your current PHP file to continue modifying the data until it's what you need, or if you need the data first to be back in JavaScript, and then send it to PHP a second time, then you can simply restart the AJAX process again with another call. just do Step 1. again, send the POST data to PHP – oliver_siegel May 08 '19 at 11:11
  • 1
    @olli - lols @ the php secret magic, that must make me a wizard! I wonder what house I should be in! – imposterSyndrome May 08 '19 at 11:28
0

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.

loc4l
  • 21
  • 5
0

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 (e.g 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

imposterSyndrome
  • 896
  • 1
  • 7
  • 18
  • thank You Jameson. Yes, I understand. I was just hoping that I could get it into the same PHP file. I used the ajax source php file to do what I needed. Thank you for being very descriptive. – edcoder May 08 '19 at 11:10
  • you can do it in the same file as you can send an Ajax request to whatever script you like, but you still need to update the page in the front end - the key thing is that you need to ask javascript to do something, when you echo $lab_id; you send that response back to your Ajax function - so although it doesn't appear on the screen you have got 90% of the way to what you expect, you just need to handle the response. Glad this was helpful, feel free to mark it as an accepted answer! – imposterSyndrome May 08 '19 at 11:27