簡體   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