PDA

View Full Version : Sticky Select Form Fields


presley2
12-03-2005, 02:53 AM
Hi,
I have a form, with some error checking, that works fine after "submit" inserting into DB ---but I have a little problem, I cannot seem to make my "select" fields sticky --I have tried several different solutions and nothing works. The solution below works for errors (not selecting from the pulldown) and for inserting into DB ...but does not work if error checking stops the porcessing ...I want it to remain on the last selection for user convenience ...but it doesn't ...help!


<select name="session">
<option value="" <? echo ($session=='') ? "" : ""; ?>>SELECT SESSION</option>
<option value="1" <? echo ($session=='1') ? "selected=" : ""; ?>>SESSION 1</option>></select>

<select name="employment_status">
<option value="" <? echo ($session=='') ? "selected" : ""; ?>>SELECT STATUS</option>
<option value="1" <? echo ($employment_status=='1') ? "selected" : ""; ?>>STATUS 1</option>></select>

Element
12-03-2005, 03:01 AM
You can put the users last selection name into their DB and then add at the end and than:



echo "<option value=\"" . $name . "\" " . $selected . "> " . $name ."</option>";



having $name and $selected set to the value name and $selected set to "SELECTED"

ralph l mayo
12-03-2005, 03:30 AM
You shouldn't have to use sessions or a db for this unless there's a particular reason you want to. Does the file post to itself? If so:


<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="session">
<option value="">SELECT SESSION</option>
<option value="1" <?php echo (isset($_POST['session']) && $_POST['session'] == '1') ? 'selected' : ''; ?>>SESSION 1</option>></select>

<select name="employment_status">
<option value="">SELECT STATUS</option>
<option value="1" <?php echo (isset($_POST['employment_status']) && $_POST['employment_status'] == '1') ? 'selected' : ''; ?>>STATUS 1</option>></select>
<input type="submit" value="submit">
</form>
</body>
</html>

presley2
12-03-2005, 03:02 PM
Ralph,
Maybe this will help you with MY problem:



// Set the page title and include the HTML header.
$page_title = 'Register';
include ('header.inc');

if (isset($_POST['submit'])) { // Handle the form.

$message = NULL; // Create an empty new variable.

// Check for a first name.
if (empty($_POST['first_name'])) {
$fn = FALSE;
$message .= '<p>You forgot to enter your first name!</p>';
} else {
$fn = $_POST['first_name'];
}

// Check for a last name.
if (empty($_POST['last_name'])) {
$ln = FALSE;
$message .= '<p>You forgot to enter your last name!</p>';
} else {
$ln = $_POST['last_name'];
}

// Check for a last name.
if (empty($_POST['session'])) {
$s = FALSE;
$message .= '<p>You forgot to enter your session!</p>';
} else {
$s = $_POST['session'];
}

// Check for a last name.
if (empty($_POST['employment_status'])) {
$es = FALSE;
$message .= '<p>You forgot to enter your employment status!</p>';
} else {
$es = $_POST['employment_status'];
}





if ($fn && $ln && $s && $es ) { // If everything's OK.

// Register the user in the database.
require_once ('mysql_connect.php'); // Connect to the db.

// Make the query.
$query = "INSERT INTO job_apps (first_name, last_name, session, employment_status) VALUES ('$fn', '$ln', '$s', '$es')";
$result = @mysql_query ($query); // Run the query.
if ($result) { // If it ran OK.

// Send an email, if desired.
echo '<p><b>You have been registered!</b></p>';
include ('footer.inc'); // Include the HTML footer.
exit(); // Quit the script.

} else { // If it did not run OK.
$message = '<p>You could not be registered due to a system error. We apologize for any inconvenience.</p><p>' . mysql_error() . '</p>';
}

mysql_close(); // Close the database connection.

} else {
$message .= '<p>Please try again.</p>';
}

} // End of the main Submit conditional.

// Print the message if there is one.
if (isset($message)) {
echo '<font color="red">', $message, '</font>';
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset><legend>Enter your information in the form below:</legend>

<p><b>First Name:</b> <input type="text" name="first_name" size="15" maxlength="15" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>

<p><b>Last Name:</b> <input type="text" name="last_name" size="30" maxlength="30" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>

<select name="session">
<option value="" <? echo ($session=='') ? "" : ""; ?>>SELECT SESSION</option>
<option value="1" <? echo ($session=='1') ? "selected=" : ""; ?>>SESSION 1</option>></select>

<select name="employment_status">
<option value="" <? echo ($session=='') ? "selected" : ""; ?>>SELECT STATUS</option>
<option value="1" <? echo ($employment_status=='1') ? "selected" : ""; ?>>STATUS 1</option>></select>



<div align="center"><input type="submit" name="submit" value="Register" /></div>

</form><!-- End of Form -->

<?php
include ('footer.inc'); // Include the HTML footer.
?>

ralph l mayo
12-04-2005, 05:15 AM
<select name="session">
<option value="" <? echo ($session=='') ? "" : ""; ?>>SELECT SESSION</option>
<option value="1" <? echo ($session=='1') ? "selected=" : ""; ?>>SESSION 1</option>></select>

<select name="employment_status">
<option value="" <? echo ($session=='') ? "selected" : ""; ?>>SELECT STATUS</option>
<option value="1" <? echo ($employment_status=='1') ? "selected" : ""; ?>>STATUS 1</option>></select>


I don't see where you're setting $session or $employment_status to anything before you check them. You are setting a few vars called $s and $es.