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.
You need to
echoordiein your php script, so your function can get the results.So, change your script to this:
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.EDIT: You also need to change the
"to a'at the end of your else.