Welcome! Log In Create A New Profile Forum Rules

Advanced

Dynamic lists are not reloaded after the form is submitted

Posted by THYREN 
Dynamic lists are not reloaded after the form is submitted
four years ago
Hi all,

I think that book is really well done but there's one thing I didn't see in it: how to reload the content of dynamic lists after a form has been submitted?

I have a form where the code checks if the user has entered all the required information after he/she submitted the form. If the user forgets something, the form is displayed again with all the previous info (sticky forms) and everything works fine except for all the lists: they just display the first line ("Choose a Location:") and nothing else.

I display all the different options of my lists by looping through an array populated by a mysql query but it looks like it doesn't run again after the first time.

Here's the code for one of them:

...
<td width="30%"><select name="location"><option value="NULL">Choose a Location:</option>
<?php
$query = 'SELECT location_id, begin_km, landmark_desc FROM location ORDER BY begin_km ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {echo "<option value=\"$row[0]\">$row[1] $row[2]</option>";}
?></select></td>
</tr>
...

My code is inserted inside the html form so I hope that it's not the problem (I wouldn't see why anyway). Does anyone has an idea?

Thanks in advance,
Re: Dynamic lists are not reloaded after the form is submitted
four years ago
I'll take a stab.

- use the $_GET super global or $_POST super global to grab the previously selected value the user chose.

so, something like this after the $result var runs your query
$select_val = $_POST['select_val'];

- Then use that in the the loop to compare values

<?

while ($row = mysql_fetch_array ($result, MYSQL_NUM))
{
if($row[0] == $select_val)
{
echo "<option value=\"$row[0]\" selected=\"selected\">$row[1] $row[2]</option>";
}
else
{
echo "<option value=\"$row[0]\">$row[1] $row[2]</option>";
}
}

?>


Hope that helps.

==
Ian
Re: Dynamic lists are not reloaded after the form is submitted
four years ago
Thank you for your suggestion but this code would enable the list to become a "sticky list", which is what I need at the end but I have another problem to fic before : it is that once the form has been submitted and that there is an error, it comes back to this form but all the lists have lost all their values, except the first one.
Re: Dynamic lists are not reloaded after the form is submitted
four years ago
Can you show the code you are using for the form, please.
Re: Dynamic lists are not reloaded after the form is submitted
four years ago
Here's the whole code for this page (I replaced personal information by ***) but because all the other form objects work fine, I don't think there's any problem with the form in itself...


And by the way, in the <form action="noticecreation.php" method="post"> line, I can't replace the file name by " $_SERVER[PHP_SELF] ", otherwise, I get an error saying
"Not Found
The requested URL /$_SERVER[PHP_SELF] was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request."

??? :-/


<?php
session_name ('YourVisitID');
session_start(); // Start the session.

require_once ('./includes/config.inc.php'); // error handling script
require_once ('./includes/db_connect.php'); // Connects to the database

// If no session value is present, redirects the user:
if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT'])) ) {

// Start defining the URL:
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/index.php'; // Add the page.
header("Location: $url");
exit(); // Quit the script.
}

$notice_number = date("YmdHis");
$start_date = "NCR not activated yet";
$project = "***"; //***

