-
Ajax/PHP issue
Can someone please help me with this code by looking over it for something obvious? The problem seems to be that the embedded JavaScript code is not being executed and, therefore, the PHP code is also skipped. Both, the HTML and the PHP files are in the root directory.
html/js:
Code:
<div class="container">
<!-- Contacts -->
<div id="contacts">
<div class="row">
<!-- Alignment -->
<div class="col-sm-offset-3 col-sm-6">
<p> </p>
<p>Some header message here</p>
<p> </p>
<form name="contact" class="well" id="contact">
<legend>Contact Form</legend>
<div class="control-group">
<div class="controls">
<input type="text" class="form-control" placeholder="Name" id="name" />
<p class="help-block"></p>
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="email" class="form-control" placeholder="Email" id="email" required/>
</div>
</div>
<div class="control-group">
<div class="controls">
<textarea rows="10" cols="100" class="form-control" placeholder="Message" id="message" style="resize:none"></textarea>
</div>
</div>
<div id="success"> </div>
<button type="submit" class="btn btn-primary pull-right" id="submit">Send</button>
<button type="reset" class="btn btn-default pull-right" id="res">Reset</button>
<br />
</form>
</div>
</div>
</div>
</div>
<script>
$(function() {
$("button#submit").click(function(event){
event.preventDefault();
$.ajax({
type: "POST",
url: "process.php",
data: $('form.contact').serialize(),
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
});
});
</script>
</body>
</html>
php:
PHP Code:
<?php
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['Email']);
$message= strip_tags($_POST['message']);
echo "Name =".$name."</br>";
echo "Email =".$email."</br>";
echo "Message =".$message."</br>";
echo "<span class=\"label label-info\" >your message has been submitted .. Thank you</span>";
}
?>
-
Re: Ajax/PHP issue
Not sure what web browser you are using, but almost all have consoles to help with JavaScript debugging. I would highly suggest trying Firebug for Firefox to see what is failing.