Jump to content

[SOLVED] Problem with PHP and combo boxes


lucy

Recommended Posts

Ive written a form, which has a script, which is meant to check weather all of the combo boxes have "something in", if not, a message is echoed to the screen, along with the form, to say fill in all of the fields.

 

The problem is, its not working. Please help me.

 

<form name="form1" method="post" action="cal.php">
<table width="750" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="159">Service:</td>
    <td width="10"> </td>
    <td width="583"><select name="service" id="service">
      <option> </option>
      <option>4</option>
      <option>4</option>
      <option>4</option>
      <option>4</option>
      <option>Valuation</option>
    </select></td>
  </tr>
  <tr>
    <td>Purchase price:</td>
    <td> </td>
    <td><select name="cost" id="cost">
      <option>                      </option>
      <option>£50,000 or less</option>
      <option>50,000.01 - £80,000</option>
      <option>£80,000.01 - £120,000</option>
      <option>£120,000.01- £150,000</option>
      <option>£150,00.01 - £200,000</option>
      <option>£200,000.01 - £250,000</option>
      <option>£250,000.01 - £300,000</option>
      <option>£300,000.01 - £350,000</option>
      <option>£350,000.01 - £400,000</option>
      <option>£400,000.01 or more</option>
    </select></td>
  </tr>
  <tr>
    <td>Number of bedrooms:</td>
    <td> </td>
    <td><select name="beds" id="beds">
      <option> </option>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
      <option>6 or more</option>
    </select></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td><input type="submit" name="submit" id="submit" value="Calculate" ></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
</form>

<?php
   $service=$_POST['service'];
   $cost=$_POST['cost'];
   $beds=$_POST['beds'];
   
      
   if ( isset($_POST['service']) && isset($_POST['cost']) && isset($_POST['beds']) )
   {   
   if ($service == "Valuation"){
	   echo "VALLUATION";
    }
   }
   else {
   echo "Please fill in all fields";
   }
?>

 

Please can someone please help me with this.

 

Thanks,

Lucy

Link to comment
https://forums.phpfreaks.com/topic/170297-solved-problem-with-php-and-combo-boxes/
Share on other sites

for form is submitting to cal.php, which is where the form is located, along with the script.

 

The script works fine when the page is first loaded, i.e. all combo boxes are blank and is says "please fill in all fields" but then when one field is filled in and submitted, this text goes away, which it should not, as ALL combo boxes are not filled in, and therefore should stay on screen.

 

Thanks,

Lucy

You need to check for the value of the select menu... When you add a select box with a blank 1st option, it still IS being set... so

if(isset())

is always true. It is just being set iwth a blank value.

 


<form name="form1" method="post" action="cal.php">
<table width="750" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="159">Service:</td>
    <td width="10"> </td>
    <td width="583"><select name="service" id="service">
      <option> </option>
      <option>4</option>
      <option>4</option>
      <option>4</option>
      <option>4</option>
      <option>Valuation</option>
    </select></td>
  </tr>
  <tr>
    <td>Purchase price:</td>
    <td> </td>
    <td><select name="cost" id="cost">
      <option>                      </option>
      <option>£50,000 or less</option>
      <option>50,000.01 - £80,000</option>
      <option>£80,000.01 - £120,000</option>
      <option>£120,000.01- £150,000</option>
      <option>£150,00.01 - £200,000</option>
      <option>£200,000.01 - £250,000</option>
      <option>£250,000.01 - £300,000</option>
      <option>£300,000.01 - £350,000</option>
      <option>£350,000.01 - £400,000</option>
      <option>£400,000.01 or more</option>
    </select></td>
  </tr>
  <tr>
    <td>Number of bedrooms:</td>
    <td> </td>
    <td><select name="beds" id="beds">
      <option> </option>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
      <option>6 or more</option>
    </select></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td><input type="submit" name="submit" id="submit" value="Calculate" ></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
</form>

<?php
   $service=$_POST['service'];
   $cost=$_POST['cost'];
   $beds=$_POST['beds'];
   
      
   if ( isset($_POST['service']) && $_POST['service'] != "" && isset($_POST['cost']) && $_POST['cost'] != "" && isset($_POST['beds']) && $_POST['beds'] != "")
   {   
      if ($service == "Valuation"){
         echo "VALLUATION";
       }
   }
   else {
      echo "Please fill in all fields";
   }
?>

I fixed it. Alll i did was add value tags to the combo options, and say it combo = -1 then its not selected. Code below:

 

<?php

  
   $service=$_POST['service'];
   $cost=$_POST['cost'];
   $beds=$_POST['beds'];
   
      
   if ( $service != "-1" && $cost !="-1" && $beds !="-1"){
   echo "all selected";
   }
   else {
   echo "please select all fields";
   }
?>

<form name="form1" method="post" action="cal.php">
<table width="750" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="159">Service:</td>
    <td width="10"> </td>
    <td width="583"><select name="service" id="service">
      <option value="-1"> </option>
      <option value="1">4</option>
      <option value="2">4</option>
      <option value="3">4</option>
      <option value="4">4</option>
      <option value="5">Valuation</option>
    </select></td>
  </tr>
  <tr>
    <td>Purchase price:</td>
    <td> </td>
    <td><select name="cost" id="cost">
      <option value="-1">                      </option>
      <option value="1">£50,000 or less</option>
      <option value="2">50,000.01 - £80,000</option>
      <option>£80,000.01 - £120,000</option>
      <option>£120,000.01- £150,000</option>
      <option>£150,00.01 - £200,000</option>
      <option>£200,000.01 - £250,000</option>
      <option>£250,000.01 - £300,000</option>
      <option>£300,000.01 - £350,000</option>
      <option>£350,000.01 - £400,000</option>
      <option>£400,000.01 or more</option>
    </select></td>
  </tr>
  <tr>
    <td>Number of bedrooms:</td>
    <td> </td>
    <td><select name="beds" id="beds">
      <option value="-1"> </option>
      <option value="1">1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
      <option>6 or more</option>
    </select></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td><input type="submit" name="submit" id="submit" value="Calculate" ></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
</form>

 

thanks

Lucy

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.