if (isset($_POST['submitted'])) { // Handles the form:

// Check for an location:
if ($_POST['location'] <> "NULL") {
$location = $_POST['location'];
} else {
$location = FALSE;
echo '<p><font color="red">Please select a location!</font></p>';
}

// Check for an NCE Category:
if ($_POST['nce_type'] <> "NULL") {
$nce_type = $_POST['nce_type'];
} else {
$nce_type = FALSE;
echo '<p><font color="red">Please select an NCE category!</font></p>';
}

// Check for a nonconformity description:
if ($_POST['non_conformity_desc'] <> "NULL") {
$non_conformity_desc = $_POST['non_conformity_desc'];
} else {
$non_conformity_desc = FALSE;
echo '<p><font color="red">Please describer the nonconformity!</font></p>';
}

if ($location && $nce_type && $non_conformity_desc) { //The required information has been correctly selected:

$notice_type = "1"; //notice_type 1 = notice
$notice_status = "1"; // notice_status 1 = notice created

// Inserts into the db the new created NCR
$query = "INSERT INTO notice (notice_number, notice_type, notice_status, project, originator, location, nce_cat, non_conformity_desc)
VALUES ('$notice_number', '$notice_type', 'notice_status', '$project', '$originator', '$location', '$nce_type', '$non_conformity_desc')";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error() );

if (mysql_affected_rows() == 1) { // If the notice creation ran OK, updates the notice history:

$originator = $_SESSION['user_id']; // Originator's user ID
$modif_date = date("YmdHi"); // Generates the notice modification history time

// Inserts into the db the new created NCR (ncr_type 1 = notice and ncr_status 1 = notice created)
$query = "INSERT INTO notice_history (notice_number, new_status, notice_type, modif_by, modif_date)
VALUES ('$notice_number', 'notice_status', '$notice_type', '$originator', '$modif_date')";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error() );

if (mysql_affected_rows() == 1) { // If the notice history ran OK:
echo "<h3>The notice $notice_number has been correctly created and the Technical Reviewer should receive an email shortly!</h3>";

// sends an automatically generated email to the Technical reviewer.


exit();
} else { // If the notice history did not run OK:
echo '<p><font color="red">You could not create this notice due to a system error. We apologize for any inconvenience.</font></p>';
}

} else { // If the INSERT INTO did not run OK.
echo '<p><font color="red">Error: You could not be create this notice due to a system error. We apologize for any inconvenience.</font></p>';
}

} else { // If one of the required information has NOT been correctly selected:
echo '<p><font color="red">Please try again!</font></p>';
}

mysql_close(); // Close the database connection.
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[www.w3.org];
<html xmlns="[www.w3.org]; xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Notice Creation</title>

<style type="text/css" media="screen">
@import url("./includes/main.css");
</style>

</head>
<body>

<form action="noticecreation.php" method="post">
<table width="100%" border="1" align="center">
<tr>
<td width="15%" rowspan="7" align="center" valign="top"><p align="center"><img src="./images/bcmot.gif" alt="BCMoT" name="BCMoT" width="112" height="97" align="top" id="BCMoT" /></p>
<p align="center">***</p></td>
<td height="85%" colspan="4"><div align="center">
<h2>***</h2>
</div></td>
</tr>
<tr>
<td width="10%" height="30" class="headerboxes">NCR Number</td>
<td width="30%"><input name="notice_number" type="text" readonly="true" value="<?php echo $notice_number; ?>" size="15" maxlength="14" /></td>
<td width="15%" class="headerboxes">Date Opened</td>
<td width="30%"><input name="start_date" type="text" readonly="true" value="<?php echo $start_date; ?>" size="20" maxlength="30" /></td>
</tr>
<tr>
<td width="10%" height="30" class="headerboxes">Project</td>
<td width="30%"><input name="project" type="text" id="project" readonly="true" value="<?php echo $project; ?>" size="32" maxlength="30" /></td>
<td width="15%" class="headerboxes">Concession</td>
<td width="30%">&nbsp;</td>
</tr>
<tr>
<td width="10%" height="30" class="headerboxes">Originator</td>
<td width="30%"><input name="originator" type="text" readonly="true" value="<?php echo "{$_SESSION['first_name']} {$_SESSION['last_name']}"; ?>" size="32" maxlength="41" /></td>
<td width="15%" class="headerboxes">Response Deadline</td>
<td width="30%">&nbsp;</td>
</tr>
<tr>
<td width="10%" height="30" class="headerboxes">Audit Item</td>
<td width="30%">&nbsp;</td>
<td width="15%" class="headerboxes">Location</td>
<td width="30%"><select name="location"><option value="NULL">Choose a Location:</option>
<?php
/* ORIGINAL ONE:
$query = 'SELECT location_id, route_num, direction, begin_km, landmark_desc FROM location ORDER BY begin_km ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
echo "<option value=\"$row[0]\">$row[3] $row[4]</option>";
}

if($row[0] == $select_val){
echo "<option value=\"$row[0]\" selected=\"selected\">$row[3] $row[4]</option>";
} else {
echo "<option value=\"$row[0]\">$row[3] $row[4]</option>";
}
*/
$query = 'SELECT location_id, route_num, direction, begin_km, landmark_desc FROM location ORDER BY begin_km ASC';
$result = mysql_query ($query);
$location_id = $_POST['location_id'];
while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
if($row[0] == $select_val){
echo "<option value=\"$row[0]\" selected=\"selected\">$row[3] $row[4]</option>";
} else {
echo "<option value=\"$row[0]\">$row[3] $row[4]</option>";
}
}
?></select></td>
</tr>
<tr>
<td width="10%" height="30" class="headerboxes">Specification</td>
<td width="30%">&nbsp;</td>
<td width="15%" class="headerboxes">QMS Standard / Procedure</td>
<td width="30%">&nbsp;</td>
</tr>
<tr>
<td width="10%" height="30" class="headerboxes">NCE Category</td>
<td width="30%"><select name="nce_type"><option value="NULL">Choose an NCE Category:</option>
<?php
$query = 'SELECT nce_type FROM nce_cat ORDER BY nce_type ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {echo "<option value=\"$row[0]\">$row[0]</option>";}
?>
</select></td>
<td width="15%" class="headerboxes">Spec and Clause#</td>
<td width="30%">&nbsp;</td>
</tr>
<tr>
<td width="15%"><div align="center">
<h3>II) ORIGINATOR</h3>
</div></td>
<td width="85%" colspan="4"><h3>DESCRIPTION OF NON-CONFORMITY:</h3>
<p><textarea name="non_conformity_desc" cols="120" rows="5" id="non_conformity_desc"><?php if (isset($_POST['non_conformity_desc'])) echo $_POST['non_conformity_desc']; ?></textarea></p></td>
</tr>
</table>

