简体   繁体   English

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

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

Hi i need to implement something like, i have a list of values in the ul and li tag, in one page, and a button, so when i click on it on the next page all these ul, li values should get populated into a select option. 嗨,我需要实现类似的内容,我在ul和li标记中有一个值列表,在一个页面中,还有一个按钮,因此当我在下一页上单击所有这些ul时,li值应填充到一个选择选项。 Ex. 例如 form1.php 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>

once i submit the form1.php, then on the next page.php it should be somethis like this 一旦我提交了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" />

If the user is not meant to interact with it, you can send the location data with the rest of the form values on submit via hidden input fields. 如果不希望与用户进行交互,则可以通过隐藏的输入字段将位置数据与其余表单值一起发送。 A js/jquery solution is not necessary for this task. 此任务不需要js / jquery解决方案。 You can add as many hidden inputs as you like. 您可以根据需要添加任意数量的隐藏输入。 You can use php to make it dynamic, say, if you have an array of locations to draw from. 您可以使用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>

At the next page, $_POST['location'] = array('New Delhi','Mumbai'); 在下一页, $_POST['location'] = array('New Delhi','Mumbai'); So you can create the form with the location select like this: 因此,您可以使用以下位置选择创建表单:

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

well I guess you should use javascript (let me help myself with jquery) first because ul and li elements are not part of a form tag... but: 好吧,我想您应该首先使用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>

You can retrieve this in next page with $_POST 您可以在下一页使用$_POST检索

On form 1 在表格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>

On form 2 在表格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