Forum Moderators: open

Message Too Old, No Replies

jquery form plugin

         

asg333

1:32 am on Apr 11, 2011 (gmt 0)

10+ Year Member



Hi,

I am trying to auto submit a form using the jquery form plug-in.

Here is my code:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps TEST</title>

<script type="text/javascript" src="/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="/jquery.form.js"></script>
<script type="text/javascript" src="/jquery.gmap-1.1.0.js"></script>
<script type="text/javascript">
// prepare the form when the DOM is ready
$(document).ready(function() {
// bind form using ajaxForm
$('#htmlForm').ajaxForm({
// target identifies the element(s) to update with the server response
target: '#htmlExampleTarget',

// success identifies the function to invoke when the server response
// has been received; here we apply a fade-in effect to the new content
success: function() {
$('#htmlExampleTarget').fadeIn('slow');
}
});
});
</script>
</head>


<form id="htmlForm" action="html-echo.php" method="POST">
Message: <input type="text" name="message" onKeyUp="if(this.value.length>4)htmlForm.submit()">
</form>

<div id="htmlExampleTarget"><div>




</body>
</html>


If I use the above code, the file html-echo.php is loaded instead of updating the htmlExampleTarget div with the form data.

If I remove the automatic submit, and submit the form using a submit button, it works fine.

Any ideas?

Thank you!

Tommybs

8:13 pm on Apr 15, 2011 (gmt 0)

10+ Year Member



It's probably because the htmlFrom.submit method causes the form to actually submit. Whereas the ajaxForm is returning false for the default action. Have you tried changing your onKeyUp event to

onKeyUp="if(this.value.length>4)htmlForm.submit(); return false;"

asg333

10:25 pm on Apr 15, 2011 (gmt 0)

10+ Year Member



Thank you for the suggestion Tommy. Still doing the same thing, loading the action page instead of performing the ajax function.

Fotiman

10:38 pm on Apr 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Calling the form's submit() method will bypass the onsubmit event handler. ajaxForm likely attaches an event listener to the onsubmit event, which would be triggered by a regular form submit button. Here's a quick test just to show that the onsubmit event handler isn't triggered when you call the submit method directly:

<html>
<head>
</head>
<body>
<form action="#" onsubmit="return foo();">
<input type="button" value="click me" onclick="this.form.submit();">
</form>
<script>
function foo() {
alert('foo');
return false;
}
</script>
</body>
</html>


To get around this issue, you may be able to use the submit [api.jquery.com] jQuery method instead. As in:


onKeyUp="if(this.value.length>4)$('#htmlForm').submit()"

asg333

3:27 pm on Apr 16, 2011 (gmt 0)

10+ Year Member



Fotiman - thank you so much! That worked perfect!