Jump to content

help retaining form fields on page reload


gammaman

Recommended Posts

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

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.