Page 1 of 1
help processing checkbox on html page
Posted: Thu Apr 15, 2004 7:58 am
by phptomc
Forgive a struggling PHP novice's question, but I have spent three days searching for the answer to this on forums and FAQs:
I have installed a free php form-handling script called aformmail.php which processes web forms and does a sendmail, and can then send an autoresponder and add the form data to a .csv file. It is all working fine.
I want to adapt the script so that it does the last two actions (autoresponder and adding to csv file) only if a checkbox called "optin" on my web page has been clicked. I have the value of the checkbox set to "yes". I cannot get this simple conditional to work. I've tried:
if ($optin == 'yes') {
auto_respond($form);
save_form($form);
}
and
if ($optin) {
auto_respond($form);
save_form($form);
}
and a million other syntax, but it either passes everything, or fails everything, when I test by checking or not checking the "optin" box.
Any help would be much appreciated.
Tom
Posted: Thu Apr 15, 2004 8:04 am
by magicrobotmonkey
print_r($optin) and see what it is!
value appears to be...
Posted: Thu Apr 15, 2004 8:10 am
by phptomc
Thanks:
the value appears to be "yes" when you check the box, and is not present when you don't. The form output that is emailed to me shows this is you DO tick the box:
The aFromMail form submitted:
Email subj...: join
Xlocation....: Australia
Optin........: yes
and this if you DON'T:
The aFromMail form submitted:
Email subj...: join
Xlocation....: Australia
Tom
Posted: Thu Apr 15, 2004 8:19 am
by JayBird
with checkboxes, a value is ONLY passed if the checkbox is selected. If you don't select it NOTHING is passed. You need some code like this to set $optin to something if it wasn't selected
Code: Select all
if (!isset($optin)) {
$optin = "No";
}
Mark
Posted: Thu Apr 15, 2004 9:04 am
by AnsonM
I suggest coding your own mail script

Much easier
Hardly worked with checkboxes...
I think its because you haven't set a value for the checkbox if it was ticked.. so it doesnt work..
You could try radio buttons

