0

I have actually read all related answers to my question but I need a clear and simple example on how to properly implement my code below.

myHome.php

jquery

var url = "computeArea.php";
var data = $('thisForm').serialize();
$.post(url,data,function(response)); // how do i get the area being returned from  
                                       computeArea function? i need to save the 
                                       return value to a javascript variable

computeArea.php

function computeArea ($data){ // do i need to parse $data to make it an array?
    return $area;
}

im new to jquery and your help is very helpful. thank you!

iamhealed
  • 35
  • 6

4 Answers4

1

You can do:

$.post(url,data,function(response){
   alert(response)
});

ps: you are missing the . between $ and post.

In your php code you could do that:

 echo json_encode($area);
Mimo
  • 6,015
  • 6
  • 36
  • 46
  • I added the `echo json_encode($val)` on the php bit, because with just return you won't get anything client side – Mimo Jul 12 '13 at 01:03
  • ok, i tried this one but my page will only reload. something is wrong with the $.post() because alert ("hello") wont popup. any idea? – iamhealed Jul 12 '13 at 02:43
  • This might be because you do the serialize on form submit event, which if not prevented issues the page reload. – Mimo Jul 12 '13 at 02:57
  • ok, the popup will now show but the response is empty. i even pass a literal string just to check if it will return the value but it did not. what could be missing here? – iamhealed Jul 12 '13 at 03:04
0

You are misunderstanding the use of post requests. This will not call the computeArea function in computeArea.php and pass data as its parameter:

var data = $('thisForm').serialize();
$.post(url,data,function(response));

You can do this instead for computeArea.php:

$data = $_POST['watever_you_are_serializing'];
// Do computations, etc.
$area = 123; // Contains computed area
echo $area;  // Or json_encode($area);

If you need to call that function from computeArea.php, then you can create a new file for $.post request (eg. computeArea2.php) and include computeArea.php from there. It would be something like this:

include 'computeArea.php';
$data = $_POST['watever_you_are_serializing'];
echo computeArea($data);
Rainulf
  • 341
  • 1
  • 9
  • hello, i just made it this way to make it simpler..but i really need to post the value because the php function is a third party app. – iamhealed Jul 12 '13 at 01:16
  • If that's the case, then you can just call that function with the data you will get from $_POST as its parameter. And finally echo its return. – Rainulf Jul 12 '13 at 01:18
0

Do a simple teste.

jQuery:

$.post("url/to/file.php",{variable_name: "hello"/*(we'll give this value to variable_name*/)},
  function(response){
    if(response>0)
       alert('Something went wrong');
    else
       alert(response);
});

Now, on server side:

<?php
 if(isset($_POST['variable_name']) && $_POST['variable_name']!=="")
  echo $_POST['variable_name'];
 else
  echo 1;
?>
calexandru
  • 307
  • 1
  • 3
  • 16
-1

Something along the lines of:

var url  = "computeArea.php",
    data = $('thisForm').serialize(),
    new_variable;

$.post(url, data, function(response) {

    new_variable = response;

});

Though I presume there's a bit more to your PHP script, as otherwise $area isn't defined anywhere.

christian.thomas
  • 1,122
  • 1
  • 8
  • 19