dongtaochan0777 2019-08-06 10:59
浏览 55
已采纳

PHP表单(使用POST)不提交或发送电子邮件,只显示空白页面

I have the following HTML:

<form method="post" action="https://www.domain.co.uk/v2/contact.php" id="contactform">
              <p><label for="name"><span class="required">*</span> Your Name</label><br>
              <input name="name" type="text" id="name" size="30" value="" class="input" required> </p>
              <p><label for="email"><span class="required">*</span> Email</label><br>
              <input name="email" type="email" id="email" size="30" value="" class="input" required></p>
              <p><label for="phone">Phone</label><br>
              <input name="phone" type="tel" id="phone" size="30" value="" class="input"></p>
              <p><label for="subject">Subject</label><br>
              <select name="subject" id="subject" class="input">
              <option value="Not sure" selected="selected">Not sure</option>
                <option value="this">this</option>
                <option value="that">that</option>
              </select></p>              
              <p><label for="comments"><span class="required">*</span> Message</label><br>
              <textarea name="comments" cols="40" rows="3" id="comments" class="input" required></textarea></p>
              <p><input type="submit" class="submit" id="submit" value="Submit"></p>
              </form>

and the following in contact.php

<?php

    if ($_POST['name'] != "") {
    $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
    if ($_POST['name'] == "") {
        $errors .= 'Please enter a valid name.<br/><br/>';
    }
    } else {
        $errors .= 'Please enter your name.<br/>';
    }

    if ($_POST['email'] != "") {
    $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
    }
    } else {
        $errors .= 'Please enter your email address.<br/>';
    }

    if ($_POST['phone'] != "") {
    $_POST['phone'] = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
    if ($_POST['phone'] == "") {
        $errors .= 'Please enter your phone number.<br/>';
    }
    } else {
        $errors .= 'Please enter your phone number.<br/>';
    }

    if ($_POST['subject'] != "") {
    $_POST['subject'] = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
    if ($_POST['subject'] == "") {
        $errors .= 'Please choose a subject.<br/>';
    }
    } else {
        $errors .= 'Please choose a subject.<br/>';
    }

    if ($_POST['comments'] != "") {
    $_POST['comments'] = filter_var($_POST['comments'], FILTER_SANITIZE_STRING);
    if ($_POST['comments'] == "") {
        $errors .= 'Please enter a message.<br/>';
    }
    } else {
        $errors .= 'Please enter a message.<br/>';
    }


    if (!$errors) {
            $mail_to = 'info@myemails.co.uk';
            $subject = 'Enquiry';
            $message .= 'Regarding: ' . $_POST['subject'] . "

";
            $message .= 'Name: ' . $_POST['name'] . "

";
            $message .= 'Email: ' . $_POST['email'] . "

";
            $message .= 'Phone: ' . $_POST['phone'] . "

";
            $message .= 'Message ' . $_POST['comments'] . "

";

            $success = mail($mail_to, $subject, $message, "From: <$email>");

            if ($success){
              print "sent";
            }
            else{
              print "failed";
            }
    }

?>

no matter what I change or try I end up on a blank white page for contact.php instead of seeing the sent or failed message (having removed my javascript validation incase I was causing issue there), likewise there is nothing in the error log and despite having gone back over the code I can't spot the issue? Unsure if I have stared at it for too long and missing something obvious or there is a deeper problem?

Any pointers appreciated.

var_dump shows it is getting the information:

array(5) { ["name"]=> string(10) "Joe Bloggs" ["email"]=> string(16) "joe@anyemail.com" ["phone"]=> string(11) "07123456789" ["subject"]=> string(8) "Not sure" ["comments"]=> string(17) "test message here" }
  • 写回答

1条回答 默认 最新

  • duandaodao6951 2019-08-06 11:15
    关注

    You used string concatenation, but you didn't defined your variables before that, if you change your code, like this:

    <?php
    
            $errors = '';
    
            if ($_POST['name'] != "") {
            $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
            if ($_POST['name'] == "") {
                    $errors .= 'Please enter a valid name.<br/><br/>';
            }
            } else {
                    $errors .= 'Please enter your name.<br/>';
            }
    
            if ($_POST['email'] != "") {
            $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
            }
            } else {
                    $errors .= 'Please enter your email address.<br/>';
            }
    
            if ($_POST['phone'] != "") {
            $_POST['phone'] = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
            if ($_POST['phone'] == "") {
                    $errors .= 'Please enter your phone number.<br/>';
            }
            } else {
                    $errors .= 'Please enter your phone number.<br/>';
            }
    
            if ($_POST['subject'] != "") {
            $_POST['subject'] = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
            if ($_POST['subject'] == "") {
                    $errors .= 'Please choose a subject.<br/>';
            }
            } else {
                    $errors .= 'Please choose a subject.<br/>';
            }
    
            if ($_POST['comments'] != "") {
            $_POST['comments'] = filter_var($_POST['comments'], FILTER_SANITIZE_STRING);
            if ($_POST['comments'] == "") {
                    $errors .= 'Please enter a message.<br/>';
            }
            } else {
                    $errors .= 'Please enter a message.<br/>';
            }
    
            $message = '';
            if (empty($errors)) {
                            $mail_to = 'info@myemails.co.uk';
                            $subject = 'Enquiry';
                            $message .= 'Regarding: ' . $_POST['subject'] . "
    
    ";
                            $message .= 'Name: ' . $_POST['name'] . "
    
    ";
                            $message .= 'Email: ' . $_POST['email'] . "
    
    ";
                            $message .= 'Phone: ' . $_POST['phone'] . "
    
    ";
                            $message .= 'Message ' . $_POST['comments'] . "
    
    ";
    
                            $success = mail($mail_to, $subject, $message, "From: <$email>");
    
                            if ($success){
                                print "sent";
                            }
                            else{
                                print "failed";
                            }
            } else {
                echo $errors;
            }
    
    ?>
    

    It works. You said that you are at live site, so probably errors are not show up, if you want you can add this lines:

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    

    at the top of your php file, to see errors, but clients also would see these errors. You can add IP Check for your IP around them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来