提交通常新创建的ajax表单


submit normally newly created ajax form

我想提交我的表单。在我的表单中,我使用第一个按钮通过ajax创建一个文本框,然后使用第二个按钮正常提交表单。当我想使用$_POST获取新创建的文本框的值时,它会出错。如何在提交时获得ajax创建按钮的值。我的php代码是:

<?php`enter code here`
 session_start();
  ob_start();
require_once "config.php";
if ($_SERVER['REQUEST_METHOD']=="POST")
{
if (isset($_POST['subtest']))
    {
        print $_GET['tt'];
    }
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="jquery-ui-1.8.21.custom/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.21.custom/js/jquery-ui-1.8.21.custom.min.js">                     </script>
<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery("#subt").click(function(){
        jQuery("#divload").show();
        jQuery.ajax({url:"adp.php", type:"post", success:function(result){
        jQuery("#disp").html(result);
        jQuery("#divload").hide();
      }});
    return false;
    });
});
</script>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#subtest").click( function() {
alert(jQuery("#tt").val());
});
});
</script>
</head>

<body id="#bdy">
<form id="FrmMain" method="post" action="">
<div id="Shd" style="font: 10%;  margin: 50px; background-repeat:repeat-y; padding-       left:120px;" >
    <input type="text" id="txtFrom" name="txtFrom" />
    <input type="text" id="txtUpto" name="txtUpto" />
    <input type="text" id="txtOpt" name="txtOpt" />
    <input type="submit" id="subt" name="subt" />
    <input type="submit" id="subtest" name="subtest" />
    <div id="RuBox" style="font-weight:bold;"><input type="checkbox" id="chkaccept"   name="chkaccept"  />&nbsp;&nbsp;I accept &nbsp;<a href="terms.html" style="color:#009;">terms and    conditions</a>.</div>
    </div>
   </form>
  <div style="color:#F00; font-size:18px; display:none;" id="divload">Please wait loaidng...   </div> 
   <div id="disp"></div>
</body>
</html>

我的adp.php文件的代码:

<?php 
sleep(5);
?>
<div style="color:#30F; font-size:36px;">
 Application testing............
 <input type="text" id="tt" name="tt" />
 </div>

我没有在临时表单中获得名为"tt"的文本框的值

感谢

您在这里几乎没有发布任何内容:

jQuery(document).ready(function(){
    jQuery("#subt").click(function(){
        jQuery("#divload").show();
        jQuery.ajax({url:"adp.php", type:"post", success:function(result){
        jQuery("#disp").html(result);
        jQuery("#divload").hide();
      }});
    return false;
    });
});

所以我假设您只想动态生成一个文本字段。您可以在不使用ajax的情况下完成此操作:

var form = $('#FrmMain');
$('<input>').attr({'type' : 'text', 'id' : 'tt', 'name' : 'tt'}).appendTo(form);

另外,当表单方法为POST 时,您还试图打印出$_GET['tt']

if (isset($_POST['subtest']))
{
    print $_GET['tt'];
}

这也应该是:

echo $_POST['tt'];