<div align="center"><input type="submit" name="submit" value="Create Notice" /></div>
<input type="hidden" name="submitted" value="TRUE" />

</form>

</body>
</html>



Edited 2 time(s). Last edit was four years ago by THYREN.
Re: Dynamic lists are not reloaded after the form is submitted
four years ago
Check the INSERT for the NCR. I think the one value is missing the "$" in the front of it.

Is this an "IF" statement?
 $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error() );

Check the format used on the echo statements. Shouldn't the variables be concatenated in these lines?
 echo "<option value=\"$row[0]\" selected=\"selected\">$row[3] $row[4]</option>";
 echo '<option value="' . $row[0] . '" selected="selected"> ' . $row[3] .  $row[4] . '</option>" ';
Re: Dynamic lists are not reloaded after the form is submitted
four years ago
jlhaslip Wrote:
-------------------------------------------------------
> Check the INSERT for the NCR. I think the one
> value is missing the "$" in the front of it.
>
> Is this an "IF" statement?
$result = mysql_query
> ($query) or trigger_error("Query: $query\n<br
> />MySQL Error: " . mysql_error() );
>
> Check the format used on the echo statements.
> Shouldn't the variables be concatenated in these
> lines?
echo "<option value=\"$row[0]\"
> selected=\"selected\">$row[3]
> $row[4]</option>";echo '<option value="'
> . $row[0] . '" selected="selected"> ' . $row[3]
> . $row[4] . '</option>" ';


Thank you for the missing $ before "notice_status" !

The "$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error() );" line is actually from the book :-p It just either executes the query or displays an error message.

And for the code-generated list, I just use a space between both fields (instead of concatenating them) because what's how the users want it to be displayed. Do you think that's why the list is NOT generated again after the form has been submitted? (which is the whole point of this thread btw :D )

Thank you jlhaslip for going through all my code! I really appreciate that! :-)

Re: Dynamic lists are not reloaded after the form is submitted
four years ago
I expect the problem is because you close the database connection after handling the form submission. So when you redisplay the form, those lists whose values come from a database query have no database-based options.

Also, I don't know where you got this from:
if ($_POST['location'] <> "NULL") {

But I don't think that'll work, at least not as you would expect. You're comparing $_POST['location'] to the STRING whose value is "NULL". This is entirely different than having a NULL value (which is to say an unknown value). Use isset() for non-text inputs, and empty() or strlen() for text inputs and textareas.

Best Wishes,
Larry

Writer/Web Developer/Instructor
Forum Moderator
Re: Dynamic lists are not reloaded after the form is submitted
four years ago
Indeed, it was I was closing the database connection!

Thank you so much Larry (and thank you for your book too :D )!
Sorry, only registered users may post in this forum.

Click here to login