submit form with link while checking values
I have an HTML form defined as
<form name="writeYourAd" id="writeYourAd" method="post" action="post.php?action=preview" onsubmit="return checkContentForm(this);" enctype="multipart/form-data">
If I use a , it works fine. But if instead I use this:
<a href="#" class="mmh_orngebtn mmh_grybtn" onclick="javascript:document.writeYourAd.submit();"><span>开发者_运维百科continue</span></a>
It submits the form but does not run the checkContentForm function (or at least it lets me through anyways).
Why? What can I do?
I think that you need to point the onclick handler to your checkContentForm function.
Or write your own submit function that includes calling checkContentForm and then submits.
try add the function to the submit button as an onclick event
You should call form.onsubmit()
manually, then remove it (in case if some browsers will call it on submit()
call, because it's what they should do by spec, but most of them don't), then call form.submit()
Also take a look at this thread
it will go to your post.php page before it will perform the onsubmit function
try staying on the same page and using include in you post.php
like
if ($_POST[submit]){
include "post.php";
echo "<script>return checkContentForm(document.writeYourAd);</script>";
}
or you can remove the action, then send your post before or after the checkContentForm() function using ajax
<form name="writeYourAd" id="writeYourAd" method="post" action="" onsubmit="return SendQuery(this);" enctype="multipart/form-data">
JavaScript:
function SendQuery(myForm)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="post.php";
url=url+"?action=preview";
thisForm = myForm;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged(){
if (xmlhttp.readyState==4){
if (xmlhttp.status == 200){
return checkContentForm(thisForm);
}
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
Just Check for some errors.
精彩评论