我有一个带有图像的表单,带有复选框的图像文件名和带有数量的字段文本(这是管理员在wordpress文件夹上传图像时动态创建的) 用户选中复选框并写入数量,我希望将该信息发送到电子邮件。 我做到了这一点,但它只有在我点击所有复选框时才有效,如果没有选中其中一个复选框,它会发送包含空信息的电子邮件。
这是我认为问题所在代码的一部分:
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {
$result ="";
$myQuantity = $_POST["quantity"];
$myFile = $_POST["fileName"];
//Combine both filename and quantity arrays
$values = array_combine($myFile, $myQuantity);
if(!empty($_POST["fileName"])){
foreach($values as $key => $value){
$result .= "$key - Quantity: $value <br/>";
}
}
... 任何帮助将不胜感激。
更新: 这是整个表格以防万一:
echo '<div class="images-form"><form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
foreach($images as $image) {
echo '<div class="thumb"><img src="';
echo $uploads['baseurl'].'/'.$a['folder_name'].'/'.$image;
echo '" alt="" /><br/>';
$fileName = basename($path.'/'.$image);
echo $fileName;
echo ' <input name="fileName[]" type ="checkbox" value="'.$fileName.'" /><br/>';
echo 'Quantity: <input name="quantity[]" type="text" value="" size="5" /></div>';
}
echo '<div class="send-form"><input type="submit" name="cf-submitted" value="Send email"/></div></form></div>';
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {
$result ="";
$myQuantity = $_POST["quantity"];
$myFile = $_POST["fileName"];
//Combine both arrays
$values = array_combine($myFile, $myQuantity);
if(!empty($_POST["fileName"])){
foreach($values as $key => $value){
$result .= "$key - Quantity: $value <br/>";
}
}
// to get wordpress user name and last name
global $current_user;
get_currentuserinfo();
$userName = $current_user->user_firstname;
$userLastName = $current_user->user_lastname ;
$to = get_option( 'admin_email' );// get the blog administrator's email address
$email = $current_user->user_email;
$headers = "From: $email" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = "Print request";
$message = "<html><body>";
$message .= $userName." ".$userLastName." wants to print the following file(s): <br/>".$result;
$message .= "</body></html>";
// If email has been process for sending, display a success message
if ( wp_mail( $to, $subject, $message, $headers ) ) {
echo '<div>';
echo '<p>Thanks for contacting me, expect a response soon.</p>';
echo '</div>';
} else {
echo 'An unexpected error occurred';
}
}
答案 0 :(得分:1)
当浏览器提交表单时,只会将选中的复选框传递给服务器。另一方面,即使文本框为空,也始终传递。 PHP仅使用$ _POST [&#39; fileName&#39;]的复选框和$ _POST [&#39;数量&#39;]中所有数量框的数组创建数组。
如果选择的复选框少于所有复选框,则$ myQuantity将具有与$ myFile不同的元素数。
导致问题。 如果每个数组中的元素数量不相等,则Array_combine返回false。
对此的一个简单解决方案是索引输入框并消除array_combine。
$index=0;
foreach($images as $image) {
$index++;
echo '<div class="thumb"><img src="';
echo $uploads['baseurl'].'/'.$a['folder_name'].'/'.$image;
echo '" alt="" /><br/>';
$fileName = basename($path.'/'.$image);
echo $fileName;
// use $index between the [] of the field name. PHP will use it as the key value when it creates the array.
echo ' <input name="fileName['. $index .']" type ="checkbox" value="'.$fileName.'" /><br/>';
echo 'Quantity: <input name="quantity['. $index .']" type="text" value="" size="5" /></div>';
}
现在您收到$ _POST [&#39; filename&#39;]的索引数组。
$result ="";
$myQuantity = $_POST["quantity"];
$myFile = $_POST["fileName"];
// eliminate the array_combine.
if(!empty($_POST["fileName"])){
foreach( $myFile as $key => $value){
// key will now be the $index value from the form generation.
// $value will be the file name.
// The index will match the $_POST['quantity'] array allowing
// simple lookup by key value.
$result .= "$value- Quantity: ". $myQuantity[$key] ."<br/>";
}
}