douna3367 2012-04-20 22:50
浏览 37
已采纳

jquery联系表单php问题

So I have a contact field which sends an email when submitted, I want to have the javascript alert when complete but am returning issues.

The javascript:

jQuery(document).ready(function() {

    $.ajax({
        url: './contactengine.php',
        type: 'GET',
        dataType: 'JSON',
        success: function(response) {
                        alert("GOOD");
                },
        error: function() {
                        alert("BAD");
                }
    });

});

The php:

<?php

$EmailFrom = "emailname@gmail.com";
$EmailTo = "emailto@gmail.com";
$Subject = "new contact from website";
$Name = Trim(stripslashes($_POST['Name'])); 
$company = Trim(stripslashes($_POST['company'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "
";
$Body .= "company: ";
$Body .= $company;
$Body .= "
";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "
";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "
";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success)
{
    $result = array('success' => true);
}
else
{
    $result = array('success' => false, 'message' => 'Something happened');
    header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
}
  echo json_encode($result);
?>

When I submit the form I get redirected to a page which says the following:

{"success":true}

When I should be staying on the same page and just alerting good.

  • 写回答

3条回答 默认 最新

  • duanjing1023 2012-04-20 23:00
    关注

    The problem here is in your script I think, from the behaviour you describe it sounds like your PHP is doing the job.

    I've made a few corrections to the ajax call based on the PHP code, try this and see if it helps.

    $.ajax({
            url: 'contactengine.php', //removed ./ - that's wrong I think
            type: 'POST',  //you are accessing $_POST not $_GET in the PHP file
            data: $('myform').serialize(), //you need to send some data atleast
            dataType: 'JSON',
            success: function(response) {
                            alert("GOOD");
                    },
            error: function() {
                            alert("BAD");
                    }
        });
    

    What I've done is removed ./ from the front of your URL - it should be either nothing, / for the root directory, or ../ for one directory up from the current directory. If the file is in the same directory as your page, keep it like I've done.

    I've changed type from GET to POST - you are accessing the PHP $_POST global in your script, so the PHP would have been looking in the wrong place for variables.

    Last but not least, I've put some data in. You need to pass in those variables that the PHP expects - so I've used a serialize() call. Replace the jQuery selector so that it will target your form, and it should pass in everything in your form to the PHP.

    For more detail on the jQuery ajax function, see here: http://api.jquery.com/jQuery.ajax/

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)