jl.
jl.

Reputation: 2239

Jquery submit and post to post a form unable to return data

i have problem with jquery submit and post. I would like to submit a form and post it to my php for a check, and return the php result back to the form. However, I have problem with the returning of data, it basically ignores the result.

This is my html form:

<form id="form" method="post">
    <p id="status">Status:</p>
    <p class="text">
        <label for="name" class="label">Name:</label>
        <input type="text" id="name" name="name" value="" size="30" />
    </p>
    <p class="text">
        <label for="email" class="label">Email:</label>
        <input type="text" id="email" name="email" value="" size="30" />
    </p>
    <p class="submit">
        <input type="submit" name="send_btn" id="send_btn" value="Send" />
    </p>
</form>

This is my javascript to do the submit and post:

$('#form').submit(function(e) {
    e.preventDefault();

        var name = $('#name').val();
        var email = $('#email').val();

        $.post('notify.php', {name: name, email: email}, function(data) {
            $('#status').html(data);
        });

});

This is the php that does the check and return the data:

<?php 
if (isset($_POST['name'], $_POST['email']))
{
    $name = htmlentities($_POST['name']);
    $email = htmlentities($_POST['email']);

    if ($name == "myname")
    {
        $output = 'It matches!';
    }
    else
    {
        $output = 'No matches!";
    }
}
?>

Can please highlight what has gone wrong? Thank you.

Upvotes: 1

Views: 521

Answers (4)

Mif.ComicVN
Mif.ComicVN

Reputation: 87

dont forget

return false;

If u forget it, the form when submit will refesh and ajax will fail ;

$('#form').submit(function(e) {
    e.preventDefault();

        var name = $('#name').val();
        var email = $('#email').val();

        $.post('notify.php', {name: name, email: email}, function(data) {
            $('#status').html(data);
        });
    return false;       
});

Upvotes: 0

andrewpthorp
andrewpthorp

Reputation: 5106

You need to echo or die in your php script, so your function can get the results.

So, change your script to this:

<?php 
if (isset($_POST['name'], $_POST['email']))
{
    $name = htmlentities($_POST['name']);
    $email = htmlentities($_POST['email']);

    if ($name == "myname")
    {
        $output = 'It matches!';
    }
    else
    {
        $output = 'No matches!';
    }

    echo $output;
}
?>

Notice the third to last line, where I am calling echo $output - whatever you echo will be returned from your ajax call. If you want to get more complex, you should return a JSON object.

$results = array("result" => "It Matches!", "foo" => "bar");
echo json_encode($results);

EDIT: You also need to change the " to a ' at the end of your else.

Upvotes: 2

udnisap
udnisap

Reputation: 909

Print the answer on PHP as comment suggest echo $output;

at the end of the line

Upvotes: 1

Michael Dillon
Michael Dillon

Reputation: 1037

In your php code, you aren't actually writing $output to the page.

<?php
    // your code

    echo $output;
?>

Upvotes: 0

Related Questions