View Full Version : Looping through form result for values !!
Hi everyone,
I have the following piece of code in one of my scripts. It checks if the value of $_post[Sf_harness] is 'yes' if it is it sets query2 as something. If it is not 'yes' then it sets query2 as something else.
Basically this works ok at the moment.
But I want it to check more of the post values i.e $_post[Sf_harness] and $_POST[Sf_mask] and more etc
I am not sure how I can do it though. I need it work out which ones are 'yes' and 'no'. I will obivously have some yes's and some no's.
Can anybody help me please ??
Thanks :thumbsup:
f ($_POST[Sf_harness] == "yes")
$query2 = "insert into products (Sf_harness) values (yes)";
else $query2 = "insert into products (Sf_harness) values (no)";
$result2 = mysql_query($query2);
firepages
11-22-2002, 02:59 PM
assuming your POST variables carry 'yes' or 'no' you dont need to if/else anything..
<?
$query2 = "insert into products (Sf_harness,Sf_mask) values ('$_POST[Sf_harness]','$_POST[Sf_mask]')";
?>
NOTE that you should be echoing $_POST['Sf_mask'] i.e. with single or double quotes, if you have PHP>4.2 you can do this..
<?
$query2 = "insert into products (Sf_harness,Sf_mask) values ('{$_POST['Sf_harness']}','{$_POST['Sf_mask']}')";
?>
the extra braces {} tell PHP to treat whatever is inside as an array value and you wont get a parse error OR do this
<?
$query2 = "insert into products (Sf_harness,Sf_mask) values ('".$_POST['Sf_harness']."','".$_POST['Sf_mask']."')";
?>
Hey firepages
Thanks fo those tips.
No. Unfortuately it doesn't happen like that. I had that to start with but found it didn't do what I wanted.
I have form which has check boxes for those post variables. The check box values are set to 'yes'. So when the checkbox is ticked the value sent is 'yes' but when left it sends nothing.
So that is why I need to say if 'yes' then put yes into database otherwise the checkbox has been left empty so put 'no' into the database.
Do you follow. I could do it with text fields obviously but for simplicity sakes on the user end I want them to be able to use the check box's.
Any ideas on what I can do ??
:thumbsup:
firepages
11-22-2002, 04:33 PM
well you could use radio buttons instead of checkboxes which let you assign a yes/no on/off value ..
else just cheat !
<?
$array=('Sf_harness','Sf_mask','Sf_etc');
foreach($array as $var){
if(!$_POST[$var]){
$_POST[$var]='no';
}
?>
then build the query as above.
Ok so I have been trying out your suggestion firpages.
The following code below is what I have at the moment. It is giving me a parse error on line 13 which is the array line. I have tried various things to fix it with no luck.
Do you know why I am getting a parse error ??
$array=('Sf_goggle', 'Sf_hardhat', 'Sf_gloves', 'Sf_mask', 'Sf_edefenders', 'Sf_harness')
foreach ($array as $item) {
if(!$_POST[$item] || $_POST[$item] == "") {
$_POST[$item]='no'; }
else {
$_POST[$item]='yes'; }
}
:thumbsup:
firepages
11-26-2002, 11:53 PM
sorry wap3 my bad (missing array(...)
<?
$array=array('Sf_goggle', 'Sf_hardhat', 'Sf_gloves', 'Sf_mask', 'Sf_edefenders', 'Sf_harness');
foreach ($array as $item) {
if((!$_POST[$item]) || ($_POST[$item] == "")) {
$_POST[$item]='no'; }
else {
$_POST[$item]='yes'; }
}
print_r($_POST);
?>
ps
if((!$_POST[$item]) || ($_POST[$item] == "")) {
would be much tidier as
if(isset($_POST[$item])){
Hey thanks Firepages
I probably should have spotted that but im still new to php so thankyou.
:thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.