I know that this is something basic but nothing that I have been trying seems to work. I have a form that has a "Ratings" dropdown menu for each Competency. How can I test on the action page so that the user didn't forget to make a selection from one of the dropdown menus. Thank you.
code---------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><basefont face="Arial">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<Link Rel=stylesheet TYPE="text/css" href="Styles.css">
<title>Assign Ratings</title>
</head>
<body>
<?php
// include configuration file
include('config.php');
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$query = "SELECT CompetencyID, CompetencyName, SkilledDefinition
FROM LTRCOMPETENCY";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// if records are present
if (mysql_num_rows($result) > 0) {
//$row = mysql_fetch_object($result);
// get CompetencyID and Rating
$CompetencyID = $row->CompetencyID;
$Rating = $row->Rating;
echo "<form method = post action = 'processRatings.php'>";
// print them one after another
echo "<div align='center'>";
echo "<table cellpadding=10 border=1 id=reg width=780>";
echo "<tr>";
echo "<td><strong> </strong></td>";
echo "<td><strong>Competency</strong></td>";
echo "<td><strong>Performance Criteria</strong></td>";
echo "<td><strong>Rating</strong></td>";
echo "</tr>";
//set a counter
if (mysql_num_rows($result) > 0) {
$i=1;
while ($row = mysql_fetch_object($result)) {
//echo "<input type = radio name = aid value = '".$row->aid."'>".$row->atitle."</input><br />";
echo "<tr>";
echo "<td>".$i++."</td>";
echo "<td>".$row->CompetencyName."</td>";
echo "<td align='left'>".$row->SkilledDefinition."</td>";
echo "<input type=hidden name=CompetencyID[] value = '".$row->CompetencyID."'>";
echo "<td>
<select name='Rating[]'>
<option value=0>Please make a selection</option>
<option value=1>1 - Not at all</option>
<option value=2>2 - To some extent</option>
<option value=3>3 - To a moderate extent</option>
<option value=4>4 - To a considerable extent</option>
<option value=5>5 - To a great extent</option>
</select>
</td>";
echo "</tr>";
}
}
echo "<tr>";
echo "<td colspan=4><input type = submit name = submit value=Submit></td>";
echo "</tr>";
echo "</table>";
echo "</div>";
echo '</form>';
}
else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
</body>
</html>
Making sure user made a selection
Moderator: General Moderators
-
zenAngler2008
- Forum Newbie
- Posts: 2
- Joined: Sat Mar 15, 2008 6:46 pm
-
hayson1991
- Forum Newbie
- Posts: 14
- Joined: Thu Mar 20, 2008 1:19 pm
Re: Making sure user made a selection
The name attribute of the any tag should not have the "[]" at the end.zenAngler2008 wrote:I know that this is something basic but nothing that I have been trying seems to work. I have a form that has a "Ratings" dropdown menu for each Competency. How can I test on the action page so that the user didn't forget to make a selection from one of the dropdown menus. Thank you.
code---------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><basefont face="Arial">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<Link Rel=stylesheet TYPE="text/css" href="Styles.css">
<title>Assign Ratings</title>
</head>
<body>
<?php
// include configuration file
include('config.php');
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$query = "SELECT CompetencyID, CompetencyName, SkilledDefinition
FROM LTRCOMPETENCY";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// if records are present
if (mysql_num_rows($result) > 0) {
//$row = mysql_fetch_object($result);
// get CompetencyID and Rating
$CompetencyID = $row->CompetencyID;
$Rating = $row->Rating;
echo "<form method = post action = 'processRatings.php'>";
// print them one after another
echo "<div align='center'>";
echo "<table cellpadding=10 border=1 id=reg width=780>";
echo "<tr>";
echo "<td><strong> </strong></td>";
echo "<td><strong>Competency</strong></td>";
echo "<td><strong>Performance Criteria</strong></td>";
echo "<td><strong>Rating</strong></td>";
echo "</tr>";
//set a counter
if (mysql_num_rows($result) > 0) {
$i=1;
while ($row = mysql_fetch_object($result)) {
//echo "<input type = radio name = aid value = '".$row->aid."'>".$row->atitle."</input><br />";
echo "<tr>";
echo "<td>".$i++."</td>";
echo "<td>".$row->CompetencyName."</td>";
echo "<td align='left'>".$row->SkilledDefinition."</td>";
echo "<input type=hidden name=CompetencyID[] value = '".$row->CompetencyID."'>";
echo "<td>
<select name='Rating[]'>
<option value=0>Please make a selection</option>
<option value=1>1 - Not at all</option>
<option value=2>2 - To some extent</option>
<option value=3>3 - To a moderate extent</option>
<option value=4>4 - To a considerable extent</option>
<option value=5>5 - To a great extent</option>
</select>
</td>";
echo "</tr>";
}
}
echo "<tr>";
echo "<td colspan=4><input type = submit name = submit value=Submit></td>";
echo "</tr>";
echo "</table>";
echo "</div>";
echo '</form>';
}
else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
</body>
</html>
Re: Making sure user made a selection
First, if you need to post code here, please use the [syntax=php]...[/syntax] or {PHP]... tags to enclose your code. It is very difficult to read a long stretch of code as you have posted it.
Now, to your question: there are several ways to validate form input. You can do it in Javascript in the script file that contains the user form, or you can do it in the PHP code on the server, after it has been sent, or for more complicated validation, you can do it with Ajax. My preference is to validate it before it is sent to the server, then you don't have to make the user start all over again if they miss something. In this case, since you only need to check that some selection has been made, the approach would probably be to include a Javascript function that tests for a non-zero value for the select form element. The trick is when to call the function. One way to do it is to use a regular command button instead of the Submit button, then use the OnClick event to call your Javascript function, which tests for a non-zero value, then either triggers the Submit event or pops up an Alert box for the user. At the moment, I can't remember the syntax for triggering a form's Submit event, but it's pretty straightforward.
Now, to your question: there are several ways to validate form input. You can do it in Javascript in the script file that contains the user form, or you can do it in the PHP code on the server, after it has been sent, or for more complicated validation, you can do it with Ajax. My preference is to validate it before it is sent to the server, then you don't have to make the user start all over again if they miss something. In this case, since you only need to check that some selection has been made, the approach would probably be to include a Javascript function that tests for a non-zero value for the select form element. The trick is when to call the function. One way to do it is to use a regular command button instead of the Submit button, then use the OnClick event to call your Javascript function, which tests for a non-zero value, then either triggers the Submit event or pops up an Alert box for the user. At the moment, I can't remember the syntax for triggering a form's Submit event, but it's pretty straightforward.