PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Eiolon
Forum Newbie
Posts: 17 Joined: Tue Feb 14, 2006 1:42 pm
Post
by Eiolon » Fri Apr 09, 2010 3:16 pm
When an error occurs on a script, the values in the text fields are retained by echoing the input. I am not sure where to echo the input for my select menu so the user does not have to select the option again.
Here is my select menu:
Code: Select all
<select name="department_id" id="department_id" style="width:406px;"><option>SELECT</option><?php while($row_departments = mysql_fetch_array($departments)) { print '<option value="' . $row_departments['id'] . '">' . $row_departments['code'] . ' - ' . $row_departments['name'] . '</option>';} ?></select>
Thanks!
minorDemocritus
Forum Commoner
Posts: 96 Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA
Post
by minorDemocritus » Fri Apr 09, 2010 3:59 pm
Try something like this:
Code: Select all
<select name="department_id" id="department_id" style="width:406px;"><option>SELECT</option><?php
while($row_departments = mysql_fetch_array($departments)) {
print '<option value="' . $row_departments['id'] . '"';
// some simple validation
if ( isset($_GET['department_id'])
&& htmlspecialchars($_GET['department_id']) == $row_departments['id']) {
// the HTML way
print 'selected="selected"';
}
print '>' . $row_departments['code'] . ' - ' . $row_departments['name'] . '</option>';
} ?></select>
minorDemocritus
Forum Commoner
Posts: 96 Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA
Post
by minorDemocritus » Fri Apr 09, 2010 4:08 pm
minorDemocritus wrote: Try something like this:
Code: Select all
<select name="department_id" id="department_id" style="width:406px;"><option>SELECT</option><?php
while($row_departments = mysql_fetch_array($departments)) {
print '<option value="' . $row_departments['id'] . '"';
// some simple validation
if ( isset($_GET['department_id'])
&& htmlspecialchars($_GET['department_id']) == $row_departments['id']) {
// the HTML way
print 'selected="selected"';
}
print '>' . $row_departments['code'] . ' - ' . $row_departments['name'] . '</option>';
} ?></select>
...But that might be too slow, so maybe this would be better:
Code: Select all
<select name="department_id" id="department_id" style="width:406px;"><option>SELECT</option><?php
// some simple validation
$dept_id_from_user = '';
if ( isset($_GET['department_id']) {
$dept_id_from_user = htmlspecialchars($_GET['department_id']);
}
while($row_departments = mysql_fetch_array($departments)) {
print '<option value="' . $row_departments['id'] . '"';
if ($dept_id_from_user == $row_departments['id']) {
// the HTML way
print 'selected="selected"';
}
print '>' . $row_departments['code'] . ' - ' . $row_departments['name'] . '</option>';
} ?></select>
Eiolon
Forum Newbie
Posts: 17 Joined: Tue Feb 14, 2006 1:42 pm
Post
by Eiolon » Fri Apr 09, 2010 4:53 pm
Thanks for the help. That does the job if I am sending the user to a different page to display the error and they have to hit the back button on the browser. However, I am making the error display on the same page as the form. So when the error is displayed it is still not displaying what was chosen.
minorDemocritus
Forum Commoner
Posts: 96 Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA
Post
by minorDemocritus » Fri Apr 09, 2010 4:59 pm
I'm gonna need to see more of the code to help you with that...
Eiolon
Forum Newbie
Posts: 17 Joined: Tue Feb 14, 2006 1:42 pm
Post
by Eiolon » Fri Apr 09, 2010 5:03 pm
Sure thing:
Code: Select all
<?php
require_once('../includes/mysql.php');
require_once('../includes/functions.php');
$query_departments = "SELECT id, code, name FROM departments ORDER BY code ASC";
$departments = mysql_query($query_departments) OR die ('Cannot retrieve a list of departments.');
$query_frequencies = "SELECT id, frequency FROM frequencies";
$frequencies = mysql_query($query_frequencies) OR die ('Cannot retrieve a list of frequencies.');
if (isset($_POST['submit'])) {
$errors = array();
if (($_POST['department_id']) < 1) {
$errors[] = 'Select a department';
}
if ($_POST['department_id']) {
$did = escape_data($_POST['department_id']);
}
if (empty($_POST['call_number'])) {
$errors[] = 'Call number must be entered';
}
if ($_POST['call_number']) {
$cn = escape_data($_POST['call_number']);
}
if (($_POST['frequency_id']) < 1) {
$errors[] = 'Frequency must be entered';
}
if ($_POST['frequency_id']) {
$fid = escape_data($_POST['frequency_id']);
}
if ($_POST['type_book']) {
$book = escape_data($_POST['type_book']);
}
if ($_POST['type_cd']) {
$cd = escape_data($_POST['type_cd']);
}
if ($_POST['type_dvd']) {
$dvd = escape_data($_POST['type_dvd']);
}
if ($_POST['type_internet']) {
$internet = escape_data($_POST['type_internet']);
}
if ($_POST['type_map']) {
$map = escape_data($_POST['type_map']);
}
if ($_POST['type_microfiche']) {
$microfiche = escape_data($_POST['type_microfiche']);
}
if ($_POST['type_picture']) {
$picture = escape_data($_POST['type_picture']);
}
if ($_POST['description']) {
$description = escape_data($_POST['description']);
}
if ($_POST['url']) {
$url = escape_data($_POST['url']);
}
if ($_POST['acquired']) {
$acquired = escape_data($_POST['acquired']);
}
if (empty($errors)) {
$insert = "INSERT INTO documents (department_id, call_number, frequency_id, type_book, type_cd, type_dvd, type_internet, type_map, type_microfiche, type_picture, description, url, acquired, added, modified) VALUES ('$did','$cn','$fid','$book','$cd','$dvd','$internet','$map','$microfiche','$picture','$description','$url','$acquired',now(),now())";
$result = mysql_query($insert) OR die ('Could not add the document to the database.');
if ($insert) {
header('Location: document_added.php');
exit; }
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="../includes/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
if (!empty($errors)) {
echo '<font color="red">Please correct the following problem(s):<ul>';
foreach ($errors as $error) {
echo "<li>$error</li>\n";
}
echo '</ul></font>';
}
?>
<form id="add" name="add" method="post" action="">
<table border="0" cellpadding="3" cellspacing="3" width="100%">
<tr>
<td width="90">Department:</td>
<td><select name="department_id" id="department_id" style="width:406px;"><option>SELECT</option><?php
while($row_departments = mysql_fetch_array($departments)) {
print '<option value="' . $row_departments['id'] . '"';
// some simple validation
if (isset($_GET['department_id'])
&& htmlspecialchars($_GET['department_id']) == $row_departments['id']) {
// the HTML way
print 'selected="selected"';
}
print '>' . $row_departments['code'] . ' - ' . $row_departments['name'] . '</option>';
} ?></select></td>
</tr>
<tr>
<td width="90">Call Number:</td>
<td><input type="text" name="call_number" id="call_number" style="width:400px;" value="<?php if (isset($_POST['call_number'])) echo $_POST['call_number']; ?>"></td>
</tr>
<tr>
<td width="90">Material Type:</td>
<td>
<input type="checkbox" name="type_book" value="1"> Book
<input type="checkbox" name="type_cd" value="1"> CD
<input type="checkbox" name="type_dvd" value="1"> DVD
<input type="checkbox" name="type_internet" value="1"> Internet
<input type="checkbox" name="type_map" value="1"> Map
<input type="checkbox" name="type_microfiche" value="1"> Microfiche
<input type="checkbox" name="type_picture" value="1"> Picture
</td>
</tr>
<tr>
<td width="90">Frequency:</td>
<td><select name="frequency_id" id="frequency_id" style="width:406px;"><option>SELECT</option><?php while($row_frequencies = mysql_fetch_array($frequencies)) { print '<option value="' . $row_frequencies['id'] . '">' . $row_frequencies['frequency'] . '</option>';}?></select></td>
</tr>
<tr>
<td width="90">Description:</td>
<td><textarea rows="4" name="description" style="width:400px;"><?php if (isset($_POST['description'])) echo $_POST['description']; ?></textarea></td>
</tr>
<tr>
<td width="90">URL:</td>
<td><input type="text" name="url" id="url" style="width:400px;" value="<?php if (isset($_POST['url'])) echo $_POST['url']; ?>"></td>
</tr>
<tr>
<td width="90">Date Acquired:</td>
<td><input type="text" name="acquired" id="acquired" maxlength="10" value="<?php if (isset($_POST['acquired'])) echo $_POST['acquired']; ?>"> YYYY-MM-DD format required</td>
</tr>
</table>
<br />
<input type="submit" name="submit" id="button" value="Add Document" />
</form>
</body>
</html>
minorDemocritus
Forum Commoner
Posts: 96 Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA
Post
by minorDemocritus » Fri Apr 09, 2010 5:11 pm
FYI, "something like this:" usually means "figure out what I'm doing here, and adapt it for your form".
In this case, I used $_GET, but you used $_POST in the script... so just change the $_GET to $_POST :
Code: Select all
<?php
while($row_departments = mysql_fetch_array($departments)) {
print '<option value="' . $row_departments['id'] . '"';
// some simple validation
if (isset($_POST['department_id'])
&& htmlspecialchars($_POST['department_id']) == $row_departments['id']) {
// the HTML way
print ' selected="selected"';
}
print '>' . $row_departments['code'] . ' - ' . $row_departments['name'] . '</option>';
} ?>
Eiolon
Forum Newbie
Posts: 17 Joined: Tue Feb 14, 2006 1:42 pm
Post
by Eiolon » Fri Apr 09, 2010 5:19 pm
I understand now. Thanks very much for your help