I want to submit this form via jquery ajax, this is what I have made and it is not working. i.e. Form is submitting with page refresh and I am not seeing the response i.e. printing array on the same page.
HTML
<link rel='stylesheet' type='text/css' href='css/pepper-grinder/jquery-ui-1.10.4.custom.css' />
<script type='text/javascript' src='js/jquery-1.10.2.js' ></script>
<script type='text/javascript' src='js/jquery-ui-1.10.4.custom.min.js' ></script>
<form id="form1" method="get" action="submit.php ">
<label>Name of Organization</label>
<input type="text" name="OrgName" id="OrgName" class="textfield">
<label>Address of Organization</label>
<input type="text" name="OrgAddress" id="OrgAddress" class="textfield">
<input type="submit" value="Register Organization">
</form>
<div id="response">ads</div>
<script>
$document.ready(function(){
$("#form1").click((function(event){
event.preventDefault();
$.ajax({
url:'submit.php',
type:'GET',
data:$(this).serialize(),
success:function(result){
$("#response").text(result);
}
});
});
});
</script>
PHP (submit.php)
<?php
print_r($_GET);
?>
The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.
We can submit a form by ajax using submit button and by mentioning the values of the following parameters. type: It is used to specify the type of request. url: It is used to specify the URL to send the request to. data: It is used to specify data to be sent to the server.
The .serialize() method creates a text string in standard URL-encoded notation. It can act on a jQuery object that has selected individual form controls, such as <input> , <textarea> , and <select> : $( "input, textarea, select" ).serialize();
A standard form submit sends a new HTTP request (POST or GET) and loads the new page in the browser. In Ajax, the data is sent to the server (POST or GET) in the background, without affecting the page at all, and the response is then received by javascript in the background, again without affecting the page at all.
Use this - there have been a few syntax errors and the event has to be submit
$(function(){
$("#form1").submit(function(event){
event.preventDefault();
$.ajax({
url:'submit.php',
type:'GET',
data:$(this).serialize(),
success:function(result){
$("#response").text(result);
}
});
});
});
$document.ready(function(){
$("#form1 input[type='submit']").click(function(event){
event.preventDefault();
$.ajax({
url:'submit.php',
type:'GET',
data:$(this).serialize(),
success:function(result){
$("#response").text(result);
}
});
});
});
I your code $("#form1").click(.... does not have any meaning here... You want the event handler when you press the submit button. So I think If you take appropriate the selector then It might work perfectly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With