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 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据