Checkboxes causing email submission to goto 404

I’ve created a form (I’ve left parts out) that collects the user’s checkbox selections (which are created via WordPress) and emails the results back to me.

When I tick the hardware selections and submit the email comes through fine but when I tick any of the accessories and click submit it goes to a 404 page.

Read More

Can anyone see where the error is? I’ve created the two sets of checkboxes and results in the same way, just can’t see where the error is!

<form id="customisesystem" name="enquiry" method="POST" onSubmit="return formCheck(this);" action="<?php echo the_permalink(); ?>">
  <div id="customise-area">
    <?php $posts = get_field('options');
                            if( $posts ):
                            $items = 0;
                            foreach( $posts as $post): // variable must be called $post (IMPORTANT)
                                setup_postdata($post); ?>
    <div class="custom-option">
      <p><b>
        <?php the_title(); ?>
        </b></p>
      <br />
      <div>
        <p><?php echo the_field('description'); ?></p>
      </div>
      <?php $counter = 1; while(the_repeater_field('images')): ?>
      <?php if($counter <= 1) { ?>
      <img width="180" height="136" src="<?php the_sub_field('image'); ?>" alt="<?php the_title(); ?>" />
      <?php } ?>
      <?php $counter++; endwhile; ?>
      <p>
        <input type="checkbox" name="hardware[]" value="<?php the_title(); ?>">
        Select</p>
      <div class="clear"></div>
    </div>
    <?php $items++; endforeach;
                            wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
                            endif; ?>
  </div>
  <div id="customise-area">
    <?php $posts = get_field('accessories');
                            if( $posts ):
                            $items = 0;
                            foreach( $posts as $post): // variable must be called $post (IMPORTANT)
                                setup_postdata($post); ?>
    <div class="custom-option">
      <p><b>
        <?php the_title(); ?>
        </b></p>
      <br />
      <div>
        <p><?php echo the_field('description'); ?></p>
      </div>
      <?php $counter = 1; while(the_repeater_field('images')): ?>
      <?php if($counter <= 1) { ?>
      <img width="180" height="136" src="<?php the_sub_field('image'); ?>" alt="<?php the_title(); ?>" />
      <?php } ?>
      <?php $counter++; endwhile; ?>
      <p>
        <input type="checkbox" name="accessories[]" value="<?php the_title(); ?>">
        Select</p>
      <div class="clear"></div>
    </div>
    <?php $items++; endforeach;
                            wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
                            endif; ?>
  </div>
  <? if(isset($_POST['submit'])) { 

$to = "rob@domain.com";
$header = 'From: rob@domain.com';
$subject = "Domain: Quotation";
$enquiry_first_name = $_POST['enquiryfirstname'];
$enquiry_last_name = $_POST['enquirylastname'];
$enquiry_title = $_POST['enquirytitle'];
$enquiry_organisation = $_POST['enquiryorganisation'];
$enquiry_address = $_POST['enquiryaddress'];
$enquiry_country = $_POST['enquirycountry'];
$enquiry_email_address = $_POST['enquiryemailaddress'];
$enquiry_telephone = $_POST['enquirytelephone'];
$enquiry_additional_comments = $_POST['enquiryadditionalcomments'];
$enquiry_product = get_the_title();
if(!empty($_POST['hardware'])) {
    foreach($_POST['hardware'] as $check) {
        $hardwareresults .= $check."n";
    }
}
if(!empty($_POST['accessories'])) {
    foreach($_POST['accessories'] as $test) {
        $accessoriesresults .= $test."n";
    }
}

$body = "You have a quote request from the website:

Quotation:
$enquiry_product

Hardware:
$hardwareresults

Accessories:
$accessoriesresults

Name: $enquiry_title $enquiry_first_name $enquiry_last_name
Type of organisation: $enquiry_organisation 
Address: $enquiry_address, $enquiry_country
E-Mail: $enquiry_email_address 
Tel: $enquiry_telephone
Comments: $enquiry_additional_comments

Kind regards";

mail($to, $subject, $body, $header);

echo "Thank you for your enquiry.";

} ?>
</form>

Related posts

Leave a Reply

3 comments

  1. I think, that hasn’t directly to do anything with the above code (or the error will just be called as a constraint function call to an CMS internal function that affects this behavior), but I looked at the posted site and tried it out.

    When everything is fine, the URL is the same as the 404 error (and both are output with a 200 OK HTTP header. That’s weird and seems like this is an issue with the whole system you are running it on. I’ve taken a look at the 404 page and found some PHP errors, that are output first:

    Warning: trim() expects parameter 1 to be string, array
    given in
    /home/teamwork/public_html/clients/rogue/wp-includes/query.php
    on line 1452

    Warning: strip_tags() expects
    parameter 1 to be string, array given in
    /home/teamwork/public_html/clients/rogue/wp-includes/formatting.php
    on line 968

    Perhaps these WordPress functions are called. Can you debug where/when they are called exactly?

    Maybe that affects your CMS to go into an error mode instead of rendering the right view?

  2. To start use htmlspecialchars in values output:

    <input type="checkbox" name="accessories[]" value="<?php the_title(); ?>">
    <input type="checkbox" name="accessories[]" value="<?php htmlspecialchars(the_title()); ?>">
    

    Second: You formCheck twice,

    in <form id="customisesystem"> and `<input type="submit" />`.
    

    Just left one.

    and Third and the most important.

    WP does not works as you seem to think. Following condition it should be checked inside a suitable hook as init:

    add_action('init', 'check_form');
    function check_form(){
        if(isset($_POST['submit'])){ 
            // check form
            if (is_OK)
                // redirect to same page
                wp_redirect($my_url);
            else
                // continue to show the form to allow modification
        }
    }
    

    Fourth: your page shows a message error due expects parameter 1 to be string, array given for php functions. So do this:

    Go to lines 1452and 968 and make a debug of vars passed to those functions and show what they are:

    var_dump()

    Probably you are doing something like this:

    trim($_POST['accessories']);
    

    but you should do something like this:

    foreach ($_POST['accessories'] as $val)
    {
        $new_val = $val;
        $new_val = trim($new_val);
        $new_val = strip_tags($new_val);
    
        // more ....
    }
    
  3. Having spent 2 days on this problem I’ve found the most frustrating answer! Where I had the checkbox value as accessories[], I changed that to access[] and it worked.

    I can’t quite explain why but would it be the length of the value var?