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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)