douping5015 2015-05-30 23:42
浏览 92

PHP联系表单将在提交后刷新页面。

After searching for about 3 hours i still can't figure this one out. I Have a html template with a contact form and im trying to make it work using a PHP script. I changed the template to PHP and pasted the PHP form script in it. everything is working fine except the confirmation text. After a successful submission it will just refresh the page instead of printing "Your mail has been sent successfuly ! Thank you for your feedback". i do not want a redirect, i just want it to print on the same page.

Any ideas? I got a sample of my code.

<form action="<? echo $_SERVER['PHP_SELF']; ?>" id="contact-form" method="post" class="form afsana-form" role="form">
                            <div class="row">
                                <div class="col-sm-12 form-group">
                                    <input class="form-control afsana-style" id="name" name="name" placeholder="name" type="text" required autofocus />
                                </div>
                                <div class="col-sm-12 form-group">
                                    <input class="form-control afsana-style" id="email" name="email" placeholder="email" type="email" required />
                                </div>
                                <div class="col-sm-12 form-group">
                                    <textarea class="form-control" id="message" name="message" placeholder="message" rows="5"></textarea>
                                </div>
                                <div class="col-sm-12 form-group">
                                    <button class="btn btn-primary afsana-btn" name="submit" value="verzenden" type="submit">Verzenden <i class="ion-arrow-graph-up-right"></i></button>
                                </div>
                            </div>
                        </form>
                        <?php

if(isset($_POST["submit"])){
// Checking For Blank Fields..
if($_POST["name"]==""||$_POST["email"]==""||$_POST["message"]==""){
echo "Fill All Fields..";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$subject = (Contact_form);
$message = $_POST['message'];
$headers = 'From:'. $email . "
"; // Sender's Email
$headers .= 'Cc:'. $email2 . "
"; // Carbon copy to Sender
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
mail("something@domain.com", $subject, $message, $headers);
echo "Your mail has been sent successfuly ! Thank you for your feedback";
}
}
}
?>
  • 写回答

2条回答 默认 最新

  • dongqin1861 2015-05-30 23:50
    关注

    First, you have this: $subject = (Contact_form); which should throw an error, so I assume you have error reporting turned off. When developing, you should have error reporting on so you can see errors in your code... Else you are just working blind. I don't mean by throwing tacky error_reporting(0) in every file either, I mean to set your error reporting level to E_ALL in your php.ini.

    You also have: $headers .= 'Cc:'. $email2 . " "; However, $email2 is not defined anywhere, so you would get an error here too.. which is why it's important to test with error reporting on.

    See if this works:

        <?php
    
        $error = '';
    
        if(isset($_POST['submit']))
        {
            if ( !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) )
            {
    
                $email = $_POST['email'];
                $email = filter_var($email, FILTER_SANITIZE_EMAIL);
                if ( $email = filter_var($email, FILTER_VALIDATE_EMAIL) )
                {
                    $subject = '(Contact_form)';
                    $message = $_POST['message'];
    
                    $headers = 'From:'. $email . "
    "; // Sender's Email
    
                    $message = wordwrap($message, 70);
    
                    if ( $result = mail("something@domain.com", $subject, $message, $headers) ) {
                        $error = 'Success';
                    } else {
                        $error = 'There was an error sending your email!';
                    }
    
                } else {
                    $error = 'Invalid Email Address!';
                }
    
    
            } else {
                $error = 'Please fill all fields.';
            }
        }
    
    ?>
    
    <p><?= $error ?></p>
    <form action="" method="post">
        <input type="text" name="name" value="" /><br />
        <input type="email" name="email" value="" /><br />
        <textarea name="message" rows="5"></textarea><br />
        <input type="submit" name="submit" value="submit" />
    </form>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能