2

我创建了一个表单(我省略了部分),它收集用户的复选框选择(通过 Wordpress 创建)并将结果通过电子邮件发送给我。

当我勾选硬件选择并提交时,电子邮件正常,但是当我勾选任何附件并单击提交时,它会进入 404 页面。

谁能看到错误在哪里?我以同样的方式创建了两组复选框和结果,只是看不到错误在哪里!

<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>
4

3 回答 3

1

要开始htmlspecialchars在值输出中使用:

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

第二:你formCheck两次,

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

只留下一个。

第三,也是最重要的。

WP 不像你想象的那样工作。在以下情况下,应在合适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
    }
}

第四expects parameter 1 to be string, array given:您的页面显示由于php 函数而导致的消息错误。所以这样做:

转到第 1452 和 968 行并调试传递给这些函数的变量并显示它们是什么:

var_dump()

可能你正在做这样的事情:

trim($_POST['accessories']);

但你应该这样做:

foreach ($_POST['accessories'] as $val)
{
    $new_val = $val;
    $new_val = trim($new_val);
    $new_val = strip_tags($new_val);

    // more ....
}
于 2013-02-12T13:54:09.637 回答
1

我认为,这并没有直接对上面的代码做任何事情(或者错误将被称为对影响这种行为的 CMS 内部函数的约束函数调用),但我查看了发布的站点并尝试了它.

当一切正常时,URL 与 404 错误相同(并且两者都带有200 OKHTTP 标头输出。这很奇怪,似乎这是您运行它的整个系统的问题。我看过404页面,发现一些PHP错误,首先输出:

警告:trim() 期望参数 1 是字符串,数组在 /home/teamwork/public_html/clients/rogue/wp-includes/query.php中的第1452

警告:strip_tags() 期望参数 1 是字符串,给定数组在 /home/teamwork/public_html/clients/rogue/wp-includes/formatting.php968行

可能会调用这些 WordPress 函数。您可以调试它们的确切调用位置/时间吗?

也许这会影响您的 CMS 进入错误模式而不是呈现正确的视图?

于 2013-02-12T13:59:38.730 回答
1

在这个问题上花了 2 天后,我找到了最令人沮丧的答案!在我将复选框值设置为 的地方accessories[],我将其更改为access[]并且它起作用了。

我不能完全解释为什么,但它会是值 var 的长度吗?

于 2013-02-12T15:57:44.637 回答