简体   繁体   English

无法让我的联系表格生效

[英]Can't get my contact form to work

I have a question about creating a contact form.我有一个关于创建联系表单的问题。 Currently I have a form and some php code.目前我有一个表单和一些 php 代码。 But every time when I click on the submit button, I will be redirected to the php site itself instead of sending an actual mail.但是每次当我点击提交按钮时,我都会被重定向到 php 站点本身,而不是发送实际的邮件。

This is the code I have:这是我的代码:

HTML Form: HTML 表单:

<form action="send.php" method="post" target="_new">
  <h1>Name:</h1> <br><input type="text" name="name" class="name"><br>
  <h1>E-mail:</h1> <br><input type="text" name="email" class="email"><br>
  <h1>Message:</h1> <br><textarea rows="20" cols="60" width="450px" height="210px" name="message"></textarea><br>
  <input type="submit" name="submit" value="submit" class="submit">
</form>

Php page: php页面:

<?php

$recipient = "myemail@mail.com";
if (isset($_POST['email'])){
    $mail = $_POST['email'];
    $name = $_POST['name'];
    $message = $_POST['message'];
    mail("$recipient", $name, $message);
}
header('Location: send.php');
?>

I've read on the internet that there is a possibility with Ajax and javascript?我在互联网上读到 Ajax 和 javascript 有可能吗? But I'm not very familiar with AJAX.但我对 AJAX 不是很熟悉。 So help would be really appreciated!所以帮助将不胜感激! :D :D

The only thing this form has to do is to send an email to my email account without opening a mail program while clicking on the submit button.此表单唯一要做的就是在单击提交按钮时向我的电子邮件帐户发送电子邮件,而无需打开邮件程序。

Thanks in regard!致谢!

When you clicking on submit button, the action is gone to send.php after the mail function irt is redirected to send.php itself.当您在提交按钮点击,动作去send.php邮件功能IRT被重定向到后send.php本身。 So a selfloop starts.于是一个自循环开始了。 You need to change that redirect page to your form page.您需要将该重定向页面更改为您的表单页面。 ie., IE。,

header('Location: send.php'); //change it !!

Also you need a mail server to send the mail from the local machine otherwise you need to host the project.您还需要一个mail server来从本地计算机发送邮件,否则您需要托管该项目。

If you want to send the mail using AJAX does the following code:如果要使用AJAX发送邮件,请执行以下代码:

<script>
$('form').submit(function(){
    $.ajax({
        url:'send.php',
        data:$('form').serialize(),
        success:function(response){
            alert(response);
        }
    });
    return false;
});
</script>

Then send.php will be like然后send.php会像

$recipient = "myemail@mail.com";
if (isset($_POST['email'])){
    $mail = $_POST['email'];
    $name = $_POST['name'];
    $message = $_POST['message'];
    if(mail($recipient, $name, $message)){
        echo 'Mail sent';
    }
    else{
         echo 'Mail cant sent';
    }
}

请删除 $recipient 中的双引号 ""

mail($recipient, $name, $message);
<form  method="post">
                            <h1>Name:</h1> <br><input type="text" id="name" name="name" class="name"><br>
                            <h1>E-mail:</h1> <br><input type="text" id="email" name="email" class="email"><br>
                            <h1>Message:</h1> <br><textarea id="msg" rows="20" cols="60" width="450px" height="210px" name="message"></textarea><br>
                            <input type="submit" name="submit" value="submit" class="submit">
                        </form>
<script>
$.ajax({
    type: "post",
    url: "send.php",
    data: {name: $('#name').val(), email: $('#email').val(), message: $('#msg').val()},
    success: function(result){
        alert("Mail send!");
    }
});
</script>

your php code is the same but you don't need the header.您的 php 代码是相同的,但您不需要标题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM