PHP联系人/订单表单不起作用(switch语句)

时间:2018-03-07 23:01:13

标签: php email switch-statement contact-form

我试图找到解决方案大约一个星期了。我很可能遇到了正确的答案而不理解它,因为坦率地说,我对PHP一无所知。所以我真的很感激你的帮助。

我正在更新我父亲的网站,并且我正在尝试将我现有的代码与联系表单和一些代码组合在一起,这些代码由执行其原始网站的人完成,我无法收到订单(所有联系人详细信息)过得很好,但订单本身显示为' Array')

基本上,如果已经给出了数量,那么切换功能应该换出项目的名称并将其添加到订单中。

我希望这是有道理的!

PHP:

<?php
session_start();

$tellimus = array();

function kirjuta_mail($input){

    while ($rida = array_pop($input)){
        $vastus .= "Tellin: ".$rida[0].": ".$rida[1]." tk.\n";
    } 

    $vastus .="\n\n $name\n $email\n $tel\n $address\n\n Kommentaar: $message\n\n";

}    

    if (isset($_POST["submit"])) {
    $kaart = $_POST['kaart'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $tel = $_POST['tel'];
    $address = $_POST['address'];
    $message = $_POST['message'];
    $human = intval($_POST['human']);
    $headers = "From: Saar Graafika lehelt \r\n";
    $headers .= "Reply-To: $email \r\n";
    $to = 'email@email.com'; 
    $subject = 'Tellimus Saar Graafika lehelt';

    $body = "\n $kaart\n\n $name\n $email\n $tel\n $address\n\n Kommentaar: $message\n\n";

    if (!$_POST['name']) {
    $errName = 'Please enter your name';
}
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $errEmail = 'Please enter a valid e-mail address';
}
    if (!$_POST['tel']) {
    $errTel = 'Please enter your phone number';
}
    if (!$_POST['address']) {
    $errAddress = 'Please enter your address';
}
    if ($human !== 8) {
    $errHuman = 'The anti-spam is incorrect';
}
    if (!$errName && !$errEmail && !$errTel && !$errAddress && !$errHuman) {
    if (mail ($to, $subject, $body, $headers)) {
        $result='<div class="alert alert-success">Thank you for your order - we'll be in touch soon!</div>';
    } else {
        $result='<div class="alert alert-danger">We are sorry - there's been an error. Please try again later!</div>';
    }
}
    }
        function IsInjected($str)
{
    $injections = array('(\n+)',
           '(\r+)',
           '(\t+)',
           '(%0A+)',
           '(%0D+)',
           '(%08+)',
           '(%09+)'
           );

    $inject = join('|', $injections);
    $inject = "/$inject/i";

    if(preg_match($inject,$str))
    {
      return true;
    }
    else
    {
      return false;
    }
}

if(IsInjected($email))
{
    echo "We are sorry - there's been an error!";
    exit;
}

    if($checkme) {

        for ($i = 0; $i < 139; $i++){

            if (strlen($_POST['kaart'][$i])) {

                  switch ($i) {
                      case 0:
                          array_push($tellimus,array("kaart113", $_POST['kaart'][$i]));
                          break;
                            LOTS MORE ITEMS HERE
                          case 146:
                          array_push($tellimus,array("Challenges: CD ", $_POST['kaart'][$i]));
                          break;                          
                  }
            }
        }

    if (count($input) == 0)
        kirjuta_mail($tellimus);   
    }
?>

这些位来自原始代码:

session_start();

$tellimus = array();

function kirjuta_mail($input){

    while ($rida = array_pop($input)){
        $vastus .= "Tellin: ".$rida[0].": ".$rida[1]." tk.\n";
    } 

    $vastus .="\n\n $name\n $email\n $tel\n $address\n\n Kommentaar: $message\n\n";

} 

if($checkme) {

for ($i = 0; $i < 139; $i++){

    if (strlen($_POST['kaart'][$i])) {

          switch ($i) {
              case 0:
                  array_push($tellimus,array("kaart113", $_POST['kaart'][$i]));
                  break;
                    LOTS MORE ITEMS HERE
                  case 146:
                  array_push($tellimus,array("Challenges: CD ", $_POST['kaart'][$i]));
                  break;                          
          }
    }
}

if (count($input) == 0)
    kirjuta_mail($tellimus);   
}

相关HTML:

