0

我不知道为什么这不起作用。我试图通过 ajax 传递数据。我用了很多次,但由于某种原因它不起作用。它什么也没有返回。

这是js

$('#contactformbtn').click(function(){
    var fullname = $('#fullname').val();
    var youremail = $('#youremail').val();
    var subject = $('#subject').val();
    var yourmessage = $('#yourmessage').val();

    var datastring = 'fullname=' + fullname + '&youremail=' + youremail + '&subject=' + subject + '&yourmessage=' + yourmessage;

    $.ajax({
        type: "POST",
        url: "ajax-contact.php",
        data: datastring,
        success: function(status){
            alert(status);
        }
    });

    //alert(datastring);
    return false;
});

这是php

<?php
require 'core/init.php';

if(isset($_POST)){
    $fullname = $_POST['fullname'];
    $youremail = $_POST['youremail'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    echo $fullname;
}
?>

在 chrome 中,我正在检查控制台并收到 2 个错误

Uncaught TypeError: Cannot read property '1' of null
Uncaught TypeError: Cannot read property '3' of null

我的完整js是这样的,(以防错误不是联系功能)

$(document).ready(function(){

$("#download-btn").bind("click", downloadfile);
$(".download-link").hide();

function downloadfile()
{
    var dl = $("#dl").val();
    var counter = 10;

    var interval = setInterval(function(e) {
        counter--;

        if(counter > 0){
            $("#download-btn").html("Your Download will begin in " + counter + " Seconds");
            $("#download-btn").attr("disabled", "disabled");
        } else {
            window.location.href="http://www.forwardfiles.com/get_file.php?i="+dl;
            $("#download-btn").hide();
            $(".download-status").html("<h3 class='center'>Download is complete</h3>");
            clearInterval(interval);
        }

    }, 1000);
}


$('#contactformbtn').click(function(){
    var fullname = $('#fullname').val();
    var youremail = $('#youremail').val();
    var subject = $('#subject').val();
    var yourmessage = $('#yourmessage').val();

    var datastring = 'fullname=' + fullname + '&youremail=' + youremail + '&subject=' + subject + '&yourmessage=' + yourmessage;

    $.ajax({
        type: "POST",
        url: "ajax-contact.php",
        data: datastring,
        success: function(status){
            alert(status);
        }
    });

    //alert(datastring);
    return false;
});

});
4

3 回答 3

0

我得到它的工作,我的代码很好。问题是我没有运行 php 5.3 版,所以我更新了它并且它的工作:D。无论如何谢谢你的帮助

于 2013-10-29T21:15:50.633 回答
0

你试过.serialize吗?

$('#contactformbtn').click(function(){
$.ajax({
    type: "POST",
    url: "ajax-contact.php",
    data: $("form").serialize(),
    success: function(status){
        alert(status);
    }
});

//alert(datastring);
return false;

});

于 2013-10-29T19:56:33.987 回答
0

如果这些值,请检查您的 HTML 代码

var fullname = $('#fullname').val();
    var youremail = $('#youremail').val();
    var subject = $('#subject').val();
    var yourmessage = $('#yourmessage').val();

已经定义了他们的 id 而不仅仅是“名称”标签

于 2013-10-29T20:07:19.840 回答