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.
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>
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: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?
To start use
htmlspecialchars
in values output:Second: You
formCheck
twice,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
asinit
: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:
Probably you are doing something like this:
but you should do something like this:
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 toaccess[]
and it worked.I can’t quite explain why but would it be the length of the value var?