Forum Moderators: coopster

Message Too Old, No Replies

Getting variables of form checkboxes

         

GGR_Web

3:53 pm on May 18, 2009 (gmt 0)

10+ Year Member



Hi. I have a pre-existing form I need to add extra checkboxes to. I can't seem to get the existing proccessing script to accept anymore infomation though.

I'm still very much learning this:)

<?
if(!($make && $model && $engine && $Year && $from)){
?>
You have not entered Make, Model, Engine, Year or Email Address. Please click back button and enter.

<?
//Check email is valid
}elseif(!(ereg("^.+@.+\\..+$",$from))){
?>
<?=$from?> is not a valid email address. Please click back button and enter
<?
}else{

$emailmessage = "EBC Products Addition\n\n";
$emailmessage .= "Make : $make\nModel : $model\nEngine : $engine\nReg : $Reg\nYear : $Year\n";
$emailmessage .= "Spec : $Spec\nABS : $ABS\nEmail : $from\nGreenstuff : $Greenstuff\nRedstuff : $Redstuff\n";
$emailmessage .= "Turbo-Groove : $TurboGroove\nFront : $Front\nRear : $Rear\n";

if(mail("sales@example.com","EBC Product Addition Request","$emailmessage","From:$from","-fsales@example.com")){
//echo "work - email";
}else{
echo "email not worked";
}
?>
Thank you for your interest in :snip Products. It will help us to understand which products you would like to be made available on our sites.
<?
}
?>

d40sithui

9:24 pm on May 18, 2009 (gmt 0)

10+ Year Member



Unless this script is not the one accepting and processing $_POST/$_GET variables, then you should have no problem. Whatever fields you have on the form, use $_POST/$_GET as needed and save into your variables for later (when emailing).
It doesn't look like you're showing the full script here. Where is the part where you retrieve the new data?

GGR_Web

7:55 am on May 19, 2009 (gmt 0)

10+ Year Member



Sorry unless you mean:
<FORM METHOD="post" ACTION="notlisted_reply.php"> which is at the top of my plain vanilia feedback form I don't really know what you're asking for. I did think it odd there was not a reference to $_POST/$_GET as in my limited understanding that is what actually passes the variables over to the script. Maybe I should of checked the script actually works before attempting to modify it.

I think I know how to rewrite this. Maybe I should.

GGR_Web

2:26 pm on May 19, 2009 (gmt 0)

10+ Year Member



OK I have this script working and sending email to me.
How can I get checkbox values?

<?php

//create short variable names
$make=$_POST['make'];
$model=$_POST['model'];
$engine=$_POST['engine'];
$reg=$_POST['reg'];
$year=$_POST['Year'];
$type=$_POST['Spec'];
$abs=$_POST['ABS'];
$from=$_POST['from'];

//set up some static information
$toaddress = "example@example.com";

$subject = "Feedback from web site";

$mailcontent = "Make: ".$make."\n".
"Model: ".$model."\n".
"Engine Size: ".$engine."\n".
"Vehicle Registation: ".$reg."\n".
"Year Of Manufacture: ".$year."\n".
"Origin: ".$type."\n".
"ABS Equiped: ".$abs."\n".
"Email Address: ".$from."\n";

$fromaddress = "From: $from";

//invoke mail() function to send mail
mail("$toaddress","$subject","$mailcontent","$fromaddress")
?>

[edited by: GGR_Web at 2:27 pm (utc) on May 19, 2009]

rocknbil

3:47 pm on May 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<FORM METHOD="post" ACTION="notlisted_reply.php">

The default method of a form is get, but since you specified a method you will be looking in post.

var $checkboxes=Array ('a','b','c','d');

foreach ($checkboxes as $key) {
if (isset($_POST[$key])) {
// sanitize and process $_POST[$key]
}
}

A checkbox value will not be sent in $_POST/$_GET/$_REQUEST if it's not checked, so you only have to check if it's been set.

d40sithui

4:01 pm on May 19, 2009 (gmt 0)

10+ Year Member



Yeah the reason I asked that was because I thought those variables were passed from another processing script via $_SESSION or something. It doesn't look like it is, so you can go with rocknbill's guidance. Post again if you need further help.

GGR_Web

8:25 am on May 20, 2009 (gmt 0)

10+ Year Member



Rocknbil
I tried using your method, but I'm hitting a blank screen when I submit the form.
What do the 'values' in Array represent?
Similiar to checkbox[0], checkbox[1], ect?
Do I need to call $key with $key or $key[0]?

Why do I need a foreach function when it seems I can call values out of the array?

For the moment I've got
"Product Required: ".$product[0]." ".$product[1]." ".$product[2]."\n".
Which is semi-working, but isn't returning the first ticked value for some reason. I'll try putting in a hidden input to overcome that.

rocknbil

3:19 pm on May 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm hitting a blank screen when I submit the form.

Put at the top of your script:

error_reporting(E_ALL);
ini_set('display_errors', '1');

This will reveal other things, including non-fatal warnings, and help you code more strictly.

Why do I need a foreach function when it seems I can call values out of the array?

Example only, to demonstrate how you'd check for input values. Sure you could just pull the values. The foreach is useful if you want to make sure at least one is checked, or compare input against preset values for security reasons (i.e., any input for these values will be of the types I define, not some mySQL injection attempt, etc. . . )

The point is the values will only be present in $_POST (if your form method is post) if the boxes are checked.

Do I need to call $key with $key or $key[0]?

No, the previous example presumes your form output is this:

<input type="checkbox" name="a" id="a" value="1">
<input type="checkbox" name="b" id="b" value="2">
<input type="checkbox" name="c" id="c" value="3">
<input type="checkbox" name="d" id="d" value="4">

If anything besides what's posted, you'd do

var $a = $_POST['a'];
var $b = $_POST['b'];
.........