Jump to content


Photo

PHP - MySQL


  • Please log in to reply
5 replies to this topic

#1 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 13 April 2006 - 11:21 PM

i have 4 checkboxes unchecked by default, when they are checked they send strings ...

# script1.php send info to script2.php
<input type="checkbox" name="garage"value="Garage"unchecked> </input></td>
<input type="checkbox" name="cellar"value ="Cellar"unchecked> </input></td>
<input type="checkbox" name="garden"value="Garden"unchecked> </input></td>
<input type="checkbox" name="pool"value ="Pool"unchecked> </input></td>

so far everything perfect i get the values with this lines

#script2.php
$garage = $HTTP_POST_VARS['garage'];
$cellar = $HTTP_POST_VARS['cellar'];
$garden = $HTTP_POST_VARS['garden'];
$pool = $HTTP_POST_VARS['pool'];

and enter it in the DB after

Now the problem is when the boxes aren't checked they send no values to script2.php and that gives me an error.

I think the easyest would be to do something so from script1.php the boxes send 2 values (checked or unchecked) Can somebody suggest something ?

P.S I tried some if ..else but didn't seem to work (perhaps there was errors in the code ?)

#2 sford999

sford999
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 14 April 2006 - 01:37 AM

if/else would be easiest way of doing it, I use the same sort of code as below for several of my pages on my own site.

$garage = $_POST['garage'];
$cellar = $_POST['cellar'];
$garden = $_POST['garden'];
$pool = $_POST['pool'];

if($garage == "")
{
echo "insert error message here";
}
if($cellar == "")
{
echo "insert error message here";
}
if($garden == "")
{
echo "insert error message here";
}
if($pool == "")
{
echo "insert error message here";
}
else
{

// rest of the code here

}


#3 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 14 April 2006 - 02:05 AM

you could also do something similar to this :
if(empty($garage) || empty($cellar) || empty($garden) || empty($pool))
{
   echo "Please fill all fields";
}

if you don't like that you can just combine sford999 's code into a similar statement
if(($garage == "") || ($cellar == "") || ($garden == "") || ($poll == ""))
{
   echo "Please fill all fields";
}

got a problem? Google helps many of those in need

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 April 2006 - 02:08 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Now the problem is when the boxes aren't checked they send no values to script2.php and that gives me an error.[/quote]
If you are getting a php error, then we need to see the code used to process the form data. If you are getting a warning (not the same thing), you could consider setting the error warning display level lower. If you are getting a database error when you try to add the data to the database, we need to see the database table definitions as well as the sql query being used.
Legend has it that reading the manual never killed anyone.
My site

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 14 April 2006 - 09:45 AM

EDIT Ooh crap! I didn't rerad your post correclty. What I would recommend you to do instead then is just change:
$garage = $_POST['garage'];
$cellar = $_POST['cellar'];
$garden = $_POST['garden'];
$pool = $_POST['pool'];
to the following:
$garage = (isset($_POST['garage']) ? $_POST['garage'] : '');
$cellar = (isset($_POST['cellar'] ? $_POST['cellar']: '');
$garden = (isset($_POST['garden'] ? $_POST['garden'] : '');
$pool = (isset($_POST['pool'] ? $_POST['pool']  : '');

This should stop the errors from appearing, as it checks whether for example the cellar ckeckbox has been checked, if has it'll set the value of $cellar to the value $_POST['cellar'], but if $_POST['celler'] isn;t set it give the value of $celler the value of nothing.

Hope that helps

----------------------- BELOW IS WHAT I POSTES REVIOUSLY BUT STILL APPLIES -----------------------

The problem is checkboxes are treated differently when submiitting a checkbox compared to a normal textarea. When you name your checkboxes you need to name all the same by give them seperate values wiith the value attribute instead. So if you rename you checkboxes to the following, such as places[] and dont forget to put this - [] - at the of the name too, that way you checkbox data gets sent as an array within the $_POST['places'] array.
<input type="checkbox" name="places[]" value="Garage" /></td>
<input type="checkbox" name="places[]" value ="Cellar" /></td>
<input type="checkbox" name="places[]" value="Garden" /></td>
<input type="checkbox" name="places[]" value ="Pool" /></td>
Now to see which checkboxes have been ticked you use this code:
foreach($_POST['places'] as $k => $v)
{
    echo "You have selected - " . $v . "<br />\n";
}
What this code does is loop through the $_POST['places'] array and then echoes out which checkboxes you have submitted.

Only checked checkboxes get submitted. Unchecked checkboxes dont get submitted.

Hope that helps.

#6 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 24 April 2006 - 10:30 AM


A big thanks to everybody who helped. I thought i'd start with the solution of wildteen88 because i had tried before the "if" and "||" statements but i was getting an error since at my request i was getting always "" null value -> error.

Doing echo of an "error msg" or "fill up" the wouldn't help because its a checkbox...
In short i just replace with the your solution wildteen88 an taraaam no errors...well i don't know yet if it fully works because a get another error trying to upload an image to the DB, but i'll let you all know when i'm over it ;-)

christos




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users