I’m new to jQuery / AJAX.
I’m trying to send single input with jquery/ajax/php.
But, after pressing submit nothing is happening, where is my error?
Any help much appreciated.
HTML:
<form action="submit.php">
<input id="number" name="number" type="text" />
<input id="submit" name="submit" type="submit" />
</form>
JQUERY / AJAX:
$(document).ready(function(e) {
$('input#submit').click(function() {
var number = $('input[name=number]');
var data = 'number=' + number.val();
$.ajax({
url: "submit.php",
type: "GET",
data: data,
cache: false,
success: function(html) {
if (html == 1) {
alert('wyslane');
}
else {
alert('error');
}
}
});
return false;
});
});
PHP:
<?php
$mailTo = 'email@gmail.com';
$mailFrom = 'email@gmail.com';
$subject = 'Call Back';
$number = ($_GET['number']) ? $_GET['number'] : $_POST['number'];
mail($mailTo, $subject, $number, "From: ".$mailFrom);
?>
HTML:
The action URL is irrelevant as you want to submit your data via AJAX. Add the
submitid to the form and override the default submit behavior, instead of overriding theonclickhandler of the submit button. I’ll explain in the JS section.JS:
Quotes were missing.
I don’t really understand your success callback, why do you expect that
htmlshould be equal to 1?