Submit an ajax form created by an ajax call

Three pages Test5.php


    <html>
    <head>
        <script>
        function loadXMLDoc()
        {
            var xmlhttp;
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }

            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                }
            }

            fst = document.form1.first.value ;
            sec = document.form1.second.value ;
            params = "first=" + encodeURI(fst) + "&second=" + encodeURI(sec) ;
            xmlhttp.open("POST","testproc2.php",true) ;
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded") ;
            xmlhttp.setRequestHeader("Content-length", params.length);
            xmlhttp.setRequestHeader("Connection", "close");
            xmlhttp.send(params);
       }

        function changeInnerDiv()
        {
            alert("In changeInnerDiv") ;
            var xmlhttp ;
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }

            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                }
            }

            fst = document.form1.first.value ;
            sec = document.form1.second.value ;
            params = "first=" + encodeURI(fst) + "&second=" + encodeURI(sec) ;
            xmlhttp.open("POST","testproc3.php",true) ;
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded") ;
            xmlhttp.setRequestHeader("Content-length", params.length);
            xmlhttp.setRequestHeader("Connection", "close");
            xmlhttp.send(params);
        }

      
    </head>
    <body>
    This text should remain static 
<hr> <div id="myDiv"> <?php echo "<form name="form1"> " ; echo "First:      <input type="text" name="first">
" ; echo "Second: <input type="text" name="second">
" ; echo " </form> " ; ?> <button type="button" onclick="loadXMLDoc()">Change Content</button> </div> </body> </html>

Pressing the button calls page testproc2.php as expected, it does echo the two values from the form

    <html>
    <head>
    </head>
    <body>

<?php
   //error_reporting(E_ALL) ;
   //echo "&lt;br> From Textproc.php" ;
   $first = $_POST['first'] ;
   $second = $_POST['second'] ;

   //echo "<div id="myInnerDiv"> "  ;
   echo "In testProc2, " . &#36;first . " was first, and " . &#36;second . " was second &lt;br> " ;
   echo "Now another form from testproc2&lt;br> " ;

   echo "&lt;form name="form2"> " ;
   echo "First:  &nbsp; &lt;input type="text" name="uno"> &lt;br> " ;
   echo "Second:  &lt;input type="text" name="dose"> &lt;br> " ;
   echo " &lt;/form> " ;
   echo "&lt;button type="button" onclick="changeInnerDiv">Change The Content&lt;/button> " ;
?>
&lt;/body>
&lt;/html>

But nothing happens when I press the submit on testproc2.php. I've tried putting a div in testproc2.php, tried moving the java script into testproc2.php. Submit does nothing. Is it possible to have an ajax call return a form that can be submitted? I'd rather not, but do I need jquery? Thanks Brian

链接地址: http://www.djcxy.com/p/22330.html

上一篇: Spring Rest api和Spring基本安全

下一篇: 提交由ajax调用创建的ajax表单