Kuritsu
Kuritsu

Reputation: 35

How can my dropdown box contain validation for a null entry?

I'm trying to get my dropdown box in PHP validated so that if the value is Null, it will output an error. But I cannot seem to be able to get this working.

I've tried the solutions on other websites and also other answers on this website but they don't seem to be working.

<?php 
  include("config.php");
  $sql = "SELECT ID, Name, Surname, Address, TelNo, Email, Paid, Method, 
          Form, GMS, CanVolunteer FROM Tbl_Social";
  if(isset($_POST['form'])) {
    if($_POST['form'] == 'NULL') {
        echo 'Please select an option from the dropdown menu.';
    }
    elseif ($_POST['form'] == 'ID') {
        echo 'You have selected ID.';
    }
  }
?>  

<!DOCTYPE html>
<html>
  <div class="dropdown">
    How do you want to find a player?<br>
    <select name="form">
      <option value="">Select a choice </option>
      <option value="ID">ID</option>
      <option value="Name">Name</option>
      <option value="Surname">Surname</option>
      <option value="Address">Address</option>
      <option value="TelNo">Telephone Number</option>
      <option value="Email">Email</option>
    </select>
    <input type="text" name="varQuery">
    <input type="submit">
  </div>
</html>

I'm wanting the Webpage to display a message along the lines of "Please enter a value in the dropdown box" if the form is null.

Upvotes: 0

Views: 134

Answers (2)

Omer Tekbiyik
Omer Tekbiyik

Reputation: 4744

Just give a value Select a choice like

 <option  value="select">Select a choice </option>

And check if select is selected or not.

 if($_POST['form'] == 'select') {
    echo 'Please select an option from the dropdown menu.';

}
elseif ($_POST['form'] == "ID") {
    echo 'You have selected ID.';

}

By the way if you want to send POST

<form action ="POST">... <form>

is more useful.

All code :

<?php
if(isset($_POST['clickedButton'])) {

if($_POST['form'] == 'select') {
    echo 'Please select an option from the dropdown menu.';

}
elseif ($_POST['form'] == "ID") {
    echo 'You have selected ID.';

}}
?>  

<!DOCTYPE html>
<html>

<div class="dropdown">
How do you want to find a player?<br>
<form method ="POST" > 
<select name="form">
    <option  value="select">Select a choice </option>

    <option value="ID">ID</option>

    <option value="Name">Name</option>

    <option value="Surname">Surname</option>

    <option value="Address">Address</option>

    <option value="TelNo">Telephone Number</option>

    <option value="Email">Email</option>
    <input type="text" name="varQuery">
    <button type="submit" name ="clickedButton" >ENTERS </button>
 </select>
 </form>

Upvotes: 1

Dharman
Dharman

Reputation: 33247

isset() already checks if the variable exists and is not null. You can just combine your if statement into one:

if(!isset($_POST['form'])) {
   echo 'Please select an option from the dropdown menu.';
}
elseif ($_POST['form'] == 'ID') {
   echo 'You have selected ID.';
}

Upvotes: 0

Related Questions