我想从主页向子域页面提交表单。
这是我的代码

html-主页(主域)

<table>
    <tr>
        <td>Name</td>
        <td><input type="text" name="txtName" id="txtName" /></td>
    </tr>
    <tr>
        <td>Email</td>
        <td><input type="text" name="txtEmail" id="txtEmail" /></td>
    </tr>
    <tr>
        <td><input name="btnSubmit" id="btnSubmit"  value="Submit" type="button"></td>
    </tr>
</table>

<form id="getDetails" method="post" action="http://customers.liyyas.com/">
    <input type="hidden" name="act" value="Users" />
    <input type="hidden" name="hdnName" id="hdnName" />
    <input type="hidden" name="hdnEmail" id="hdnEmail" />
</form>


脚本

<script type="text/javascript">
$(document).ready(function(){
    $('#btnSubmit').click(function()
        {
          alert("hai");
            document.getElementById("getDetails").submit();
            document.getElementById("hdnName").value = $('#txtName').val();
            document.getElementById("hdnEmail").value = $('#txtEmail').val();
     });
    });
 </script>


子域页面-user.php

<?php
$act = formatstring($_POST['act']);
switch($act)
{
case "Users":
        $Name=$_POST['hdnName'];
        $Email=$_POST['hdnEmail'];
        print($Name);
        exit();
}
?>


在子域中,我正在打印值,但未打印

是否可以从母域到子域提交表单?

最佳答案

您需要从以下位置更改form元素的action属性

http://customers.liyyas.com/




http://customers.liyyas.com/customers.php


我还假设您知道根据此代码

$('#btnSubmit').click(function()
    {
      alert("hai");
        document.getElementById("getDetails").submit();
        document.getElementById("hdnName").value = $('#txtName').val();
        document.getElementById("hdnEmail").value = $('#txtEmail').val();
 });


表单将在更改hdnName和hdnEmail的值之前提交?对于您来说,通过切换几行来快速切换解决方案也可能是一个错误。这可能是一个错误的原因是,当您提交表单时,页面将被重新加载,这意味着用户将永远无法看到通过JavaScript插入的新值。

解决方法可能是

$('#btnSubmit').click(function()
    {
      alert("hai");
        document.getElementById("hdnName").value = $('#txtName').val();
        document.getElementById("hdnEmail").value = $('#txtEmail').val();
        document.getElementById("getDetails").submit();
 });

10-04 22:50
查看更多