Jump to content
Larry Ullman's Book Forums

Sticky Drop Down Box


Recommended Posts

I have seen an example of creating a sticky Drop Down box, but I'm a little confused on how to get it to work.

 

In the 'if ($_SERVER['REQUEST_METHOD'] == 'POST') { // handle the form' section, I have:

 

 

$stickytest = '';

if(isset($_POST['submit'])) {

$stickytest = $_POST['name'];

}

 

print "$stickytest";

print $_POST['name'];

 

For this code, the two things that are printed are in fact equal during my testing.

 

Then, in my '} // end of form submission if' '// display the form' code, I have:

 

 

if ($stickytest == $row['name']) {

print "HI";

print "<option selected value=\" {$row['name']} \"> {$row['name']} </option>";

} else {

print "HO";

print "<option value=\" {$row['name']} \"> {$row['name']} </option>";

}

 

I am expecting to see either HI or HO during my testing, and I am seeing neither.

Link to comment
Share on other sites

Here is all of it:

 

 

<?php

 

define('TITLE', 'Test');

include('templates/header.html');

print '<h2>Test</h2>';

 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { // handle the form

 

// need the database connection

include('mysql_connect.php');

 

// validate the form data

 

$problem = FALSE;

if (!empty($_POST['name'])) {

 

// prepare the values for storing:

$name = trim(strip_tags($_POST['name']));

} else {

print '<p style="color: red;">Please submit all fields</p>';

$problem = TRUE;

}

 

if (!$problem) {

 

$statusarray = array ('Name' => 0);

 

// Define the query:

$statusquery = 'SELECT name, status FROM users ORDER BY name';

 

// run the query

if ($sq = mysql_query($statusquery, $dbc)) {

 

// retrieve and process every record:

while ($statusrow = mysql_fetch_array($sq)) {

 

$name = $statusrow['name'];

$status = $statusrow['status'];

$statusarray[$name] = $status;

 

} // end of fetch

 

foreach ($statusarray as $aname => $astatus) {

print "<p>$aname: $astatus</p>\n";

}

 

} else { // Query didn't run.

print '<p style="color: red;">Could not retrieve the data because:<br />' . mysql_error($dbc)

. '.</p><p>The query being run was: ' . $statusquery . '</p>';

} // End of query IF.

 

$stickytest = '';

if(isset($_POST['submit'])) {

$stickytest = $_POST['name'];

}

 

print "$stickytest";

print $_POST['name'];

 

if ($astatus <> 'P') {

print "Invalid choice.";

$valid_choice = 'FALSE';

} else {

$valid_choice = 'TRUE';

}

 

if ($valid_choice <> 'FALSE') {

 

// define the query

 

$query = "INSERT INTO users (id, name) VALUES (0, '$name')";

 

// execute the query

 

if (@mysql_query($query, $dbc)) {

print '<p>Name has been added.</p>';

} else {

print '<p style="color: red;">Could not add the name because:<br />' .

mysql_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';

}

 

}

 

} // no problem

 

mysql_close($dbc); // close the connection

 

} // end of form submission if

 

// display the form

?>

 

<div><p>Please enter your name:</p>

<form action="add_name.php" method="post">

 

<table cellpadding="2" cellspacing="2" align="left">

<tr>

<td>Name:</td>

<td><input type="text" name="name" size="20"

value="<?php if (isset($_POST['name'])) { print htmlspecialchars($_POST['name']); } ?>" /></td>

</tr>

 

<?php

 

$teams = array ('user1', 'user2');

 

foreach ($teams as $key => $value) {

 

print "<tr><td>$value:</td><td><select name=\"$value\">";

 

include('mysql_connect.php');

 

$query = "SELECT * FROM $value";

 

if ($r = mysql_query($query, $dbc)) { // run the query

 

// if sticky add selected parm

 

while ($row = mysql_fetch_array($r)) {

 

if ($stickytest == $row['name']) {

print "HI";

print "<option selected=\"selected\" value=\" {$row['name']} \"> {$row['name']} </option>";

} else {

print "HO";

print "<option value=\" {$row['name']} \"> {$row['name']} </option>";

}

 

}

 

} else { // query did not run

print '<p style="color: red;">Could not retrieve the data because:<br />' . mysql_error($dbc)

. '.</p><p>The query being run was: ' . $query . '</p>';

} // end of query if

 

mysql_close($dbc); // close the connection

 

print "</select></td></tr>";

 

}

 

?>

 

</table>

<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

<input type="submit" name="submit" value="Enter Name" />

 

</form>

 

<?php include('templates/footer.html');

?>

 

</div>

</body>

</html>

Link to comment
Share on other sites

 Share

×
×
  • Create New...