dpa55065 2017-03-28 19:00
浏览 56
已采纳

PHP动态表单与文本字段和复选框相结合

I have a form with an image, the image file name with a checkbox, and a field text (with the quantity) (this is dynamically created when the admin uploads the image on a wordpress folder) The user select the checkbox and write a quantity, I want that information sent to an email. I made that possible, but it only works when I click on all the checkboxes, if one of them is not selected, it sends the email with empty information.

this is the part of the code where I think the problem is:

    // 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/>";
            }
        }

... Any help will be appreciated.

update: this is the whole form just in case:

    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 '&nbsp;&nbsp;<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" . "
";
        $headers .= "MIME-Version: 1.0
";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1
";
        $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';
        }
    }
  • 写回答

1条回答 默认 最新

  • dongmaonao0505 2017-03-29 15:36
    关注

    When the browser submits a form, only checkboxes that are checked are passed to the server. Text boxes on the other hand, are always passed even if they are blank. PHP creates an array using just the checked boxes for $_POST['fileName'] and an array of all of the quantity boxes in $_POST['quantity'].

    When less than all checkboxes are selected, $myQuantity will have a different number of elements than $myFile.

    Which leads to the problem. Array_combine returns false if the number of elements in each array isn't equal.

    A simple solution to this is to index your input boxes and eliminate the 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 '&nbsp;&nbsp;<input name="fileName['. $index .']" type ="checkbox" value="'.$fileName.'" /><br/>';
        echo 'Quantity: <input name="quantity['. $index .']" type="text" value="" size="5" /></div>';
    }
    

    Now you receive an indexed array for $_POST['filename'].

    $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/>";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)