Jump to content

Recommended Posts

 

I've got one that has me scratching my head, not sure exactly how to do this. I've got a drop down menu that I would like to consider mandatory. If it's not selected, I'd like to display an error on the page close to the drop-down, if it is selected, the form should post to another page.

 

I've tinkered with a little javascript which I couldn't figure out. I've used a switch to display different dropdowns and that works fine, I've just included one as an example. Here's what the form looks like:

 

 

<FORM NAME="CORDS" METHOD="POST" ACTION="checkmeout.php">
<INPUT TYPE="HIDDEN" NAME="product" VALUE="<?php echo $item3; ?>">
<INPUT TYPE="HIDDEN" NAME="price" VALUE="<?php echo $price; ?>">
<INPUT TYPE="HIDDEN" NAME="pagetype" VALUE="neck">

switch ($neck) {
case "A":
echo " 
<input type=\"hidden\" name=\"product[]\" value=\"$item3\">
<select name=\"product[]\"><option value=\"\">Select your cord or chain:</option>
<option value=\"18-inch silver chain\">18-inch Sterling silver box chain</option>
<option value=\"16-inch black cord\">16-inch black rubber cord</option>
<option value=\"18-inch black cord\">18-inch black rubber cord</option></select>";
break;
}
?>
<br><br>Include giftbox
<input type="checkbox" name="giftbox" value="giftbox"> 

<INPUT TYPE="SUBMIT" Value="Add to Cart"> </FORM>

Link to comment
https://forums.phpfreaks.com/topic/75070-form-validation-and-error-message/
Share on other sites

I find using

foreach($_POST as $post)
{

if($post==NULL){print 'You have left fields blank!';}

}

 

usually does the trick, however it will check all of the posted variable to check its contents isn't null (there is a way to make it specific but unless you need it don't bother... if you need it specific then just ask and I'll give you the longer piece for that)

If you only want to validate the pull down, You'll want to use something like the following:

if(!isset($_POST['product'][1]) || empty($_POST['product'][1]))
{
    echo 'You must select a product from the pulldown menu';
}

 

Where do you put the "if" statement -- at the top of the form, bottom, outside of the form tags.  Just curious.  Tinkering with your suggestion and haven't gotten it to work yet....

That code will go into the script which processes the form when it is submitted. Looking at your forms action tag the script which processing the form is checkmeout.php

Yes you can do this however you you'll need to change the forms action, so it submits to itself (the page the form is displayed on) rather than getting submitted to another page.

 

Example:

<?php

$error = null;

// form validation:
if(isset($_POST['name']) && empty($_POST['name']))
{
    $error[] = 'Please fill in the name field';
}

if(isset($_POST['age']) && empty($_POST['age']))
{
    $error[] = 'Please fill in the age field';
}


// display errors if there is any:
if(is_array($error))
{
    echo 'Please correct the following:<br />';
    echo '<ul><li>' . implode('</li><li>', $error) . '</li></ul>';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name: <input type="text" name="name" /><br />
Age: <input type="text" name="age" /><br />
<input type="submit" name="submit" value="Submit" />
</form>

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.