0

i have been reading some posts here regarding issues about storing values form JS to a PHP variable and most comments said to use AJAX.

both AJAX and JS codes can be used to store JS variable value to a PHP variable however can someone explain why most people suggest to use AJAX? or what advantages do I have if I used AJAX over JS to store that value

thanks

Reub
  • 665
  • 2
  • 18
  • 35
  • 1
    _storing values form JS to a PHP variable and most comments said to use AJAX_ .You've answer your own question, I guess. – Chay22 Apr 29 '16 at 17:32
  • You use AJAX when your page needs to get data without refreshing. – Jose Manuel Abarca Rodríguez Apr 29 '16 at 17:33
  • You can send a JSON object with multiple variables in a single request as @Jose Manuel Abarca Rodríguez said without refreshing. It's pretty easy to build a JSON object in JS and it's becoming a standard in data transfers. – Jose Serodio Apr 29 '16 at 17:37
  • Unless the data is sent on https, remember it is not secure. PHP allows you to store a data object as a session variable, thereby restricting access to the session user. This can be accomplished in JS as well, and is available in many frameworks. – Kirk Powell Apr 29 '16 at 17:43
  • 1
    You're a bit mixed up on what AJAX is. AJAX isn't a language or something separate from JavaScript. It's a means of sending data to a server. Think of it this way: You (JS) write a letter and stick it in an envelope with the address on it. The mailman (AJAX) gets it to its destination. You write JS that *uses* AJAX. You don't *replace* JS with AJAX. – Mike Cluck Apr 29 '16 at 17:50

1 Answers1

0

You cannot store a PHP value from javascript/jQuery without using AJAX.

After the DOM has been rendered, no additional PHP will run. That's it, all done. In order to send additional data to the server, you have two choices: (1) post a form to another PHP file, which will send the data typed into the form elements and will change/refresh the current page. or (2) use AJAX.

AJAX is a way to communicate with a separate PHP page, sending data back/forth, without refreshing/changing the page the user is on. An AJAX code block looks something like this:

$.ajax({
    type: 'post',
     url: 'ajax.php',
    data: 'varName=' +varSomething,
    success: function(d){
        if (d.length) alert(d);
    }
});

or

$.ajax({
    type: 'post',
     url: 'ajax.php',
    data: 'varSomename=' +varValue
}).done(function(){
    //success function
});

Data sent back from PHP is received in the AJAX code block's success function (and ONLY there), where it can be manipulated and/or injected back into the DOM, like this:

$.ajax({
    type: 'post',
     url: 'ajax.php',
    data: 'thevar=' +theval,
    success: function(d){
        $('#someDIV').html(d);
    }
});

Here are some additional links re AJAX:

dynamic drop down box?

Prevent Page Load on Jquery Form Submit with None Display Button


When the data arrives at the specified PHP file (ajax.php in above examples), you can get that data via the $_POST[] variable:

<?php
    $summat = $_POST['someVarname'];

    //To send data back to the AJAX success function, you just echo it:
    $out = '<div style="font-size:3rem;color:blue;">';
    $out .= $summat;
    $out .= '</div>';
    echo $out

Now you have the data in a local PHP variable. This variable will cease to exist when the PHP file execution is completed. To retain the PHP variable, use a $_SESSION super-variable. In order to use the $_SESSION variable, you must add session_start() to the top of the PHP page:

<?php
    session_start();
    $_SESSION['summat'] = $_POST['someVarname'];

    //Now you have a permanent variable on the server, associated with this user's PHP session
Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111