gammaman Posted May 12, 2008 Share Posted May 12, 2008 I want to have the form partially filled out when it returns back to the first page "a.php". This included the drop down box, I want it to have the value that the user selected before they submitted the form. Here is the first page "a.php" <?php session_start(); $f1=$_SESSION['f1']; $f2=$_SESSION['f2']; $f3=$_SESSION['newclass']; mysql_connect("localhost","fierm","13183"); mysql_select_db(fierm); $result=mysql_query("select * from Course"); echo '<form action="b.php" method = "post">'; echo '<input type= "text" name="f1">'."<br/>"; echo '<input type = "text" name="f2">'."<br/>"; echo '<select name = "newclass" id="item">'."<br/>"; while ($row=mysql_fetch_array($result)) { $option=$row['courseName']; echo "<option>$option</option>"; } echo "</select>"; echo "<br/>"; echo '<input type = "submit" value="submit">'; ?> Here is where it is sent to "b.php" If the value of the second box is not "student" as the code suggests, I want it to return to "a.php" and remember the value of the fist box and the value from the drop down <?php session_start(); $p1=$_POST['f1']; $p2=$_POST['f2']; $p3=$_POST['newclass']; $_SESSION['newclass']=$p3; $_SESSION['f1']=$p1; $_SESSION['f2']=$p2; if($p2=='student'){ echo "it got here"; }else{ header("location: a.php"); } ?> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 12, 2008 Share Posted May 12, 2008 If you follow the links in my signature, I have written some basic form tutorials that explain how to create a single script to handle the display, validation, and processing of a form on a single script. You can adapt them to your situation by doing the following: The form in a.php should submit to a.php so that the script that renders the form can also validate it. If the form is invalid you can re-display the form with the previous values set. Change the processing portion of a.php to set session variables and then redirect to b.php. b.php can then do whatever it normally does. And if b.php needs any of the values from a.php they are in the session. Quote Link to comment Share on other sites More sharing options...
gammaman Posted May 12, 2008 Author Share Posted May 12, 2008 Sorry but this does not really help me. I did not see anything in your examples where you select a field from a database, store it into a combo box and then have it retain that value on reload. Quote Link to comment Share on other sites More sharing options...
Skittalz Posted May 12, 2008 Share Posted May 12, 2008 perhaps making a.php into a function would work where you can just pass the values Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 12, 2008 Share Posted May 12, 2008 Does this help clarify: <?php // Select items from the database $sql = " SELECT `value_column` AS `value`, `display_column` AS `display` FROM `your_table` WHERE 1 ORDER BY `display` "; // Get the items $item_query = mysql_query($sql) or die('Error: ' . mysql_error()); /** * GETTING THE DEFAULT SELECTED ITEM */ $default_item = null; // initially no item // If you are here from another page in your site, you have to pass the // default either via $_GET or $_SESSION; use the appropriate line $default_item = isset($_GET['default_item']) ? $_GET['default_item'] : $default_item; // $default_item = isset($_SESSION['default_item']) ? $_SESSION['default_item'] // : $default_item; // However, if 'theName' is set within $_POST, then you are here from // a form submittal. In this case you should use the $_POST value because // it is the one the user selected $default_item = isset($_POST['theName']) ? $_POST['theName'] : $default_item; /** * END - GETTING THE DEFAULT SELECTED ITEM */ // create a dropdown echo '<select name="theName">'; // open tag while( $item = mysql_fetch_assoc($item_query) ) { echo sprintf('<option value="%s"%s>%s</option>', $item['value'], $item['value'] == $default_item ? ' selected' : '', $item['display'] ); } echo '</select>'; // close tag ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 12, 2008 Share Posted May 12, 2008 Have a.php handle itself with like if (isset($_POST)) { }, and echo the values in $_POST as the value="" attribute of the form. <input type="text" value="<?php echo $_POST['whatever'] ?>" name="whatever" /> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 12, 2008 Share Posted May 12, 2008 DarkWater, that is what I've already recommended he do. Quote Link to comment Share on other sites More sharing options...
gammaman Posted May 12, 2008 Author Share Posted May 12, 2008 Yeah, that helps a little bit. I will let you know if I get it working tommorow. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 12, 2008 Share Posted May 12, 2008 I know, roopurt, but I guess he didn't understand you. "Sorry but this does not really help me. I did not see anything in your examples where you select a field from a database, store it into a combo box and then have it retain that value on reload." =P Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.