0

I am just messing around with some php and jquery. My test site has the user enter in numbers in a form and then when they click submit the information is sent to that php file, does some calculations and then prints the result in a <div> that I specify without reloading the page. I just want to know is there any way to retrieve that value that the php file returns in a jquery variable?

CODE:

$(document).ready(function(){
    $("#results").hide();
    $("#myform").validate({
        debug: false,
        rules: {

        },
        messages: {

        },
        submitHandler: function(form) {
            // do other stuff for a valid form
            $.post('process.php', $("#myform").serialize(), function(data) {
                $('#results').show();
                $('#results').html(data) ;


            });

        }
    });
2
  • 1
    Have you seen $.ajax()? Commented Jan 14, 2013 at 0:51
  • Yes, all you need to do is to return response in JSON for better integration with JavaScript Commented Jan 14, 2013 at 0:51

1 Answer 1

3

Your PHP value is already being retuned as data.

To use it outside of your handler, assign it to a global variable.

var phpData = '';

$(document).ready(function(){
    $("#results").hide();
    $("#myform").validate({
        debug: false,
        rules: {

        },
        messages: {

        },
        submitHandler: function(form) {
            // do other stuff for a valid form
            $.post('process.php', $("#myform").serialize(), function(data) {
                $('#results').html(data).show(); // you can chain for efficiency
                phpData = data;
            });

        }
    });
});

For more functionality, you can send the data from PHP as a JSON object.

$data = array('success' => true,
              'message' => 'Your message',
              'otherNum' => 25);

echo json_encode($data);

Then in your Javascript you can access it as follows.

data.success = true
data.message = 'Your message'
data.otherNum = 25
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.