Hi

I'm trying to practise with this form, which will eventually be part of a much larger form. What I'm trying to do is to validate my reg_agent field in which a user can choose whether they are an agent or private.

The validation is supposed to say that when a user clicks the radio button for Agent (which = Yes) then the reg_license field can not be left blank.

If the requirements are filled - then the data is inserted to the mysql table user_test.

What currently happens is:
If reg_agent = Yes and reg_license = blank - the correct error message comes up but the script continues and uploads the submitted form to the database anyway.

The script continues whatever the scenario.

I've tried lots of variations on :

if (!empty($reg_agent) || ($_POST['reg_agent'] == "Yes" && !empty($reg_license))) {

So I'm thinking that I've just been staring at this too long :0)

Any pointers or help would be gratefully received as always :-)

Many thanks

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p>Testing Form</p>
<p>
  <?php
  if (isset($_POST['submit'])) {
    $reg_agent = $_POST['reg_agent'];
    $reg_license = $_POST['reg_license'];
    $output_form = false;

    if ($_POST['reg_agent'] == "Yes" && empty($reg_license)) {
      // We know both $reg_agent AND $reg_license are blank 
      echo 'You need to enter a license number.<br />';
      $output_form = true;
}
 }
  else {
    $output_form = true;
  }
  
  if (!empty($reg_agent) || ($_POST['reg_agent'] == "Yes" && !empty($reg_license))) {
    $dbc = mysqli_connect("localhost", "xxxxxxx", "xxxxxxxx", "xxxxxxxxxx");
	 $query = "INSERT INTO user_test (reg_agent, reg_license) VALUES ('$reg_agent', '$reg_license')";
   
    
		mysqli_query($dbc, $query)
		or die('Error querying database.');
	  
	   // Confirm success with the user
	 
       ?>
You have successfully submitted your agent details:<br />
Your Agent Details are as follows: <br />
  Agent or Private: 
  <?php echo "$_POST[reg_agent]"?><br />
  Agent License No: 
  <?php echo "$_POST[reg_license]"?>
  
  <?php
	   
    mysqli_close($dbc);
   } 
  
  if ($output_form) {
?>  
</p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
		<p>
	    <label>Agent or Individual: </label> <br />
		<input type="radio" name="reg_agent" value="Yes" id="reg_agent_0" <?php if ($_POST["reg_agent"]=="Yes") echo "checked=\"checked\""; ?>/>
		Agent
		<label>
 		<input type="radio" name="reg_agent" value="No" id="reg_agent_1" <?php if ($_POST["reg_agent"]=="No") echo "checked=\"checked\""; ?>/>
  		Private</label><br />
		<label>Agent License No. : </label><br />
		<input name="reg_license" type="text" id="reg_license" value="<?php echo $reg_license; ?>" size="30" /><br />
        <input type="submit" name="submit" value="Submit" />
  </p>
</form>

<?php 
  }
?>

</body>
</html>

Recommended Answers

All 2 Replies

Try something like this:

if (isset($_POST['submit'])) {
  $reg_agent = $_POST['reg_agent'];
  $reg_license = $_POST['reg_license'];
 
  if ($_POST['reg_agent'] == "Yes" && !empty($reg_license)) {
    $dbc = mysqli_connect("localhost", "xxxxxxx", "xxxxxxxx", "xxxxxxxxxx");
    $query = "INSERT INTO user_test (reg_agent, reg_license) VALUES ('$reg_agent', '$reg_license')";
    mysqli_query($dbc, $query);
  } else {
    echo 'You need to enter a license number.<br />';
    $output_form = true;
  }
} else $output_form = true;

Yes great thanks! That works. Many thanks for the help :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.