weixin_33670786 2014-09-20 06:21 采纳率: 0%
浏览 21

同一页上有多个表格

I have 12 forms on the same page each form needs to be able to be submitted on there on. When submitted the info should be emailed to me. I'm looping in PHP to create the forms to the products:

    <?php foreach ($_productCollection as $_product): ?>

     <form action="" method="post">
                    <p>
                        <textarea class="finalTextArea" name="message" id="textarea" placeholder="Motivering MAX 50 Tecken"></textarea>
                    </p>
                    <p>
                        <input type="text" class="finalText" name="name" id="textfield" placeholder="För och Efternamn">
                    </p>
                    <p>
                        <input type="text" class="finalText" name="email" id="textfield2" placeholder="E-mail">
                    </p>
                    <p>
                        <input type="submit" class="finalTextSubmit" name="submit" value="Rösta!">
                    </p>
                </form> 

and I'm trying to email the form to me using this code:

          <?php
          if(isset($_POST['submit'])){
          $to = "martin@imonline.se"; // this is your Email address
          $from = $_POST['email']; // this is the sender's Email address
          $first_name = $_POST['name'];
          $subject = "Form submission";
          $subject2 = "Copy of your form submission";
          $message = $first_name . " " . " wrote the following:" . "

".            $_POST['message'];
          $message2 = "Here is a copy of your message " . $first_name . "

" .    $_POST['message'];

          $headers = "From:" . $from;
          $headers2 = "From:" . $to;
          mail($to,$subject,$message,$headers);
          mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
          echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";

          }


          ?>

It works on the first form, and when that's submitted it won't work at all. Is there a way to do this with AJAX or another solution ?

  • 写回答

1条回答 默认 最新

  • weixin_33695082 2014-09-20 06:35
    关注

    If you switch the names of all your inputs to end with [], like for example name="message[]" then you will be able to have multiple messages submitted. If you choose to do this you will have to have all fields in one single form though. Something similar to the following (although be aware I have not tested this, so it might contain some flaws, it's just to give an alternative way to solve the problem):

    <form action="" method="post">
    <?php 
        foreach ($_productCollection as $_product){ 
    ?>
        <p>
            <textarea class="finalTextArea" name="message[]" id="textarea" placeholder="Motivering MAX 50 Tecken"></textarea>
        </p>
        <p>
            <input type="text" class="finalText" name="name[]" id="textfield" placeholder="För och Efternamn">
        </p>
        <p>
            <input type="text" class="finalText" name="email[]" id="textfield2" placeholder="E-mail">
        </p>
    
    <?php
        }
    ?>
        <p>
            <input type="submit" class="finalTextSubmit" name="submit" value="Rösta!">
        </p>    
    </form> 
    

    Now all of the post parameters will be stored in arrays, creating multiple of the same value. We then have to loop through each one and send a mail for every element in the array.

    <?php
        if( $_SERVER['REQUEST_METHOD'] == "POST" ){
            $i = 0;
            foreach( $_POST['email'] AS $from ){
                $to = "martin@imonline.se";
                $first_name = $_POST['name'][$i];
                $subject = "Form submission";
                $subject2 = "Copy of your form submission";
                $message = $first_name . " " . " wrote the following:" . "
    
    ".            $_POST['message'][$i];
                $message2 = "Here is a copy of your message " . $first_name . "
    
    " .    $_POST['message'][$i];
    
                $headers = "From:" . $from;
                $headers2 = "From:" . $to;
                mail($to,$subject,$message,$headers);
                mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
                echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
                $i++;
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中