Page 1 of 1

Checkboxes, not using $_POST

Posted: Fri Jan 15, 2010 8:25 am
by tez.grant
Hey,
sorry to say this is my first php project but i am used to using actionscript so i'm not new to scripting languages. i don't seem to be the only one who has trouble with checkboxes

the data input into the text fields is uploaded to the MYSQL database fine but the checkbox values are not, there is no obvious reason i can see below is the entire code from the file which is intended to add data for a new venue to the database, basic i know but functionality comes first.

Code: Select all

 
 
<?php 
$hostname = "localhost";
$db_user = "********";
$db_password = "*******";
$database = "********";
$db_table = "********";
 
//connect to mysql
$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db); 
?> 
<html> 
<head> 
<title>test venue form</title> 
</head> 
<body> 
 
<?php 
if (isset($_REQUEST['Submit'])) { 
# THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE 
$sql = "INSERT INTO $db_table(name,summary,email,location,min_guests,max_guests,price,festive,themed,party_types,key_features,details,optional_extras,description,venue_image,enabled)
 values ('".mysql_real_escape_string(stripslashes($_REQUEST['name']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['summary']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['email']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['location']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['min_guests']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['max_guests']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['price']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['festive']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['themed']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['party_types']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['key_features']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['details']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['optional_extras']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['description']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['venue_image']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['enabled']))."')";
if($result = mysql_query($sql ,$db)) { 
 
echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="http://www.webune.com/images/headers/default_logo.jpg"'; 
 
} else { 
echo "ERROR: ".mysql_error(); 
} 
} else { 
?> 
 
 
<form method="post" action=""> 
Name:
<br> 
<input type="text" name="name"> 
<br> 
Summary:
<br> 
<input type="text" name="summary"> 
<br>
Email:
<br> 
<input type="text" name="email"> 
<br> 
Location:
<br> 
<input type="text" name="location"> 
<br> 
Guests Min:
<br> 
<input type="text" name="min_guests"> 
<br> 
Guests Max:
<br> 
<input type="text" name="max_guests"> 
<br> 
Price:
<br> 
<input type="text" name="price"> 
<br> 
Festive:
<br>
<input type="checkbox" name="festive" value="1">
<br> 
Themed:
<br> 
<input type="checkbox" name="themed" value="1">
<br> 
Party Types:
<br> 
<input type="text" name="party_types"> 
<br> 
Key Features:
<br> 
<input type="text" name="key_features"> 
<br> 
Details:
<br> 
<input type="text" name="details"> 
<br> 
Optional Extras:
<br> 
<input type="text" name="optional_extras"> 
<br> 
Description:
<br> 
<input type="text" name="description"> 
<br> 
Venue Image:
<br> 
<input type="text" name="venue_image"> 
<br> 
Enabled:
<br> 
<input type="checkbox" name="enabled" value="1">
<br> 
<br> 
<input type="submit" name="Submit" value="Submit"> 
</form> 
<br>
<br>
<form>
<input type="button" value="Back" onClick="parent.location='http://www.teztez.co.uk/myparty/newentry.html'">
</form>
 
<?php 
} 
?> 
 
</body> 
</html> 
 
 
any help you could give me would be fantastic, the deadline is looming!

many thanks :banghead:

- Tez Grant

Re: Checkboxes, not using $_POST

Posted: Fri Jan 15, 2010 9:50 am
by flying_circus
I apologize for not having a bit more time to type up a more thorough reply, but with checkboxes, you need to check if they are set.

Consider the following code:

Code: Select all

<?php
  if(isset($_POST['1']))
  {
    # Checkbox 1 is set (checked)
      //do something...
  }
  else
  {
    # Checkbox 1 is not set (unchecked)
      //do something...
  }
?>
 
<form method="post">
  <input type="checkbox" name="1" value="MyVal" />
  <input type="submit" />
</form>
An easy way to diagnose problems like this is to use var_dump($_POST) or print_r($_POST)

Re: Checkboxes, not using $_POST

Posted: Fri Jan 15, 2010 10:16 am
by tez.grant
Brilliant,
I played with it and now it works fine! cheers dude!

