AJAX works on first form, but not latter forms - why?
Posted: Thu Aug 07, 2014 8:44 am
This works for the first form, but not for the latter forms.
I've tried naming the forms to give them an identity, but not sure if that is how to separate them, or even why the latter one isn't working anyway.
JS file is:
PHP
I've tried naming the forms to give them an identity, but not sure if that is how to separate them, or even why the latter one isn't working anyway.
Code: Select all
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="/js/js_sendtofriend.js"></script>
<form name='1'>
<input type='text' id='email' name='email' class='sendtofriendemail'/>
<input type='hidden' id='buildid' name='buildid' value='$row->buildid'/><input type='button' id='submit' value='Submit' class='submitnewsletter'/>
</form>
<form name='2'>
<input type='text' id='email' name='email' class='sendtofriendemail'/>
<input type='hidden' id='buildid' name='buildid' value='$row->buildid'/><input type='button' id='submit' value='Submit' class='submitnewsletter'/>
</form>Code: Select all
$(document).ready(function(){
$("#submit").click(function(){
var email = $("#email").val();
var buildid = $("#buildid").val();
// Returns successful data submission message when the entered information is stored in database.
var dataString = '&email=' + email + '&buildid=' + buildid;
if(email=='')
{
alert("Please Fill All Fields");
}
else
{
//AJAX code to submit form.
$.ajax({
type: "POST",
url: "ajax_sendtofriend.php",
data: dataString,
cache: false,
success: function(result){
alert(result);
}
});
}
return false;
});
});Code: Select all
$email = isset($_POST['email']) ? $_POST['email'] : null;
$buildid = isset($_POST['buildid']) ? $_POST['buildid'] : null;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "This ($email) email address is considered invalid.";
}
else
{
$to = "$email";
$subject = "Wishlist";
$headers = "From: $email";
$body = "Thank you for your email. We will be in touch with you as soon as possible.
Regards,
";
mail ($to, $subject, $body, $headers);
echo "Thank you for submitting to $email your Build $buildid";
}