0

Thank you for your time reading this. Ok, so I know how to pass a jquery variable to php with no action needed on the same page but now I need the opposite and I do not seem to find any documentation about it. Imagine the simpliest possible thing. I pass the name Jack to jquery like this:

<?php
$my_name = "Jack Goodman";
?>
<script>
$(document).ready(function(){
var my_name = <?php echo json_encode($my_name); ?>;
alert(my_name); 
});
</script>

The question is how do I get the name from jquery back in php variable after?

<?php
$my_name = ?
?>
  • 1
    You need to use AJAX to send a JS variable, which is on the client, to your PHP code, which is executed on the server – Rory McCrossan Apr 03 '17 at 09:43
  • AJAX should fix this. Read more on AJAX – Rotimi Apr 03 '17 at 09:44
  • 1
    and why json_encode a string? It's not necessary – Jouby Apr 03 '17 at 09:45
  • In your case 1 you are not passing the variable from php to jquery (the language is js). You are just placing the value in the js code. To achieve what you want, i.e. send some value from the frontend (js) to the backend (php), you can use an Ajax request or submit a form. – Ajay Brahmakshatriya Apr 03 '17 at 09:45
  • You can't assign javascript or jquery variable value into a PHP variable like you mentioned in the above case. – Web_Developer Apr 03 '17 at 09:48
  • Please refer http://stackoverflow.com/a/2338960/3894567 – Sanchit Gupta Apr 03 '17 at 09:48
  • it's important to understand that all of your php code runs on the server, and then your js code runs on the client. By the time your js code runs, the php process has unloaded. You can pass variables to your server in another http request. That means page reload or ajax. – Garr Godfrey Apr 03 '17 at 09:51
  • @RoryMcCrossan I think you have a good point there. I will try shortly. Akin - same. Joudy - Try passing anything but a number with the code above without json_encode and you will see why Ajay Brahmakshatriya How the hell did you manage to gain so much reputation with saying things like this jQuery is a javascript library (or the mentioned from you "js"). Web_Developer I hope you are wrong but since I could not find anything about it I guess you are right :( Sanchit Gupta I read this earlier already. –  Apr 03 '17 at 12:11
  • @Garr Godfrey I am still trying to wrap my head around it but the issue is my jQuery code can recognise if a string contains characters in a certain UTF16 range list or not and apparently php is stupid enough not to be able to do so, or I am for not being able to find the solution :D –  Apr 03 '17 at 12:12

1 Answers1

0

You need to understand how pages are constructed. PHP is a server side language, that means that all the PHP is executed prior to being sent to the client (i.e. the persons browser). Your PHP is parsed and what is sent to the client is:

<script>
    $(document).ready(function(){
        var my_name = 'Jack Goodman';
        alert(my_name); 
    });
</script>

That is then executed on the client's PC. The PHP has completely finished, there is no way for the client to change the value of the PHP variable, it doesn't exist any more. The only way to pass information from the client to the server is to open a new connection (e.g. a new page) and set the information you want to send to the server as a GET or POST variable.

Imagine going to a page on your website, you enter a URL in the browser such as http://www.mywebsite.com. We can include variable information by using GET variables, so if we wanted to pass the name in it would be something like http://www.mywebsite.com?name=My%20Name (%20 is a space). In your PHP you can then access this using the $_GET superglobal, e.g. $name = $_GET['name'] and use it as you need.

So if you have some information you need sending to the server, traditionally you would use a form:

<form action="mypage.php" method="get" id="myform">
    <input type="text" name="name" id="myname" value="Jack Goodman">
    <button type="submit">Submit</button>
</form>

However you can also send data asynchronously using AJAX. That means we don't have to open a new page, or submit a form to send data to the server. AJAX allows us to send some data to the server and read its response. So to send the name variable we can use jQuery's AJAX plugin.

// We intercept the submission of the form
$('#myform').submit(function () {
    $.ajax({
        url: 'mypage.php',
        type: 'GET',
        dataType: 'JSON',
        data: { name: $('#myname').val() }
    }).success(function (response) {
        alert(response);
    });

    return false; // This stops the form submitting so the page doesn't reload.
});

response will contain whatever the PHP page outputs. So if your PHP page looked like this:

<?php
$name = $_GET['name'];
// ... do something with $name
$response = ['result' => 'success', 'name' => $name];
echo json_encode($response);

We're passing back a JSON string, so response will contain that JSON string. However because we know that it's a JSON string and we tell jQuery to expect a JSON string response (by using dataType: 'JSON') jQuery will automatically convert that to an object for us, so response contains an object with the properties result and name.

Styphon
  • 10,304
  • 9
  • 52
  • 86
  • Lets say I send the variable to mypage.php with ajax which will then return a php variable with it... do you think this will work since there is no actual "client side"? The whole action is supposed to happen on an api. The app sends a request to the api, the api sends a request to mypage.php and returns back a php variable to the api. It kinda feels like a movie where you go from a dimension into a deeper dimension and then to a third even deeper dimension :D –  Apr 03 '17 at 12:26
  • One more thing. I do need jquery in the picture because neither php nor any other language so far is as efficient into recognising a language off of a given string as it. PHP can not recognise (or so far I did not find evidence) if a character is in a particular UTF16 range or not. –  Apr 03 '17 at 12:35