Jump to content

PHP - MySQL


christo

Recommended Posts

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 ?)
Link to comment
https://forums.phpfreaks.com/topic/7358-php-mysql/
Share on other sites

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.

[code]$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

}[/code]
Link to comment
https://forums.phpfreaks.com/topic/7358-php-mysql/#findComment-26829
Share on other sites

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

if you don't like that you can just combine sford999 's code into a similar statement
[code]
if(($garage == "") || ($cellar == "") || ($garden == "") || ($poll == ""))
{
   echo "Please fill all fields";
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/7358-php-mysql/#findComment-26831
Share on other sites

[!--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 [b]php error[/b], then we need to see the code used to process the form data. If you are getting a [b]warning[/b] (not the same thing), you could consider setting the error warning display level lower. If you are getting a [b]database error[/b] 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.
Link to comment
https://forums.phpfreaks.com/topic/7358-php-mysql/#findComment-26833
Share on other sites

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

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

[b]----------------------- BELOW IS WHAT I POSTES REVIOUSLY BUT STILL APPLIES -----------------------[/b]

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 [b]value[/b] attribute instead. So if you rename you checkboxes to the following, such as [b]places[][/b] 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.
[code]<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>[/code]
Now to see which checkboxes have been ticked you use this code:
[code]foreach($_POST['places'] as $k => $v)
{
    echo "You have selected - " . $v . "<br />\n";
}[/code]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.
Link to comment
https://forums.phpfreaks.com/topic/7358-php-mysql/#findComment-26913
Share on other sites

  • 2 weeks later...

A big thanks to everybody who helped. I thought i'd start with the solution of [b]wildteen88[/b] 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 [b]wildteen88[/b] 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
Link to comment
https://forums.phpfreaks.com/topic/7358-php-mysql/#findComment-30040
Share on other sites

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.