简体   繁体   中英

How to submit two forms at a time and store first form submit output value as 2nd form value?

Using the below form 1, i'm generating a goo.gl short url inline in #shortUrlInfo area. I want to use the generated short url in 2nd form to save it in wordpress custom field.

Form 1:

<form method="post" action="">
<input style="display:none;" type='text' id='longUrl' value='<?php echo get_post_meta( get_the_ID(), 'longurl', true ); ?>' /> 
<input name="shorturlinput" type="button" id="shortIt" value="Short It" /></form> 

<div class="info" id="shortUrlInfo"></div>

Form 2:

<?php
global $post;
if( isset($_POST['submit_meta']) ) {
if( ! empty($_POST['change_meta']) ) {
   update_post_meta($post->ID, 'shorturl', $_POST['change_meta']);
}}
?>


<form method="post" action="">
<input type="text" name="change_meta" value="" />
<input type="submit" name="submit_meta" value="Submit" />
</form>

How can i submit the first form and pass the generated short url to 2nd form and submit that short url value in custom field with one click?

Or atleast, how can we display the generated short url in 2nd form input, which if we click on submit button, it should save in database.

It's my first question here & I've tried my best to find answer before posting here with no luck.

Well its all ajax . you need to post your data with ajax function to the controller which save the shortened url in data base , how ? like this :

give your second form + the first input of your second form 2 ids

$("#shortIt").click(function(){
  var longUrl = $('#longUrl').val()
  $.post("ur_url.php",
  {
      "longUrl": longUrl // here the url will be sent to your server and server communicates 
      // with goo.gl url shorter by its api and respond you with a variable called data
  },
  function(data, status){ // your server send here the shorturl and this function calls it
      //also this function will be run after the server respond is completed
      $('#inputID').val(data) // data will be set inside the input value
      document.getElementById("#SecondForm").submit(); // your second form submits and your
      // server needs to get the data and save it in your data base
  });
});

all will be done with a single click .

also you can do many things when the process is going on . but i hope i gave you some clue :P

Try this in HTML:

<form (return to script)>
    <input 1>
</form>
    <input 2>

Then in script:

get input 1 and input 2

Next example code shows how to achieve what I believe you want (with one click!). Explanation first: one form (file #1) sends a text to a script (file #2), this script redirects to another form (file #3), this form gets the original text and automatically resend it to another script (file #4) that inserts it in database, next image explains it better:

在此处输入图片说明

As you can see, the text in the first form becomes a value for the second form.

Now the code. In order to test next code, you will have to create four text files and give them the given names (if you change the filenames, you will have to change the "action" property in the forms), copy-paste my code in your files , then, open you browser and run only the first file like this) :

http://localhost/form_sequence_1.php

These are the files:

form_sequence_1.php

<html>
  <body>
    FORM 1
    <br/>
    <form method="post" action="form_sequence_2.php">
      Enter a text
      <input type="text" name="my_text" />
      <br/>
      <input type="submit" value="Send text" />
    </form>
  </body>
</html>

form_sequence_2.php

<?php
session_start();
$_SESSION[ "my_text" ] = $_POST[ "my_text" ];
header( "Location: form_sequence_3.php" );
?>

form_sequence_3.php

<?php
session_start();
?>
<html>
  <head>
    <script type="text/javascript">
function autosendform () {
setTimeout( "sendform()","3000" );
}
function sendform () {
document.getElementById( "my_form" ).submit();
}
    </script>
  </head>
  <body onload="autosendform();">
    FORM 2
    <br/>
    <form method="post" action="form_sequence_4.php" id="my_form">
      Text entered in previous form
      <input type="text" name="my_text" value="<?php echo $_SESSION[ "my_text" ]; ?>"/>
      <br/>
      <input type="submit" value="AUTOSENDING FORM IN 3 SECONDS" />
    </form>
  </body>
</html>

form_sequence_4.php

<?php
session_start();
echo $_SESSION[ "my_text" ] . " succesfully inserted into database.";
?>

More explanations:

  • Value in form 1 sends the text to form 2.
  • In the middle of form 1 and form 2 we need a script to capture the value.
  • Form 2 automatically resends the value thanks to a timer in JavaScript.
  • The last script gets the value and insert it in database (not for real).

The advantage of this approach is that the scripts allow you to do many things with the value or values, like validating or conversion.

Next is your code with my JavaScript timer code adapted :

<?php
global $post;
if ( isset($_POST['submit_meta']) )
   if ( ! empty($_POST['change_meta']) )
      update_post_meta( $post->ID, 'shorturl',$_POST['change_meta'] );
?>

<html>
  <head>
    <script type="text/javascript">
function autosendform () {
setTimeout( "sendform()","3000" );
}
function sendform () {
document.getElementById( "my_form" ).submit();
}
    </script>
  </head>
  <body onload="autosendform();">

<form method="post" action="" id="my_form">
  <input type="text" name="change_meta" value="" />
  <input type="submit" name="submit_meta" value="Submit" />
</form>

  </body>
</html>

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