使用PHP和WordPress发送多个输入值

时间:2022-02-13 02:05:32

OK, so here is my slightly complicated question for you guys.

好的,所以这是我们稍微复杂的问题。

I have a WordPress site where user can add products to category, there is functionality which displays sorted products in one page. Here is pretty much code of single product:

我有一个WordPress网站,用户可以将产品添加到类别,有一些功能可以在一个页面中显示已排序的产品。以下是单品的代码:

<td><input class="cbx" type="checkbox" name="product" value="product">    </td>
<td class="bright"><?php the_title(); ?> <input class="ptitle" type="hidden" name="ptitle" value="<?php the_title(); ?>"></td>
            <td class="bright">box</td>
            <td class="bright">height</td>
            <td class="bright">price</td>
            <td> <input class="count" type="text" name="<?php echo $post->ID; ?>count" maxlength="4"></td>
</tr>

This code is looped and displays in form tags on other archive.php . On archive.php i have jquery script which sends data from form to mail.php pasted below (i stripped it a bit for better visibility):

此代码循环显示在其他archive.php上的表单标记中。在archive.php上,我有jquery脚本,它将表单中的数据发送到下面粘贴的mail.php(为了更好的可见性,我将它剥离了一点):

    <?php
// Fetching Values from URL.
$email = $_POST['email'];
$place = $_POST['place'] ;
$street = $_POST['street'] ;
$code = $_POST['code'] ;
$email = filter_var($email, FILTER_SANITIZE_EMAIL); 
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$subject = 'new message';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n";
$template = '<div><br><b>Adress:</b><br> '.$street.'<br>'. $code.'     '.$place.'<br>'. '<br><b>Email:</b> ' . $email . '<br>'
$sendmessage = "<div>" . $template . "</div>";
$sendmessage = wordwrap($sendmessage, 70);
mail("test@test.com", $subject, $sendmessage, $headers);
echo "success.";
} else {
echo "<span>*incorrect email*</span>";
}

What i need is a function which will check if product is checked, get its name, gets number which is provided through input and sends it through my email function.

我需要的是一个功能,它将检查产品是否被检查,获取其名称,获取通过输入提供的数字并通过我的电子邮件功能发送它。

I have no problem sending predefined inputs, but when i get some dynamic data (there are multiple products), i don't know how to handle this. All i have in mind is creating array and foreach loop.

我发送预定义输入没有问题,但是当我得到一些动态数据(有多个产品)时,我不知道如何处理它。我想到的只是创建数组和foreach循环。

I will appreciate any tips how to deal with this :)

我会感谢任何提示如何处理这个:)

1 个解决方案

#1


Well, i managed to do this with little jquery and php

好吧,我设法用小jquery和php做到这一点

First of all, i am changing name attribute on checkbox click with jquery to determine if product will be posted

首先,我正在使用jquery更改复选框上的name属性,以确定是否将发布产品

$("input[type='checkbox']").click(function() {
  if ($(this).is(':checked')) {
     $(this).closest('tr').find('input').attr('name', 'title[]');

  }else {
     $(this).closest('tr').find('input').attr('name', ' ');
  }
});    

then, since title attribute is an array, i added foreach function in my mail.php

那么,因为title属性是一个数组,我在mail.php中添加了foreach函数

$title = $_POST['title'] ;
$tstring = "";
foreach( $title as $key => $value){
    $tstring .= '<td style="border-right:1px solid #333;text-    align:center">'.$value.'</td>';
}

#1


Well, i managed to do this with little jquery and php

好吧,我设法用小jquery和php做到这一点

First of all, i am changing name attribute on checkbox click with jquery to determine if product will be posted

首先,我正在使用jquery更改复选框上的name属性,以确定是否将发布产品

$("input[type='checkbox']").click(function() {
  if ($(this).is(':checked')) {
     $(this).closest('tr').find('input').attr('name', 'title[]');

  }else {
     $(this).closest('tr').find('input').attr('name', ' ');
  }
});    

then, since title attribute is an array, i added foreach function in my mail.php

那么,因为title属性是一个数组,我在mail.php中添加了foreach函数

$title = $_POST['title'] ;
$tstring = "";
foreach( $title as $key => $value){
    $tstring .= '<td style="border-right:1px solid #333;text-    align:center">'.$value.'</td>';
}