<form role="form" method="post" action="telli.php">
<div class="panel-group" id="accordion2">
    <div class="panel panel-default">
            <a class="acca" data-toggle="collapse" data-parent="#accordion2" href="#collapseInnerOne"><div class="panel-heading kaks">
        <h4 class="panel-title"> J&otilde;ulukaardid: A4 kokkumurtud <span class="glyphicon glyphicon-chevron-down ava"></span></h4>
            </div></a>
            <div id="collapseInnerOne" class="panel-collapse collapse">
                <div class="panel-body">                                         
                    <p>Formaat: A4 kokkumurtud &nbsp;&#124;&nbsp; Hind: <span class="uus">0.90&euro;</span></p>
                    <div class="tellimus">
                        <a href="img/tooted/kaardid/postkaart/kaart136_A5.jpg" data-gallery="#blueimp-gallery-collapseInnerOne"><img src="img/tooted/kaardid/postkaart/kaart136_A5.jpg" class="kesku postkaart" alt=""></a>
                    <div class="form-group">
                        <label class="col-form-label">Kogus:&nbsp;&nbsp;</label>
                            <input type="number" class="form-control kogus" id="kogus" name=kaart[15] placeholder="0">&nbsp;tk
                    </div>
                    </div>
                </div>
            </div>
    </div>
</div>          
            <div class="form-group">
                <label for="name" class="col-form-label">Name<span class="uus">&#42;</span></label>
                    <input type="text" class="form-control" id="name" name="name" placeholder="Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                        <?php echo "<p class='text-danger'>$errName</p>";?>
            </div>
            <div class="form-group">
                <label for="email" class="col-form-label">E-mail<span class="uus">&#42;</span></label>
                    <input name="email" class="form-control" id="email" type="email" placeholder="E-mail" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                        <?php echo "<p class='text-danger'>$errEmail</p>";?>
            </div>
            <div class="form-group">
                <label for="tel" class="col-form-label">Phone<span class="uus">&#42;</span></label>
                    <input name="tel" class="form-control" id="tel" type="tel" placeholder="Your Phone Number" value="<?php echo htmlspecialchars($_POST['tel']); ?>">
                        <?php echo "<p class='text-danger'>$errTel</p>";?>
            </div>
            <div class="form-group">
                <label for="address" class="col-form-label">Address<span class="uus">&#42;</span></label>
                    <textarea class="form-control" rows="4" name="address" placeholder="Your address"><?php echo htmlspecialchars($_POST['address']); ?></textarea>
                        <?php echo "<p class='text-danger'>$errAddress</p>";?>
            </div>
            <div class="form-group">
                <label for="message" class="col-form-label">Message</label>
                    <textarea class="form-control" rows="4" name="message" placeholder="Your Message"><?php echo htmlspecialchars($_POST['message']); ?></textarea>
            </div>
            <div class="form-group">
                <label for="human" class="col-form-label">&#42;How much is 3+5? (Anti-spam)<span class="uus">&#42;</span></label>
                    <input type="text" class="form-control" id="human" name="human" placeholder="Answer">
                        <?php echo "<p class='text-danger'>$errHuman</p>";?>
            </div>
            <div class="form-group">
                <input type="hidden" name="checkme" value="formmail">
                <input id="submit" name="submit" type="submit" value="Place Order">
            </div>
            <div class="form-group">
                <?php echo $result; ?>
            </div>
        </form>

2 个答案:

答案 0 :(得分:0)

