Jump to content

Recommended Posts

I have a form in which several fields are required....one of which is a drop down menu.  The stop edits I wrote for all the fields (which are text fields) are working perfectly.  For instance:

//Check if Primary Contact was  entered
if (trim($primecontact) == '')
{
   echo "<h2>Sorry, you must enter a primary contact</h2><br>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}

 

 

Unfortunately, using the example above does not work on the drop down omenu option  Here's the code

 

//Check if Attendee 1 Registration Type was selected
if ($att1fp) == 'NULL')
{
   echo "<h2>Sorry, you must enter a registration type for Attendee 1</h2><br>\n";
   echo "<a href=\"index.php?content=registration\">Please try again</a><br>\n";
   $baduser = 1;
   exit;
}

 

If you need more code, I will be glad to oblige.

 

Help me pretty please???

Thank you

Link to comment
https://forums.phpfreaks.com/topic/190925-stop-edit-on-a-drop-down-menu/
Share on other sites

Well, that code is not properly formatted. I'm surprised the page is not generating errors. The first line of the IF statement has one opening paren, but two closing parens. It needs to look like this:

if (($att1fp) == 'NULL')

 

However, that is not how you should validate a select field. Never, ever trust input from a user. Just because you have a select list does not mean the user cannot submit values which you didn't intend to be in that list. Malicious users can replicate your form and manipulate it to submit whatever values they want. The good news is that validating a select list is pretty simple. The approach may change slightly based upon how you generate your list, but the concept is the same.

 

If my list is a fixed list of values that is not stored in a database, I will typically use an array variable to store the values. If you are using the same PHP script to generate the form and process the form I will define the array on that page. If different pages need the list I will define the array on a separate page which is included by the pages that need it. I will then use the list to both generate the select list AND to validate the user input.

 

Example:

$colorList = array ('red', 'green', 'blue', 'yellow');

 

When creating the select list

$colorOptions = "<option value=\"\">--Select One--</option>\n";
foreach ($colorList as $color)
{
    $colorOptions .= "<option value=\"{$color}\">{$color}</option>\n";
}
echo "<select name=\"color\">";
echo $colorOptions;
echo "</select>";

 

Validating the input for the select list

//Validate that the posted value is in the valid list of values
if (!in_array($_POST['color'], $colorList))
{
    echo "You must select a color option";
}

 

If your values are in a database it is just as easy. Just query the list when creating the select list. When validating, do a query to see if the value exists in the list (after properly escaping the value first).

Working on understanding above......

 

I have/had the selection box on an html page but going by the code above, I have replaced that particular piece with a php script following along with mjdamato's perfect example.  I am able to get the selection box to appear on the page with a default of --Select One--.  However, when I click the drop down, there are no options to choose from. 

 

Going back to one of the questions mjdamato posed......the selections are not in the database. 

 

Here's the code for the box:

<b><font style="color:red;">*</font>Registration Type:</b>
	  


<?php

$regList = array('Full Registration', 'Partial Registration');

$regOptions = "<option value=\"\">--Select One--</option>\n";
foreach ($regList as $att1fp)

{
	$regOptions .= "<option value=\"{$att1fp}\"{$att1fp}</option>\n";
}

echo "<select name=\"att1fp\">";
echo $regOptions;
echo "</select>";

?>

<br />
<br />

 

Once I get the drop down menu working, I will return to working on the validation piece. 

 

mjdamato....I am learning a lot from you. Thank you!

 

You are missing a closing bracket for the opening OPTION tag when creating the options. The rendered HTML is being interpreted as empty options.

 

 

$regOptions .= "<option value=\"{$att1fp}\">{$att1fp}</option>\n";
//                                         ^--Add closing bracket here

Here is a fully working test page implementing the code I included above. I also implemented a feature to reselect the previously selected value. Give it a try.

 

<?php
//Create the list of values
$colorList = array('Red', 'Green', 'Blue', 'Yellow');

//Validate the input
if(isset($_POST['color']))
{
    if (!in_array($_POST['color'], $colorList))
    {
        $response = "You have selected an invalid option.";
    }
    else
    {
        $response = "You selected the color {$_POST['color']}";
    }
}
else
{
    $response = "No values have been posted yet.";
}

//Create the options
$options = "<option value=\"\">--Select One--</option>\n";
foreach ($colorList as $color)
{
    $selected = ($_POST['color']==$color) ? ' selected="selected"' : '';
    $options .= "<option value=\"{$color}\"{$selected}>{$color}</option>\n";
}
//Add an invalid option simulating a malicius user
$options .= "<option value=\"Car\">Car</option>\n";

?>
<html>
<body>

<?php echo $response; ?>
<br /><br />

<form method="POST">

Select a color:
<select name="color">
<?php echo $options; ?>
</select>
<br />
<button type="submit">Submit</button>
</form>

</body>
</html>

I can not believe I missed that typo!  I'm really embarrassed.  I MUST get better at spotting typos!

 

I went over your examples again and understand more each time I work on them.  I did get the validation to work with your help.

 

I must admit I still do not understand all of it but now that I have that piece working in my project, I will spend the next couple of days studying for better understanding. 

 

Thank you so much for your patience and help, mjdamato!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.