jquery - Form ajax call to PHP page not working -
all of other questions similar pretty specific asker's particular situation thought i'd inquire mine.
i'm trying send form it's action page using ajax reason page still refreshes when form submitted. ideas why?
form:
<form name='comment_form' id='comment_form' action='leave_comments.php' onsubmit='return validateform()'> <input type='text' name='user_comment' class='user_comment' placeholder='leave comment...'> <input type='hidden' name='hidden_folder' value='$random_directory'> <input type='hidden' name='hidden_title' value='$title'> <input type='hidden' name='hidden_name' value='$image_name'> <input type='hidden' name='hidden_image' value='$image_info'> <input type='hidden' name='hidden_user' value='$posted_by'> <input type='submit' class='leave_comment button' name='leave_comment button' value='comment'> </form>
the js validation:
<script> function validateform() { var o=document.forms["leave_comments_form"]["user_comment"].value; if (o==null || o=="") { alert("error 2591 : whoops, looks forgot leave comment!"); homecoming false; } } </script>
the js ajax:
<script> $("#comment_form").submit(function(e) { e.preventdefault(); if (validateform()) { var form = jquery("#comment_form"); $.ajax({ type: "get", url: form.attr("action"), data: form.serialize(), success: function() {} }); } }); </script>
i'm trying form submit without page refresh.
validateform()
needs homecoming true
when validation successful:
function validateform() { var o=document.forms["leave_comments_form"]["user_comment"].value; if (o==null || o=="") { alert("error 2591 : whoops, looks forgot leave comment!"); homecoming false; } homecoming true; }
this isn't needed onsubmit
attribute, needed when write if (validateform())
, because undefined
falsy.
jquery form-submit
No comments:
Post a Comment