Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Add/Edit Script

Problems with Checkboxes

         

theriddla1019

7:02 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



I have a dynamic PHP script setting up the SQL add and edit query from post variables sent by the previous page. The script for the most part does,

foreach ($_POST as $p => $v){

//WEED OUT THESE FIELDS AND THE SUBMIT BUTTON
if($v!= 'SUBMIT'){

//FORMAT DATES FOR INPUT INTO QUERY
if(substr($p,0,4) == 'date'){
if($v!= ''){
$date_input = $v;
list($month, $day, $year) = split('[/.-]', $date_input);
$v = $year . "-" . $month . "-" . $day;}
}

$v = strtoupper($v);

#SET TO STATIC VARIABLE
${"$p"} = $v;
#END LOOP THROUGH POST VARIABLES
}

Which this script works great, but when changing a checkbox from on to off html does not post the checkbox as having a value for off so it will not pull over. Ive thought of just pulling the columns from the table in the script but then everything would have to be matched up and thats not a very
happy thought. Does anyone know some tips or tricks to get the off value of a checkbox through html or php to pull over through post? Or how to pull all the values from the previous page not just the post variables.

dcrombie

7:17 pm on Feb 23, 2005 (gmt 0)



$checkboxValue = (isset($checkboxValue) && $checkboxValue == "on") ? 1 : 0;

;)

theriddla1019

7:24 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



I think I have something similar and seems to be working,

$Select = "SELECT * FROM $table WHERE id=$key";
$Result = mysql_query($Select,$Link);
$Row = mysql_fetch_assoc($Result);
foreach ($Row as $column => $value){

if($column!= 'entry' && $column!= id){
#set variable with column name
$$column = $_POST[$column];

//WEED OUT THESE FIELDS AND THE SUBMIT BUTTON

//FORMAT DATES FOR INPUT INTO QUERY

if(substr($column,0,4) == 'date'){
if($$column!= ''){
$date_input = $$column;
list($month, $day, $year) = split('[/.-]', $date_input);
$$column = $year . "-" . $month . "-" . $day;}
}

$$column = strtoupper($$column);

#END LOOP THROUGH POST VARIABLES

//SET FIELD NAMES AND VALUES
if($$column!= ''){
$values .= "$column='" . $$column."',";
} else {
$values .= "$column=NULL,";}
}
}

$values = substr($values,0,strlen($values)-1);

$Query = "UPDATE $table SET $values WHERE id=$key";

echo $Query;

It just sets all the column names = to null that it doesnt find from the post script. Except the ones I tell it not to