开发者

Submitting a Form Within a Form

开发者 https://www.devze.com 2023-03-07 01:58 出处:网络
When I click \"Submit Second,\" the page goes to first.html. I want it to go to second.html index.html

When I click "Submit Second," the page goes to first.html. I want it to go to second.html

index.html

<body onload="secondForm();">
<script type="text/javascript">
function ajaxRequest(){
var activexmode开发者_如何学JAVAs=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] // activeX versions in IE
if (window.ActiveXObject){ // test support for ActiveXObject in IE
    for (var i=0; i<activexmodes.length; i++){
        try{
            return new ActiveXObject(activexmodes[i])
        }
        catch(e){
            // suppress
        }
    }
}
else if (window.XMLHttpRequest) // mozilla,safari,chrome,opera
    return new XMLHttpRequest()
else
    return false
}
function secondForm(id) {
var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatechange=function() {
    if (mygetrequest.readyState==4) {
        if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1) {
            document.getElementById("secondForm").innerHTML=mygetrequest.responseText
        }
    }
}
mygetrequest.open("POST", "secondForm.html, true)
mygetrequest.send(null)
}
</script>

<form method="POST" action="first.html" id="firstForm">
<span id="secondForm"></span>
<input type="submit" value="Submit First">
</form>

secondForm.html

<form method="POST" action="second.html" id="secondForm">
<input type="submit" value="Submit Second">
</form>


You can't nest forms. You will have to change the action url for the first form.


You have a form within a form, the outer form gets processed on submit.

Also I would suggest using a library for your ajax requests, i use jQuery:

$('#secondForm').load('secondForm.html');

Change you HTML so this would work:

<form method="POST" action="first.html" id="firstForm">
   <input type="submit" value="Submit First">
</form>

<span id="secondForm"></span>

secondForm.html

<form method="POST" action="second.html" id="secondFormID"> <-- use a different ID
   <input type="submit" value="Submit Second">
</form>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号