表单提交PHP ajax [英] Form submission PHP ajax

查看:84
本文介绍了表单提交PHP ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置表单提交,并且在使Ajax连接到PHP并将表单信息发送到所需的电子邮件地址时遇到一些麻烦.在这整个过程中,我都不是专家,这是我第一次尝试AJAX提交表单信息.

I am setting up a form submission and having some troubles getting the Ajax to connect to the PHP and send the form info to the desired email address. Not an expert at this whole thing as its the first time I attempt AJAX to submit a form information.

感谢您提出任何关于出问题的信息.

Any input on what is going wrong is appreciated.

预先感谢

此代码已可以正常使用.我已使用新代码更新了问题.

THIS CODE IS NOW ALL WORKING I have updated the question with the new code.

表格:

    <form name="contactform" id="contactsubmit" action="form_action.php">
        <h2>contact form</h2>
        <hr>
        <label class="Tgrey">name</label>
        <input type="text" name="name" id="name" required>
        <label class="Tgrey">email</label>
        <input type="email" name="email" id="email" required>
        <label class="Tgrey">Telephone</label>
        <input type="tel" name="telephone" id="telephone" required>
        <br>
        <br>
        <label class="Tgrey">Telephone</label>
        <br>
        <textarea name="message" rows="10" cols="50" id="message" required></textarea>
        <br>
        <input type="submit" value="send">
    </form>

JQUERY/AJAX

$("form").submit(function(e)
{
    event.preventDefault();
    $.ajax(
    {
        'url' : $(this).attr('action'),
        'type' : "POST",
        'data' : $(this).serialize(),
        success:function(success)
        {alert("HEY")},
    });
});

PHP

   if(isset($_POST['name'], $_POST['email'], $_POST['telephone'], $_POST['message'])){

    $name = $_POST['name'];
    $email = $_POST['email'];
    $telephone = $_POST['telephone'];
    $message = $_POST['message'];

    $to  = "name@email.com";
    $subject = "Message from $name via Web:";
    $txt = "This message has come website: \n
    $message \n
    Contact details:\n 
    Name: $name, Email: $email, Telephone: $telephone";
    $header = "From Website";

    mail($to,$subject,$txt,$headers);
}

推荐答案

$_POST['submit']不在序列化表格数据中发送.您应该做的是检查POST数组中是否有数据:

$_POST['submit'] is not sent in the serialized form data. What you should do is check to see if the POST array has got data in it:

if(!empty($_POST)) {.. // if not empty, proceed

如果您不想更改PHP,则可以将&submit=submit附加到在AJAX函数中保存序列化数据的变量中:

If you didn't want to change your PHP you could append &submit=submit to your variable holding the serialized data in your AJAX function:

'data' : $(this).serialize() + '&submit=submit',

此外,正如@Fred -ii-指出的那样,您还有一些未设置的变量($subject (除非您打算在代码中进行设置,否则表单中也将缺少此变量)$headers).您需要在脚本中添加一些基本的错误检查,以指示这些问题.在打开<?php标记

In addition, as @Fred -ii- pointed out, you have some variables which are not set($subject (this is also missing from your form unless you intend to set it in the code) and $headers). You need to add some basic error checking to your scripts which will indicate these problems. Add error reporting to the top of your file(s) right after your opening <?php tag

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

这篇关于表单提交PHP ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