订单应存储在$ _POST [&#39; kaart&#39;]中,这意味着您需要使用相同名称组合在一起的复选框,以便在表单中添加订单,客户将点击这些订单以选择商品订购,循环用于将选定的项目添加到$ tellimus数组,以便将其发送到邮件功能

答案 1 :(得分:0)

您的脚本中存在很多问题。不幸的是,我不能在这里详细解释。当您使用HTML5表单元素时,我添加了属性&#39; required&#39;在必填字段中。因此,如果表格未填写,表格将不会提交。

        <form role="form" method="post"  action="telli.php">
        <div class="panel-group" id="accordion2">
            <div class="panel panel-default">
                    <a class="acca" data-toggle="collapse" data-parent="#accordion2" href="#collapseInnerOne"><div class="panel-heading kaks">
                <h4 class="panel-title"> J&otilde;ulukaardid: A4 kokkumurtud <span class="glyphicon glyphicon-chevron-down ava"></span></h4>
                    </div></a>
                    <div id="collapseInnerOne" class="panel-collapse collapse">
                        <div class="panel-body">                                         
                            <p>Formaat: A4 kokkumurtud &nbsp;&#124;&nbsp; Hind: <span class="uus">0.90&euro;</span></p>
                            <div class="tellimus">
                                <a href="img/tooted/kaardid/postkaart/kaart136_A5.jpg" data-gallery="#blueimp-gallery-collapseInnerOne"><img src="img/tooted/kaardid/postkaart/kaart136_A5.jpg" class="kesku postkaart" alt=""></a>
                            <div class="form-group">
                                <label class="col-form-label">Kogus:&nbsp;&nbsp;</label>
                                    <input type="number" class="form-control kogus" id="kogus" name=kaart[15] placeholder="0">&nbsp;tk
                            </div>
                            </div>
                        </div>
                    </div>
            </div>
        </div>          
        <div class="form-group">
            <label for="name" class="col-form-label">Name<span class="uus">&#42;</span></label>
                <input type="text" class="form-control" id="name" name="name" placeholder="Name" value="" required>

        </div>
        <div class="form-group">
            <label for="email" class="col-form-label">E-mail<span class="uus">&#42;</span></label>
                <input name="email" class="form-control" id="email" type="email" placeholder="E-mail" value="" requird>

        </div>
        <div class="form-group">
            <label for="tel" class="col-form-label">Phone<span class="uus">&#42;</span></label>
                <input name="tel" class="form-control" id="tel" type="tel" placeholder="Your Phone Number" value="" required>

        </div>
        <div class="form-group">
            <label for="address" class="col-form-label">Address<span class="uus">&#42;</span></label>
            <textarea class="form-control" rows="4" name="address" placeholder="Your address" required></textarea>

        </div>
        <div class="form-group">
            <label for="message" class="col-form-label">Message</label>
                <textarea class="form-control" rows="4" name="message" placeholder="Your Message"></textarea>
        </div>
        <div class="form-group">
            <label for="human" class="col-form-label">&#42;How much is 3+5? (Anti-spam)<span class="uus">&#42;</span></label>
                <input type="text" class="form-control" id="human" name="human" placeholder="Answer" required>

        </div>
        <div class="form-group">
            <input type="hidden" name="checkme" value="formmail">
            <input id="submit" name="submit" type="submit" value="Place Order">
        </div>
        <div class="form-group">
            <?php if(isset($result)) {echo $result;} ?>
        </div>
    </form>

表单提交后,您的脚本甚至在处理所有这些订单元素之前就会发送电子邮件。这就是为什么你有订购数组的原因。我已经改变了执行顺序的顺序,并且还改变了编码以处理顺序元素。希望这会有所帮助..

            <?php
            session_start();

            if (isset($_POST["submit"])) {
                foreach($_POST as $key=>$val) {
                   // echo "$key => $val <br/>\n";
                }
            $kaart = $_POST['kaart'];
            $name = $_POST['name'];
            $email = $_POST['email'];
            $tel = $_POST['tel'];
            $address = $_POST['address'];
            $message = $_POST['message'];
            $human = intval($_POST['human']);
            $headers = "From: Saar Graafika lehelt \r\n";
            $headers .= "Reply-To: $email \r\n";
            $to = 'email@email.com'; 
            $subject = 'Tellimus Saar Graafika lehelt';

            $body = "Name = $name\n Email = $email\n Tel = $tel\n address = $address\n\n Kommentaar: $message\n\n";

            if(IsInjected($email))
            {
                echo "We are sorry - there's been an error!";
                exit;
            }


            $kart = "";
            foreach($_POST['kaart'] as $key => $val) {
                if($val!==0){
                    $kart .= "kaart".$key."=$val\n";
                }
            }

            $body .= "\n\n$kart";

            echo nl2br($body) ;

                if (mail ($to, $subject, $body, $headers)) {
                    $result='<div class="alert alert-success">Thank you for your order - we\'ll be in touch soon!</div>';
                } else {
                    $result='<div class="alert alert-danger">We are sorry - there\'s been an error. Please try again later!</div>';
                }

            }

            function IsInjected($str)
            {
            $injections = array('(\n+)',
                   '(\r+)',
                   '(\t+)',
                   '(%0A+)',
                   '(%0D+)',
                   '(%08+)',
                   '(%09+)'
                   );

            $inject = join('|', $injections);
            $inject = "/$inject/i";

            if(preg_match($inject,$str))
            {
              return true;
            }
            else
            {
              return false;
            }
        }


        ?>