简体   繁体   English

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

[英]PHP contact form will just refresh the page after submission.

After searching for about 3 hours i still can't figure this one out. 搜索约3个小时后,我仍然无法弄清楚这一点。 I Have a html template with a contact form and im trying to make it work using a PHP script. 我有一个带有联系表单的html模板,我试图使用PHP脚本使其工作。 I changed the template to PHP and pasted the PHP form script in it. 我将模板更改为PHP,并在其中粘贴了PHP表单脚本。 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 . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email2 . "\r\n"; // 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";
}
}
}
?>

First, you have this: $subject = (Contact_form); 首先,您有: $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 . 我的意思也不是在每个文件中都添加俗气的error_reporting(0) ,而是在php.ini中将错误报告级别设置为E_ALL

You also have: $headers .= 'Cc:'. $email2 . "\\r\\n"; 您还有: $headers .= 'Cc:'. $email2 . "\\r\\n"; $headers .= 'Cc:'. $email2 . "\\r\\n"; 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 . 然而, $email2没有在任何地方定义,所以你会得到一个错误太..这就是为什么它有错误报告测试很重要。

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 . "\r\n"; // 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>

Try to put in $subject just a string value like: 尝试只将$ subject放入一个字符串值,例如:

$subject = 'Test subject';

change also the following line to this (there is no $email2 defined): 还将以下行更改为此(未定义$ email2):

$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender

and give it a try. 并尝试一下。 You can also put as first line of your code 您也可以将代码作为第一行

<?php error_reporting(E_ALL); ?>

and check for errors when submiting the form. 并在提交表单时检查错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 联系人表格正在发送电子邮件,但提交后未回复。 - Contact form is sending email but not responding after submission. 表单验证和提交后,禁用“提交”按钮。 PHP,JQ? - Disable “submit” button after form validation and submission. PHP, JQ? URL 上的加载页面搜索仅适用于 index.php 表单提交。 -JS - Load page on URL search works only from index.php form submission. -JS 表单提交后如何刷新页面? - How to refresh the page after form submission? 我确实想在使用php和javascript提交表单后刷新页面? - I do want to refresh a page after form submission using php and javascript? 提交表单后,我必须刷新我的php页面以执行代码并显示输出 - I have to refresh my php page after form submission to execute the code and display output 提交ajax表单后,iScroll4高度错误。 高度是表单提交之前的原始高度 - iScroll4 height is wrong after ajax form submission. Height is orginal height before form submit 提交表单后,Backbone.Marionette + Rails应用程序将重定向。 为什么? - Backbone.Marionette + Rails app redirects after form submission. Why? 表单提交后如何刷新我的索引页面? - how to refresh my index page after form submission? 提交表单和页面后无响应,仅自动重新加载 - No response after form submission and page just auto reload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM