简体   繁体   中英

How to submit a form from home domain to sub domain?

I want to submit a Form from home page to sub domain page. Here is my code

html - home page (main domain)

<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

<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>

Sub domain page - user.php

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

In sub domain I am printing the value but its not printing

Is it possible to submit a form from home domain to sub domain?

You need to change the action attribute of the form element from

http://customers.liyyas.com/

to

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

I also assume you know that according to this code

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

The form will submit BEFORE the values of hdnName and hdnEmail are changed? That may also be a bug for you to quickly switch solve by switching around a few lines. The reason this may be a bug is that when your form submits the page will be reloaded meaning the user will never get to see the new values inserted via JavaScript.

The fix could be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM