Good Afternoon everybody.
I have got a quick question to ask about why my jQuery is not submitting my form data.
This is my form:
<form id="submitForm" action="" method="">
<input class="ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" type="text" name="from" placeholder="Your Email" style="color:#ccc;" />
<input class="ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" type="text" name="fullName" placeholder="Your Full Name" style="color:#ccc;" />
<input class="ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" type="text" name="contactNumber" placeholder="Your Contact Number" style="color:#ccc;" />
<input class="ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" type="text" name="subject" placeholder="Subject" style="color:#ccc;" />
<textarea placeholder="Your Message Here..." class="ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" name="body" style="color:#ccc;" ></textarea>
<input type="submit" id="search_button" value="Submit!" name="submit" />
</form>
Here is my jQuery code:
<script type="text/javascript">
$(document).ready(function(){
$('#loading, .loading-text').hide();
$('form#submitForm').submit(function( event ){
event.preventDefault();
document.write('Hola!');
$('#loading').show();
var formData = $('form#submitForm').serialize();
$.ajax({
url: 'mAjax.php',
data: formData,
type: "get",
success: function( data ){
$('#loading, .loading-text').show();
if( !data.error ){
$('.alertText').html(data).show();
} else {
$('.alertText').html(data + data).show();
}
$('#loading, .loading-text').hide();
}
});
return false;
});
});
</script>
I know this may be a vague question, but I have been trying for a while now and I can't think of anything else to try.
The problem that I'm having is that when I click submit or just submit the form, nothing happens, it doens't reach the document.write('Hola!'); bit and it most definatly doesn't reach the AJAX call.
Any help on what I could try to help this would be much appreciated.
Thanks in advance!
-
since you use "submitForm" as a "id" of form then no need to write like $('form#submitForm') just use $('#submitForm')Ariful Islam– Ariful Islam2011年10月10日 11:33:33 +00:00Commented Oct 10, 2011 at 11:33
-
Pasted your code into jsFiddle here jsfiddle.net/KnWte and it's working correctly. It's definitely reaching the document.write('Hola')njr101– njr1012011年10月10日 11:34:19 +00:00Commented Oct 10, 2011 at 11:34
-
The message appears in jfiddle. jsfiddle.net/yt9YY Did you include the jquery script? Did you include this file after you included jquery?Paul Stanley– Paul Stanley2011年10月10日 11:35:04 +00:00Commented Oct 10, 2011 at 11:35
-
I definatly have the jQuery scripts included. Mobile jQuery and normal. I have changed it to just $('#submitForm') and that made no difference, but worth a try. Thanks againDarkMantis– DarkMantis2011年10月10日 11:37:12 +00:00Commented Oct 10, 2011 at 11:37
-
Maybe it isn't reaching mAjax.php? Check the file path.noinstance– noinstance2011年10月10日 11:37:17 +00:00Commented Oct 10, 2011 at 11:37
1 Answer 1
I think this is because you didn't provide a method and an action.
Instead of:
<form id="submitForm" action="" method="">
Do:
<form id="submitForm" action="[your current file]" method="GET">