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++;
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名