同一页面上的多个表单

时间:2014-09-20 06:21:30

标签: java php ajax

我在同一页面上有12个表单,每个表单都需要能够在那里提交。 提交后,信息应通过电子邮件发送给我。我在PHP中循环以创建产品的表单:

    <?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> 

我正在尝试使用此代码通过电子邮件将表单发送给我:

          <?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:" . "\n\n".            $_POST['message'];
          $message2 = "Here is a copy of your message " . $first_name . "\n\n" .    $_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.";

          }


          ?>

它适用于第一个表单,当提交时它根本不起作用。有没有办法用AJAX或其他解决方案做到这一点?

1 个答案:

答案 0 :(得分:0)

如果您将所有输入的名称切换为[],例如name="message[]",那么您将能够提交多封邮件。如果您选择这样做,您必须将所有字段都放在一个表单中。类似于以下内容(虽然请注意我没有测试过这个,所以它可能包含一些缺陷,只是提供了一种解决问题的替代方法):

<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> 

现在所有的post参数都将存储在数组中,创建多个相同的值。 然后我们必须循环遍历每个元素并为数组中的每个元素发送邮件。

<?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:" . "\n\n".            $_POST['message'][$i];
            $message2 = "Here is a copy of your message " . $first_name . "\n\n" .    $_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++;
        }
    }
相关问题