简体   繁体   中英

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. Ex. 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

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

<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'); 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:

<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

On form 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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