简体   繁体   中英

php get data from another form using a different form

Is it possible to get data from another using a different form?
I don't want to use one form

 <?php
     echo $_POST['2'];
     ?>
   <html>

   <head>
   </head>

   <body>
     <form method="post">
       <input type="submit" name="submit" />
     </form>
     <form method="post">
       <input type="text" name="2" />
     </form>
   </body>

   </html>

No, that's not possible because browsers will only ever submit one form at a time (the one containing the clicked submit button, typically).

They can't possibly submit multiple forms at once because each form has its own action and method attribute which determines the request to send.

As @peter said, you can submit only one form at a time. But there are some workarounds for your needs.

Method 1

Post your form to a php script(say form_1_action.php ) and then store the form input in a Session variable.

$_SESSION['form_data_1'] = $_POST;

Then you will be able to access it in different pages. Like,

$_SESSION['form_data_1']['field_name']

Method 2

Post your form to a php script(say form_1_action.php ) and then store the form input in a PHP variable.

$formData1 = $_POST;

Then you can use the data from the first form in the second form (the second form should be on the same file form_1_action.php ) like

<input name="name" value="{$formData1['field_name']"}>

You should pass the data from the first form in a hidden field on the second form if you need it on the form_2_action.php .

Method 3

Use Javascript to accomplish your requirements in a more userfriendly way.

try using jquery to Prevent the other form from submiting and try updating the value using event listening of the first form and update that input. 
    $( '#Submit' ).click( function ( event ) {
            event.preventDefault();
var value = <?= $postedValue ?>;
$('input[name="input_name/2"]').val(value);
}

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