javascript - How to submit two forms at a time and store first form submit output value as 2nd form value? -
using below form 1, i'm generating goo.gl short url inline in #shorturlinfo area. want use generated short url in 2nd form save 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 submit first form , pass generated short url 2nd form , submit short url value in custom field 1 click?
or atleast, how can display generated short url in 2nd form input, if click on submit button, should save in database.
it's first question here & i've tried best find answer before posting here no luck.
next example code shows how achieve believe want (with 1 click!). explanation first: 1 form (file #1) sends text script (file #2), script redirects form (file #3), form gets original text , automatically resend script (file #4) inserts in database, next image explains better:
as can see, text in first form becomes value second form.
now code. in order test next code, have create 4 text files , give them given names (if change filenames, have change "action" property in forms), copy-paste code in files, then, open browser , run first file this) :
http://localhost/form_sequence_1.php
these files:
form_sequence_1.php
<html> <body> form 1 <br/> <form method="post" action="form_sequence_2.php"> enter 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 database."; ?>
more explanations:
- value in form 1 sends text form 2.
- in middle of form 1 , form 2 need script capture value.
- form 2 automatically resends value timer in javascript.
- the last script gets value , insert in database (not real).
the advantage of approach scripts allow many things value or values, validating or conversion.
next code 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>
Comments
Post a Comment