Posted: Thu Apr 15, 2004 9:29 am
by phptomc
[quote="Bech100"]with checkboxes, a value is ONLY passed if the checkbox is selected. If you don't select it NOTHING is passed. You need some code like this to set $optin to something if it wasn't selected
[syntax=php]if (!isset($optin)) {
$optin = "No";
}[/syntax]
Mark[/quote]
Mark,
Have just tried this, and everything passes the conditional check that now follows your assignment follows:
if ($optin == "No") {
redirect();
}
In other words, the script always thinks that $optin has not been passed.
This seems crazy, as I can print the value of $optin, and it appears to be "yes" when I've ticked the box.
The question is, why isn;t the script recognising that $optin == "Yes" when it is?
Tom
Posted: Thu Apr 15, 2004 9:31 am
by magicrobotmonkey
it might be something with the quotes or some such nonsense - try making it an int (0,1,2) or a boolean (true, false)
Posted: Thu Apr 15, 2004 9:32 am
by JayBird
can you show the whole code you are useing cos i am confused.
Also, please use the BBCODE to highlight your PHP, it makes it easier to read.
Mark
whole code
Posted: Thu Apr 15, 2004 9:40 am
by phptomc
As i say, I'm a real PHP/scripting novice, so not sure if you want the entire script (very long) posted here?
Meantime, below is the whole of the main function of the script. Incidentally, I have just changed the checkbox to radio buttons, assigning values of "yes" and "no" depending on which the user ticks. Now the value of "yes" or "no" is being passed through (I can see it when I print $optin), but still the code is failing everything: it is never recognising $optin as equal to "yes".
Code: Select all
function do_formmail(){
global $autoresponder_enabled, $database_enabled;
$form = get_form_data();
$errors = check_form($form);
if ($errors) {
display_errors($errors);
return;
}
send_mail($form);
// so the user's input is mailed to me in all cases
if ($optin == "Yes") {
auto_respond($form);
save_form($form);
}
// if they have opted-in, send the autoresponder and save to csv file
// otherwise redirect to my thankyou page (declared as $redirect_url)
redirect();
}
function redirect(){
global $redirect_url;
header("Location: $redirect_url");
exit();
}
Posted: Thu Apr 15, 2004 9:53 am
by magicrobotmonkey
lets see where you build the form
Posted: Thu Apr 15, 2004 10:20 am
by phptomc
magicrobotmonkey wrote:lets see where you build the form
Is this the correct portion of the script? (thanks)
Code: Select all
<?php
function get_form_data(){
global $REQUEST_METHOD;
global $HTTP_POST_VARS;
global $HTTP_GET_VARS;
$vars = ($REQUEST_METHOD == 'GET') ? $HTTP_GET_VARS : $HTTP_POST_VARS;
//strip spaces from all fields
foreach ($vars as $k=>$v) $vars[$k] = trim($v);
return $vars;
}
function check_form($vars){
global $referrers;
global $send_to;
global $subject;
global $HTTP_REFERER;
$errors = array();
// check from email set
if (!strlen($vars['email_from'])){
$errors[] = "<b>From Email address</b> empty";
} else if (!check_email($vars['email_from'])){
$errors[] = "<b>From Email address</b> incorrect";
}
if (!strlen($send_to) && !strlen($vars['email_to'])){
$errors[] = "<b>To Email</b> address empty (possible configuration error)";
} else if (!strlen($send_to) && !check_email($vars['email_to'])){
//if to email specified in form, check it and display error
$errors[] = "<b>To Email address</b> incorrect";
}
if (!strlen($vars['subject']) && !strlen($subject)){
$errors[] = "<b>Subject</b> empty (possible configuration error)";
}
foreach ($vars as $k=>$v){
// check for required fields (end with _req)
if (preg_match('/^(.+?)_req$/i', $k, $m) && !strlen($v)){
$field_name = ucfirst($m[1]);
$errors[] = "Required field <b>$field_name</b> empty";
}
// check for number fields (end with _num)
if (preg_match('/^(.+?)_num$/i', $k, $m) && strlen($v) && !is_numeric($v)){
$field_name = ucfirst($m[1]);
$errors[] = "Field <b>$field_name</b> must contain only digits or be empty";
}
// check for number & required fields (end with _reqnum)
if (preg_match('/^(.+?)_reqnum$/i', $k, $m) && !is_numeric($v)){
$field_name = ucfirst($m[1]);
$errors[] = "Field <b>$field_name</b> must contain digits and only digits";
}
}
?>
Posted: Thu Apr 15, 2004 10:22 am
by magicrobotmonkey
No, where's the form that the user submits - the html <form> </form> etc
Posted: Thu Apr 15, 2004 10:35 am
by phptomc
<FORM METHOD="POST" ACTION="
http://www.wine-pages.com/formmail.php">
<INPUT NAME="email_to" VALUE="
tom@wine-pages.com" TYPE="HIDDEN">
<INPUT NAME="email_subj" VALUE="competition entry" TYPE="hidden">
<INPUT NAME="thanks_url" VALUE="
http://www.wine-pages.com/cmpthank.shtml" TYPE="hidden">
Your e-mail address: INPUT NAME="email_from" size=25><BR>
<SELECT NAME="Xlocation"> <OPTION VALUE="Select">Select from List</OPTION> <OPTION VALUE="UK">UK</OPTION> <OPTION VALUE="USA">USA</OPTION> <OPTION VALUE="other">other</OPTION> </SELECT><BR>
<INPUT TYPE="radio" NAME="optin" VALUE="Yes">Yes INPUT TYPE="radio" NAME="optin" VALUE="No">No<BR>
If not already a member, would like to join my mailing list?<br>
<INPUT TYPE=SUBMIT value="Enter Competition"> <INPUT TYPE=RESET value="Clear Form"></font><BR>
</FORM>