dpa55065 2017-03-28 11: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 07: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/>";
        }
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部