简体   繁体   English

如何将值从一种形式传递到另一种形式?

[英]How do I Pass Values from One Form to Another?

I have this form: 我有这个表格:

<form name="summaryform">
      <select name="category" id="category" style="width:500px;">
        <option value="">Choose category...</option>
        <option value="ActionScript">ActionScript</option>
        <option value="AppleScript">AppleScript</option>
        <option value="Asp">Asp</option>
        <option value="BASIC">BASIC</option>
        <option value="C">C</option>
        <option value="C++">C++</option>
        <option value="Clojure">Clojure</option>
        <option value="COBOL">COBOL</option>
        <option value="ColdFusion">ColdFusion</option>
        <option value="Erlang">Erlang</option>
        <option value="Fortran">Fortran</option>
        <option value="Groovy">Groovy</option>
      </select><br>
      <select name="subcategory" id="subcategory" style="width:500px;">
        <option value="">Choose sub category...</option>
        <option value="Haskell">Haskell</option>
        <option value="Java">Java</option>
        <option value="JavaScript">JavaScript</option>
        <option value="Lisp">Lisp</option>
        <option value="Perl">Perl</option>
        <option value="PHP">PHP</option>
        <option value="Python">Python</option>
        <option value="Ruby">Ruby</option>
        <option value="Scala">Scala</option>
        <option value="Scheme">Scheme</option>
      </select><br>
  <input type="text" name="comments" id="comments" value="  Enter request comments..." onfocus="clearText(this)" onblur="restoreText(this)" style="width:493px;color:#999;font-size:9pt;height:20px;">

On the form above, I have two dropdowns (name="categoryId" and "subcategoryid") and one textboxt (name="comments") 在上面的表格中,我有两个下拉菜单(名称=“ categoryId”和“ subcategoryid”)和一个文本框(名称=“ comments”)

How do I pass the values from this form to another form? 如何将值从此表单传递到另一表单?

Let's say below is the form I want to pass the values to: 下面说的是我要将值传递给的形式:

  <div>
    <p>
      <form name="summaryform" method='POST' action='process.php'>
      <div style="font-size:12pt;">
       value from one dropdown
       value from the other dropdown
       value from textbox
      </form>
    </p>
</div>

A value can be passed between forms using the POST and GET methods. 可以使用POST和GET方法在表单之间传递值。 The first form (Which is sending the values) does not have this defined, so modify the first form to this - 第一个表单(正在发送值的表单)没有定义,因此请将第一个表单修改为此-

<form name="summaryform" method='POST' action='process.php'>

Then, in process.php, put the following php code - 然后,在process.php中,输入以下php代码-

$dropdown_first = $_POST['category'];
$dropdown_second= $_POST['subcategory'];
$comment = $_POST['comments'];

After that, you can just use the variables as you see fit! 之后,您可以根据需要使用变量!

jQuery Method jQuery方法

If the forms are on the same page, you can use jQuery to get the values and write to the div: 如果表单在同一页面上,则可以使用jQuery获取值并将其写入div:

var content = $('#category').val()+'<br>'+$('#subcategory').val()+'<br>'+$('#comments').val();
$('#div-to-write-to').html(content);

PHP Method PHP方法

If the forms are on different pages, you will need to use PHP and the $_POST[] variable to pass the values you are looking for. 如果表单在不同的页面上,则需要使用PHP和$ _POST []变量来传递您要查找的值。

NOTE: for this method to work the page being POSTed to MUST be PHP. 注意:要使此方法正常工作,被发布到的页面必须是PHP。 You will also need to add an action and method in the first form to POST to the second form. 您还需要将第一种形式的操作和方法添加到POST到第二种形式。

This would be the code in the form on the page being POSTed to: 这将是张贴到页面上的表单中的代码:

<?php echo $_POST['category'].'<br>'.$_POST['subcategory'].'<br>'.$_POST['comments']; ?>
  1. If you want just print the selected values from first form @zsaa14's answer is good 如果您只想打印第一种形式的选定值,则@ zsaa14的答案很好
  2. If you want to print whole select list, autoselected your value, heres a code . 如果你想打印整个选择列表, 自动选择了你的价值, 继承人的代码

foreach($categories as $category) {

$selected = $category['name'] === $_POST['category'] ? 'selected="selected"' : '';

echo " <option value="{$category['name']}" {$selected}>{$category['name']}</option>" ;

}

NOTE: the variable names and keys are for example, use Yours. 注意:变量名称和键例如是使用Yours。 This is only fragment of code. 这只是代码片段。

您可以使用发布数据字段将值发送到下一个表单

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

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