Jump to content

[SOLVED] If choice = none in dropdown error on submit?


SEVIZ

Recommended Posts

Sure there is as long as the submit page is the same as the page with the form elements.

Like this

 

<?php 
if(isset($_POST['confirm'])){
if($_POST['mycolor'] <> "none"){
echo $_POST['mycolor'];
}else{
echo "Please select a color";
}
}
?>
<html>
<body>
<form name="frm" methord="post">
<select name="mycolor">
<option value="none">Select a Color</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
<input type="submit" name="confirm" value="Send"/>
</form>
</body
</html>

 

if(isset($_POST['submit'])) // this is if you have a submit button with the 'name' attribute of 'submit'
{
  if($_POST['whatever'] == 'none')
  {
    $errors = TRUE;
  }
  if(!$errors)
  {
    //do the rest of your form processing
  }
}

 

The first 'if' statement checks if the form has been submitted. Then $errors is set to false, since there aren't any. After that, a check is made to see if the dropdown's value is equal to none. If it is, you have errors, and as such, $errors is set to true. Since $errors is true, the rest of the processing will not be done.

Yes.

 

<?php
  if($_POST['whatever'] == 'none')
  {
    session_start();
    $_SESSION['errors'] = 'Whatever was empty';
    header('Location: page/with/the/form.php');
  }
?>

 

Then on the page with the form:

<?php
  session_start();
  if(isset($_SESSION['errors']))
  {
    echo $_SESSION['errors'];
    unset($_SESSION['errors']);
  }
  // output form
?>

 

Note: you will actually have to use session_start() at the top of your document before any output, and you will probably want to add some html to the errors and stuff, but the basic logic is there.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.