my next issue is (virtually same code as above) i need to use drop down menus with the values predefined in the database as SET fields so that if a new value is put into the set options it will update all the html forms to display it automatically, sadly the set fields are a mystery to me.

i have no idea how to have the appropriate data display in the drop down menus or enter a new set value through a php/html form.

does anyone have something that could shove me in the right direction?

cheers for your patience!
- Tez Grant

Re: Checkboxes, not using $_POST

Posted: Fri Jan 15, 2010 6:18 pm
by thinsoldier

Code: Select all

 
<? ... ... ...
'".mysql_real_escape_string(stripslashes($_REQUEST['summary']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['email']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['location']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['min_guests']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['max_guests']))."',
?>
 
 
wtf? Needs less copy/paste

Code: Select all

 
<?
// my page
$dirty = $_REQUEST;
$clean = stringCleanArray($dirty);
 
// If your source array happens to be arranged in the same order as your table fields:
// convert clean array to 1 big string with [','] sandwiched between each array value
// The very first and very last quote will be handled in the $sql VALUES line
$values = implode("','", $clean);
 
$sql = "INSERT INTO $db_table(name,summary,email,location,min_guests,max_guests,
price,festive,themed,party_types,key_features,details,optional_extras,
description,venue_image,enabled)
 VALUES ('$clean') ";
?>

Code: Select all

 
<?
// my Library
function stringCleanArray($array)
{
    foreach($array as $key=>$value)
    { $clean[$key] = stringClean($value); }
    return $clean;
}
 
function stringclean($string)
{
    $string = stripslashes($string);
    $string = mysql_real_escape_string($string);
    return $string
}
 
?>

Code: Select all

<?
// Because it's rare that you source request array will be arranged in exactly the same field order as your database
// I prefer the other mysql syntax so I can list fields & values in any order without issues.
 
// if your source array keys happen to match your table field names:
$dirty = $_REQUEST;
$clean = stringCleanArray($dirty);
 
foreach($clean as $field=>$value)
{
    $pairedUp[] = " `$field`='$value' ";
}
 
// condense pairedUP array into a long string with [comma space] 
// sandwiched between each field=value pair
 
$pairedUp = implode(' , ' , $pairedUp);
 
$sql = "INSERT INTO table SET " . $pairedUp;
 
// example:
// INSERT INTO table SET `location`='John\\'s Castle' , `min_guests`='1' , `max_guests`='3'
?>

Re: Checkboxes, not using $_POST

Posted: Fri Jan 15, 2010 6:32 pm
by thinsoldier
tez.grant wrote:Brilliant,
I played with it and now it works fine! cheers dude!

my next issue is (virtually same code as above) i need to use drop down menus with the values predefined in the database as SET fields so that if a new value is put into the set options it will update all the html forms to display it automatically, sadly the set fields are a mystery to me.

i have no idea how to have the appropriate data display in the drop down menus or enter a new set value through a php/html form.
- Tez Grant
What's a SET field?

If by drop down menu you mean <select> and by values you mean <option> :

Code: Select all

<?
// pseudo-code
$globalOptions = doSqlQuery('SELECT my special options from database and return them as an array');
 
$optionTags = ''; // empty string to start with. concatenate in foreach loop
foreach($globalOptions as $value)
{ $optionTags .= '<option>' . $value . '</option>' . "\n";  }
// \n gives you new lines if View Source html. Makes for nicer reading of generated html
?>
 
<select name="lizard_types">
<? echo $optionTags ?>
</select>
I suggest making a function for yourself to easily pass in an array and have it spit out a select tag with the array converted to option tags. Select form tag gets used a lot!

Re: Checkboxes, not using $_POST

Posted: Fri Jan 15, 2010 11:16 pm
by SimpleManWeb
I believe the "SET" you are referring to is what PHP calls "selected". Here's how you set that up.

Code: Select all

 
   <select name="Test" id="Test">
     <option value="1">Yes</option>
     <option value="2">No</option>
     <option value="3" selected>Maybe</option>
   </select>
 


Just put "Selected" inside the opening of your option tag and you will be in business.