Jump to content

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


SEVIZ

Recommended Posts

I have a dropdown with choices.  Inside I have titles for sections with a value of "none".  Is there a way via php that if value = none it errors on submit?

 

Most importantly is there a way to do this WITHOUT javascript? 

 

Thanks

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.