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

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大