Page 1 of 1

Checkboxes in a form

Posted: Fri Aug 14, 2009 1:15 pm
by MIOD
I have a small problem in a form. I have adapted a contact form for use as a questionnaire, but I have added two checkbox questions.

I have added the two questions ok, and they show up ok on my webpage. However, when the form is submitted, it is not including the results of the checkbox questions because I haven't changed the code from what it was (which was a simple text field). I know that I need to add some code that is something like "foreach" to go through each box and add the result to another field if it is checked, but I have no idea how to do it.

I'm not a developer, nor a web designer, just a bit of an amateur techie.

Can anyone help me? I asked a couple of designers and they wanted access to my site and my logins and so on, and I didn't really want to give anyone that much access.

This is my code for the bit that first explains the fields (sorry if the terminology I'm using is wrong):

Code: Select all

 
    // initalize vars
    $error = 0;
    $error_print = '';
    $message_sent = 0;
    $mail_to    = '';
    $to_contact = '';
 
    $name       = '';
    $title      = '';
    $company    = '';
    $website    = '';   
    $email      = '';
    $turnover   = '';
    $employees  = '';   
    $interested = '';
    $past       = '';
    $experience = '';
    $objectives = '';
    $learn      = '';   
    $hear       = '';           
    
    // add another field here like above
 
    $error_contact = '';
    $error_name    = '';
    $error_email   = '';
    // add another field here like above
 
    // process form now
    if (isset($_POST['action']) && ($_POST['action'] == 'send')) {
 
        // check all input variables
        $cid = ctf_clean_input($_POST['CID']);
        if(empty($cid)) {
           $error = 1;
           $error_contact = 'Selecting a contact is required.';
        }
        else if (!isset($contacts[$cid]['CONTACT'])) {
            $error = 1;
            $error_contact = 'Requested Contact not found.';
        }
        $mail_to    = ctf_clean_input($contacts[$cid]['EMAIL']);
        $to_contact = ctf_clean_input($contacts[$cid]['CONTACT']);
 
 
        $name    = ctf_name_case(ctf_clean_input($_POST['name']));
        $title    = ctf_clean_input($_POST['title']);
        $company    = ctf_clean_input($_POST['company']);
        $website    = ctf_clean_input($_POST['website']);
        $email   = strtolower(ctf_clean_input($_POST['email']));
        $turnover    = ctf_clean_input($_POST['turnover']);
        $employees   = ctf_clean_input($_POST['employees']);    
        $interested  = ctf_clean_input($_POST['interested']);
        $past = ctf_clean_input($_POST['past']);
        $experience = ctf_clean_input($_POST['experience']);
        $objectives = ctf_clean_input($_POST['objectives']);
        $learn  = ctf_clean_input($_POST['learn']);
        $hear   = ctf_clean_input($_POST['hear']);
   
And also this is the bit which says what goes in teh email:

Code: Select all

 
         if (!defined('PHP_EOL')) define ('PHP_EOL', strtoupper(substr(PHP_OS,0,3) == 'WIN') ? "\r\n" : "\n");
 
         $subj = "$sitename $subject";
 
         $msg =  "Sent from $sitename
 
    To: $to_contact
  
    From:
    Name: $name
    Title: $title
    Company: $company
    Website: $website
    Email: $email   
 
    Turnover: $turnover
    Employes: $employees
    What marketing activities are they interested in? $interested
    What marketing activities have they done in the past? $past
    What is their general experience of marketing? $experience
    What are their objectives for the workshop? $objectives
    What do they want to learn? $learn
    Where did they hear about MIOD? $hear 
    
    ";
 
and finally this is the bit that puts the question into my webpage:

Code: Select all

 
            <div class="title">
                    <label for="interested">Which marketing activities are you specifically interested in: </label>
            </div> '.echo_if_error($error_interested).'
            <div class="field">
                &nbsp;&nbsp;<input type="checkbox" value="Market research" name="interested[]" /> Market research<br />
                &nbsp;&nbsp;<input type="checkbox" value="Branding" name="interested[]" /> Branding<br />
                &nbsp;&nbsp;<input type="checkbox" value="PR" name="interested[]" /> PR (public relations) <br />
                &nbsp;&nbsp;<input type="checkbox" value="Databases and CRM" name="interested[]" /> Databases and CRM (customer relationship management) <br />
                &nbsp;&nbsp;<input type="checkbox" value="Advertising" name="interested[]" /> Advertising <br />
                &nbsp;&nbsp;<input type="checkbox" value="Direct marketing" name="interested[]" /> Direct marketing <br />
                &nbsp;&nbsp;<input type="checkbox" value="Newsletters" name="interested[]" /> Newsletters <br />
                &nbsp;&nbsp;<input type="checkbox" value="Exhibitions" name="interested[]" /> Exhibitions <br />
                &nbsp;&nbsp;<input type="checkbox" value="Events" name="interested[]" /> Events <br />
                &nbsp;&nbsp;<input type="checkbox" value="Literature" name="interested[]" /> Literature <br />
                &nbsp;&nbsp;<input type="checkbox" value="Websites" name="interested[]" /> Websites <br />
                &nbsp;&nbsp;<input type="checkbox" value="Sponsorship" name="interested[]" /> Sponsorship <br />
                &nbsp;&nbsp;<input type="checkbox" value="Loyalty" name="interested[]" /> Loyalty programmes <br />
                &nbsp;&nbsp;<input type="checkbox" value="Referral" name="interested[]" /> Referral schemes <br />
                &nbsp;&nbsp;<input type="checkbox" value="Internal" name="interested[]" /> Internal marketing<br />
                &nbsp;&nbsp;<input type="checkbox" value="Promotions" name="interested[]" /> Promotions <br />
                &nbsp;&nbsp;<input type="checkbox" value="All" name="interested[]" /> All<br />
                &nbsp;&nbsp;<input type="checkbox" value="None" name="interested[]" /> None<br />
            </div>
 
 
 
<br />  
            <div class="title">
                    <label for="past">Which marketing activities have you undertaken in the past: </label>
            </div> '.echo_if_error($error_past).'
            <div class="field">
                &nbsp;&nbsp;<input type="checkbox" value="Market research" name="past[]" /> Market research<br />
                &nbsp;&nbsp;<input type="checkbox" value="Branding" name="past[]" /> Branding<br />
                &nbsp;&nbsp;<input type="checkbox" value="PR" name="past[]" /> PR (public relations) <br />
                &nbsp;&nbsp;<input type="checkbox" value="Databases and CRM" name="past[]" /> Databases and CRM (customer relationship management) <br />
                &nbsp;&nbsp;<input type="checkbox" value="Advertising" name="past[]" /> Advertising <br />
                &nbsp;&nbsp;<input type="checkbox" value="Direct marketing" name="past[]" /> Direct marketing <br />
                &nbsp;&nbsp;<input type="checkbox" value="Newsletters" name="past[]" /> Newsletters <br />
                &nbsp;&nbsp;<input type="checkbox" value="Exhibitions" name="past[]" /> Exhibitions <br />
                &nbsp;&nbsp;<input type="checkbox" value="Events" name="past[]" /> Events <br />
                &nbsp;&nbsp;<input type="checkbox" value="Literature" name="past[]" /> Literature <br />
                &nbsp;&nbsp;<input type="checkbox" value="Websites" name="past[]" /> Websites <br />
                &nbsp;&nbsp;<input type="checkbox" value="Sponsorship" name="past[]" /> Sponsorship <br />
                &nbsp;&nbsp;<input type="checkbox" value="Loyalty" name="past[]" /> Loyalty programmes <br />
                &nbsp;&nbsp;<input type="checkbox" value="Referral" name="past[]" /> Referral schemes <br />
                &nbsp;&nbsp;<input type="checkbox" value="Internal" name="past[]" /> Internal marketing<br />
                &nbsp;&nbsp;<input type="checkbox" value="Promotions" name="past[]" /> Promotions <br />
                &nbsp;&nbsp;<input type="checkbox" value="All" name="past[]" /> All<br />
                &nbsp;&nbsp;<input type="checkbox" value="None" name="past[]" /> None<br />
            </div>
 
 
Many thanks in advance to anyone who can help me at all.

Claire

Re: Checkboxes in a form

Posted: Sat Aug 15, 2009 9:39 pm
by yacahuma
your data will come as an array. So you cannot treat it as text.

Code: Select all

 
<?
if (isset($_POST['submit']))
  print_r($_POST['data']);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form action="test1.php" method="post">
<input type="checkbox" name="data[]" value="1" />
<input type="checkbox" name="data[]" value="2" />
<input type="checkbox" name="data[]" value="3" />
<input type="submit" name="submit" />
</form>
</body>
</html>
 

Re: Checkboxes in a form

Posted: Sat Aug 15, 2009 10:28 pm
by aceconcepts
Once you have implemented the suggestions of yacahuma your checkbox elements will be POSTED as an array assuming the form of:

Code: Select all

 
//[key]=>value
Array([0]=>1, [1]=>2, [2]=>3)
 
Arrays always set their intial key as 0 unless directed to do otherwise. There are different ways to loop through an array but foreach() certainly seems to be one of the most popular.

A foreach() loop can be used in the following way for the array example above:

Code: Select all

 
foreach($checkbox as $value){
echo $value . "\n";
}
//this will output:
//1
//2
//3
 

Re: Checkboxes in a form

Posted: Sun Aug 16, 2009 8:36 am
by MIOD
Thank you both - I think I understand what you're saying (certainly I understand what yacahuma says - I knew that anyway). But I don't really understand how to implement your answers.

I guess I need to pay someone to actually make the changes for me.

Does anyone on here do this kind of work, or if not, do you know anyone who does?

Re: Checkboxes in a form

Posted: Sun Aug 16, 2009 11:36 am
by yacahuma
if your checkboxes are the only problem, you should keep trying. You know how to get the value from the checkbox, what do you want to do with it now?

Re: Checkboxes in a form

Posted: Sun Aug 16, 2009 3:04 pm
by MIOD
Thank you - I really appreciate it - but I am way out of my depth. I don't know on what lines to put what you've said, nor how to change them to fit my own fields. I've not done any PHP before, so it's all a bit too much in at the deep end for me and is frazzling my brain!

It's very kind of you, but I really think I need to get someone to actually do it for me.

Claire

Re: Checkboxes in a form

Posted: Sun Aug 16, 2009 7:03 pm
by yacahuma
Can you tell me exactly what you need to do?

Re: Checkboxes in a form

Posted: Mon Aug 17, 2009 4:09 am
by MIOD
The only bit about the whole document/page that is not working is that for the two checkbox questions (that you can see in my third section of code I posted), when the email is sent to me, it is not including the inforamtion about which boxes have been checked. Instead it just says "Array". Everything else about the form and the page works perfectly.

I adapted the form from a simple text box only contact form, and worked out how to add the checkbox questions and also some drop down questions. The code for the checkbox questions has not been adapted at all, so those fields ('past' and 'interested') are still being handled as if they were text boxes. Hence why the info isn't being output to the email.

I just need to adapt the code in one or two places so that the output of the checkboxes can be included in the email that is sent to me.

Re: Checkboxes in a form

Posted: Mon Aug 17, 2009 8:26 am
by yacahuma

Code: Select all

 
<?
if (isset($_POST['data']))
{
    $validValues = array('1'=>'Option 1',  '2'=>'Option 2', '3'=>'Option 3');
    //get a nice text from the option value and make sure is a valid value, not some user junk                       
    $sel = array_intersect(array_keys($validValues), $_POST['data']);
    //create a comma separated string
    $output = array();
    foreach($sel as $val)
        $output[] = $validValues[$val]; 
    $outputStr = 'Your selection is ' . implode (',',$output);
    echo $outputStr;
 
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form action="test1.php" method="post">
<input type="checkbox" name="data[]" value="1" />
<input type="checkbox" name="data[]" value="2" />
<input type="checkbox" name="data[]" value="3" />
<input type="submit" name="submit" />
</form>
</body>
</html>
 
 

Re: Checkboxes in a form

Posted: Mon Aug 17, 2009 8:30 am
by yacahuma
In your case you can just send the values since they are ready to print.

Code: Select all

 
if (isset($_POST['data']))
{
 
    $outputStr = 'Your selection is ' . implode (',', $_POST['data']);
    echo $outputStr;
 
}