0

I am creating a form where the user selects check boxes of "services" that they might be interested in, then the form gets emailed to me. The problem is weather a box is checked or not, all of the results are being filled in. How do I properly do this?

There are about 25, but here is just a few:

    $services = array();
if($_POST['master_planning']) {$services[] = "Master planning";} 
if($_POST['snow_plowing']) {$services[] = "Snow plowing";} 
if($_POST['fine_gardening']) {$services[] = "Fine Gardening";} 
if($_POST['deer_protection']) {$services[] = "Deer Protection";} 

$interested = implode(", ", $services);

I know it is a very basic thing, but I can't seem to solve it on my own

HTML is like:

<li><label for="planting"><input type="checkbox" name="planting" id="planting" value="x" /> Planting</label></li>

Ok, I figured (I think) out why all the values are returning true, even if the checkboxes arent clicked, now how do I solve it....

I guess I should of mentioned that this is page 2 of the form (process.php), the actual form is index.php, and I am using an ajax script to pass values.

    <script type="text/javascript">
$(document).ready(function() {
    $('#submit').click(function () {        

        var name = $('input[name=name]');
        var phone = $('input[name=phone]');
        var email = $('input[name=email]');
        var master_plan = $('input[name=master_plan]');
        var front_foundation = $('input[name=front_foundation]');
        var backyard_plan = $('input[name=backyard_plan]');
        var specialty_garden = $('input[name=specialty_garden]');
        var lawn_cutting = $('input[name=lawn_cutting]');
        var lawn_plant_health_care = $('input[name=lawn_plant_health_care]');
        var organic_property_care = $('input[name=organic_property_care]');
        var seasonal_clean_ups = $('input[name=seasonal_clean_ups]');
        var pruning = $('input[name=pruning]');
        var fine_gardening = $('input[name=fine_gardening]');
        var deer_protection = $('input[name=deer_protection]');
        var snow_plowing = $('input[name=snow_plowing]');
        var planting = $('input[name=planting]');
        var walk = $('input[name=walk]');
        var terrace = $('input[name=terrace]');
        var wall = $('input[name=wall]');
        var outdoor_kitchen = $('input[name=outdoor_kitchen]');
        var fireplace = $('input[name=fireplace]');
        var driveway = $('input[name=driveway]');
        var fencing = $('input[name=fencing]');
        var pergola = $('input[name=pergola]');
        var swimming_pool = $('input[name=swimming_pool]');
        var irrigation = $('input[name=irrigation]');
        var lighting = $('input[name=lighting]');
        var grading_drainage = $('input[name=grading_drainage]');

        var newsletter = $('input[name=newsletter]');
        var comments = $('textarea[name=comments]');

        if (name.val()=='') {
            name.addClass('hightlight');
            return false;
        } else name.removeClass('hightlight');

        if (email.val()=='') {
            email.addClass('hightlight');
            return false;
        } else email.removeClass('hightlight');

        var data = 
        'name=' + name.val() + 
        '&phone=' + phone.val() + 
        '&email=' + email.val() + 
        '&master_plan=' + master_plan.val()+            
        '&front_foundation=' + front_foundation.val()+
        '&backyard_plan=' + backyard_plan.val()+
        '&specialty_garden=' + specialty_garden.val()+
        '&lawn_cutting=' + lawn_cutting.val()+
        '&lawn_plant_health_care=' + lawn_plant_health_care.val()+
        '&organic_property_care=' + organic_property_care.val()+
        '&seasional_clean_ups=' + seasional_clean_ups.val()+
        '&pruning=' + pruning.val()+
        '&fine_gardening=' + fine_gardening.val()+
        '&deer_protection=' + deer_protection.val()+
        '&snow_plowing=' + snow_plowing.val()+
        '&planting=' + planting.val()+
        '&walk=' + walk.val()+
        '&terrace=' + terrace.val()+
        '&wall=' + wall.val()+
        '&outdoor_kitchen=' + outdoor_kitchen.val()+
        '&fireplace=' + fireplace.val()+
        '&driveway=' + driveway.val()+
        '&fencing=' + fencing.val()+
        '&pergola=' + pergola.val()+
        '&swimming_pool=' + swimming_pool.val()+
        '&irrigation=' + irrigation.val()+
        '&lighting=' + lighting.val()+
        '&grading_drainage=' + grading_drainage.val()+

        '&newsletter=' + newsletter.val() + 
        '&comments='  + encodeURIComponent(comments.val());

        $('.text').attr('disabled','true');
        $('.loading').show();

        $.ajax({
            url: "process.php", 
            type: "GET", 
            data: data, 
            cache: false, 
            success: function (html) { 
                if (html==1) { 
                    $('.form').fadeOut('slow'); 
                    $('.done').fadeIn('slow'); 
                } else alert('Sorry, unexpected error. Please try again later.'); 
            } 
        });
        return false;
    });                         
}); 
</script>

Could this be why every result is displaying?

8
  • You are looking at your $_POST variables. The test you currently have is "does this variable exist". If that is what you intended, it suggests the problem is with your form, not the code you posted. Can you show a snippet of your form (again, with 4 rather than 25 options) that has the problem? Commented Feb 7, 2013 at 3:37
  • 1
    Your packet sniffer says...? Commented Feb 7, 2013 at 3:37
  • what do you mean all the results are being filled in? What sort of results are you getting? Commented Feb 7, 2013 at 3:38
  • meaning in the email that is sent "Interested in: Master planning, Snow plowing, Fine Gardening, Deer Protection" Even if I dont check any boxes Commented Feb 7, 2013 at 3:40
  • What do your boxes look like? Show us the relevant bit of the form. Commented Feb 7, 2013 at 3:43

1 Answer 1

0

The check you have is incorrect. Lets assume you have this HTML code for the checkbox inside your form:

<input type="checkbox" name="master_planning" value="yes" />

Then on your PHP you want do the check like this:

if(isset($_POST['master_planning']) && $_POST['master_planning'] == 'yes'){
    array_push($services, "Master planning"];        
}

The function array_push adds the given element or elements at the end of the array, so that would be an easy way to "dynamically" populates an array.

Sign up to request clarification or add additional context in comments.

2 Comments

What is $_POST['formWheelchair'] ?
he had a mistake i think ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.