0

I'm posting data to a php page using jquery. I want to then have the response from the page be able to be put into php variables on the original page. I don't want to append a DIV or show the results in the HTML. (This seems to be the only thing people do with post.)

Here's the jquery:

$(document).ready(function () 
{
    $('#drpdown').change(function() 
    {
        drpdownval=$('#drpdown').val();
        $.post("page2.php",{incomingval:drpdownval},function(result))};

    });
});

PHP page2.php:

<?php
$valtoworkwith = $_REQUEST['incomingval'];
......../// do something
?>

I want to then do something on page2.php then send specific values back to the jquery to work with. How? How do I assign them a name or separate them out??

EDIT

My jquery needed a number and not text.

Here's the jquery:

$(document).ready(function () 
{
    $('#drpdown').change(function() 
    {
        drpdownval=$('#drpdown').val();
        $.post("page2.php",{incomingval:drpdownval},function(result){
          if(result != 1){ ///could not put result !="hello" or != hello
          $("#signup").overlay().load();  
          }
        )};
    });
});
user1261388
  • 55
  • 1
  • 1
  • 9
  • 1
    The original page can't put them into PHP variables, it can only put them into Javascript or the DOM. The PHP of the original page is already done when the page is rendered on the client and the jQuery runs. – Barmar Apr 12 '14 at 00:38
  • Right...I want to use it in my original jquery. If my 'do something' exists send a 'thumbs up' to the original page. I just can't figure out how to get the results to the page. (And when I can how to work with them..) – user1261388 Apr 12 '14 at 00:54

3 Answers3

0

Use JSON to get your inter-connectivity going, meaning the data exchange between client and server entirely through AJAX. Here's a link to a StackOverflow answer which goes into detail about how to make that happen, using PHP for the back end:

jQuery AJAX Call to PHP Script with JSON Return

Community
  • 1
  • 1
DoctorLouie
  • 2,699
  • 1
  • 18
  • 23
  • 1
    I think you may be on to something...(and why didn't I find this earlier??) – user1261388 Apr 12 '14 at 00:48
  • Must know what to look for sometimes, which may lead to hours of searching online. To be honest, taking those few hours to learn this, when I did, helped learn other things along the way as well. So in essence, its always good to do more research on subject, before throwing in the towel. Sometimes the answer is much more fruitful if you find all possibilities to the question :) – DoctorLouie Apr 12 '14 at 00:50
0

Simply go here, it's as clear as it can get...

https://api.jquery.com/jQuery.ajax/

That's the documentation page for $.ajax, and if you scroll down you will have excellent examples you can start from.

ied3vil
  • 964
  • 1
  • 7
  • 18
  • I'm a script newbie...and yes, I've read over this page four times over the last few days trying to understand or make it work for me. I'm not wanting the default response from the post. I want to return values (actually from a sql database....I simplified my original question hoping for an faster answer.) – user1261388 Apr 12 '14 at 00:44
  • In this case, the .php page you are ajaxing should be a mysql_query then echo the result somehow, either with json_encode or just plainly echo the value (if you just need one), and process it in the ajax.success - refer to docs. Also you will find that there is a way to send data along with the ajax requests! – ied3vil Apr 12 '14 at 00:47
  • That's what I need...how to process it in the success. I can't get a clear explanation on how. Just examples ALL OVER showing how to add to DOM. – user1261388 Apr 12 '14 at 00:50
  • Simple... play with it a bit. Do an ajax post to a "demo.php" file which contains just a "hello" string or echo "hello"; if you wish then test by doing a console.log or alert in the success method, something like this: success: function(data) { alert(data); } See what that gets you. – ied3vil Apr 12 '14 at 01:06
  • I can get the result to the page and display in a DIV. I just can't figure out how to manipulate like a php variable or even use in a conditional statement. If "hello" is returned, why can't I create an if(results == 'hello'){do something...}. (I've tried several ways and nothing works. Is it because the value comes after the page is complete? It can't be used in other lines of jquery just displayed in the browser?? – user1261388 Apr 12 '14 at 01:23
  • success: function(data) { if (data == 'hello') { //do something } } – ied3vil Apr 12 '14 at 08:20
  • Finally got things to work. For some reason it could not filter or check letters. When I changed it to a number it worked! (see edit) – user1261388 Apr 17 '14 at 22:06
  • Take a note you can specify dataType in your request. You should return JSON to be on the safe side, and it works 100% – ied3vil Apr 18 '14 at 14:14
0

Are you trying to do something like that?

if (!empty($_POST['incomingval'])){
  $valtoworkwith = $_POST['incomingval'];
  //do some stuff
  echo $valtoworkwith;
}

Or if you want to sent back a JSON

<?php

  function doSomeStuff($input) {
    //instead of that do some stuff and return back something that makes sense
    $output = $input;
    //
    return $output;
  }

  if (!empty($_POST['incomingval'])){
    header('Content-Type: application/json');

    $valtoworkwith = $_POST['incomingval'];
    $res = doSomeStuff($valtoworkwith);

    $data = array('incomingval' => $valtoworkwith, 'response' => $res);
    echo json_encode($data);
  }

?>

I hope that helped you because the answer it self was kind of vague.

Tested with postman

The in JS you can do with a promise

$(document).ready(function () 
{
    $('#drpdown').change(function() 
    {
        drpdownval=$('#drpdown').val();

        $.post("page2.php",{incomingval:drpdownval})
        .then(function(data){ //look how awesome the promise looks
        console.log(data.response); //do something with it
        })

    });
});
  • I want to take the response of the page2.php, put it in a php variable `$valtoworkwith` (Or the like). Then send it to the original pages jquery and use it to continue the script in a conditional statement. – user1261388 Apr 12 '14 at 01:28
  • sorry just edited, use a promise then, with then you can even chain calls – Alexandros Spyropoulos Apr 12 '14 at 01:37