繁体   English   中英

在php中将值从一页传递到另一页

[英]Passing value from one page to another page in php

嗨,我需要实现类似的内容,我在ul和li标记中有一个值列表,在一个页面中,还有一个按钮,因此当我在下一页上单击所有这些ul时,li值应填充到一个选择选项。 例如 form1.php

<form method="POST" action="next page.php">
<input type="text" name="designation" value="Manager" />
<label>Location:</label>
<ul>
<li>New Delhi</li>
<li>Mumbai</li>
</ul>
<input type="submit" value="submit" />
</form>

一旦我提交了form1.php,则在next.php中应该是这样的

<form method="POST" action="insert.php">
<input type="text" name="designation" value="<?php echo $_POST['designation'] ?>"

<select> all the values from the previos page of ul,li should get populated over here in drop down.</select>
//some more fields will be there in this form.
<input type="text" name="DOB" />
<input type="text" name="phone_no" />

<input type="submit" name="Submit" value="Submit" />

如果不希望与用户进行交互,则可以通过隐藏的输入字段将位置数据与其余表单值一起发送。 此任务不需要js / jquery解决方案。 您可以根据需要添加任意数量的隐藏输入。 您可以使用php使其具有动态性,例如,如果您要绘制一系列位置。

<form method="POST" action="next page.php">
    <input type="text" name="designation" value="Manager" />
    <label>Location:</label>
    <ul>
        <li>New Delhi</li>
        <li>Mumbai</li>
    </ul>
    <input type="hidden" name="location[]" value="New Delhi">
    <input type="hidden" name="location[]" value="Mumbai">
    <input type="submit" value="Submit" />
</form>

在下一页, $_POST['location'] = array('New Delhi','Mumbai'); 因此,您可以使用以下位置选择创建表单:

<?php
if(isset($_POST['location'])){  // check that this file is receiving POSTed data
    echo "<form method=\"POST\" action=\"insert.php\">";
        echo "<input type=\"text\" name=\"designation\" value=\"{$_POST['designation']}\" />";
        echo "<select name=\"location\">";  // be sure to include name attribute
            foreach($_POST['location'] as $loc){
                echo "<option>$loc</option>";
            } 
        echo "</select>";
        //some more fields will be there in this form.
        echo "<input type=\"text\" name=\"DOB\" />";
        echo "<input type=\"text\" name=\"phone_no\" />";
        echo "<input type=\"submit\" value=\"Submit\" />";  // no name is necessary
    echo "</form>";
}
?>

好吧,我想您应该首先使用javascript(让我来帮助jquery),因为ul和li元素不是表单标签的一部分...但是:

<form id="form_1" method="POST" action="next_page.php">
    <input type="text" name="test">
    <ul>
        <li>Value 1</li>
        <li>Value 2</li>
        <li>Value 3</li>
    </ul>
    <button type="submit">Do it</button>
</form>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
$('#form_1').on('submit',function(e) {
    var ul_li = $(this).find('li');
    ul_li.each(function(index){
        $('<input />').attr('type', 'hidden')
            .attr('name', 'li_'+index)
            .attr('value', $(this).text())
            .appendTo('#form_1');
    }); 
    return true;
});
</script>

您可以在下一页使用$_POST检索

在表格1上

    <form method="POST" action="next page.php">
          <input type="text" name="designation" value="Manager" />
          <label>Location:</label>
          <ul>
              <li><input type="hidden" name="city[]" value="New Delhi"></li>
              <li><input type="hidden" name="city[]" value="Mumbai"</li>
         </ul>
         <input type="submit" value="submit" />
   </form>

在表格2上

     <form method="POST" action="insert.php">
           <input type="text" name="designation" value="<?php echo 
            $_POST['designation'] ?>" >

           <select> 
               <?php foreach($_POST['city'] as $city){ ?>
                     <option><?php echo $city?></option>
               <?php } ?>
           </select>
           //some more fields will be there in this form.
           <input type="text" name="DOB" />
           <input type="text" name="phone_no" />

           <input type="submit" name="Submit" value="Submit" />
    </form>

暂无
暂无

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

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