繁体   English   中英

如何一次提交两个表单并将第一表单提交的输出值存储为第二表单值?

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

使用下面的表格1,我在#shortUrlInfo区域中生成了goo.gl短网址内联。 我想以第二种形式使用生成的短网址将其保存在wordpress自定义字段中。

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

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

我如何提交第一份表格并将生成的简短网址传递给第二份表格,然后单击一下即可在自定义字段中提交该简短网址值?

或者至少,我们如何在第二个表单输入中显示生成的短网址,如果单击提交按钮,则应将其保存在数据库中。

这是我的第一个问题,在没有运气之前,我已尽力找到答案。

好吧,所有的ajax。 您需要使用ajax函数将数据发布到控制器,该控制器将缩短的url保存在数据库中,怎么办? 像这样 :

提供您的第二个表单+第二个表单的第一个输入2个ID

$("#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
  });
});

只需单击一下,即可完成所有操作。

当过程进行时你也可以做很多事情。 但我希望我给你一些线索:P

尝试使用HTML:

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

然后在脚本中:

get input 1 and input 2

下一个示例代码显示了如何实现我认为想要的功能(单击一下即可!)。 首先说明:一个表单(文件#1)将文本发送到脚本(文件#2),此脚本重定向到另一表单(文件#3),此表单获取原始文本并自动将其重新发送到另一个脚本(文件# 4)将其插入数据库,下图更好地说明了它:

在此处输入图片说明

如您所见,第一种形式的文本成为第二种形式的值。

现在的代码。 为了测试下一个代码,您将必须创建四个文本文件并为其指定名称(如果更改文件名,则必须更改表单中的“ action”属性),然后将我的代码复制并粘贴到文件 ,然后打开浏览器并仅运行第一个文件,如下所示:

http://localhost/form_sequence_1.php

这些是文件:

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.";
?>

更多说明:

  • 表格1中的值将文本发送到表格2中。
  • 在表格1和表格2的中间,我们需要一个脚本来捕获值。
  • 表格2通过JavaScript中的计时器自动重新发送该值。
  • 最后一个脚本获取该值并将其插入数据库(不是真正的)。

这种方法的优点是脚本允许您使用一个或多个值执行许多操作,例如验证或转换。

接下来是经过修改的我的JavaScript计时器代码的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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