简体   繁体   English

$ .post不向php脚本发送数据

[英]$.post not sending data to php script

Okay I am at a loss for what is going wrong. 好吧,我因为出了什么问题而感到茫然。 I am trying to pass the form data to my php script from a simple jQuery script but for some reason when I try to access $_POST data php says that $_POST is empty? 我试图从一个简单的jQuery脚本将表单数据传递给我的PHP脚本,但出于某种原因,当我尝试访问$ _POST数据时,php说$ _POST是空的?

Here we go, so I have the following jQuery and php scripts 我们走了,所以我有以下jQuery和php脚本

jQuery jQuery的

var post = $('#cform').serialize();
console.log("POST DATA: " + post);
$.post(action, post, function(data){
    document.getElementById('message').innerHTML = data;
    $('#message').slideDown('slow');
    $('#cform img.contact-loader').fadeOut('slow',function(){$(this).remove()});
    $('#submit').removeAttr('disabled');
    if(data.match('success') != null) $('#cform').slideUp('slow');
 });

PHP PHP

$fname  = $_POST['fname'];
$lname  = $_POST['lname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments']; 

The console log of var post looks like this var post的控制台日志看起来像这样

POST DATA: fname=Daniel&lname=Jarvis&email=test%40gmail.com&phone=4444444444&comments=hello

And the var_dump of $_POST says this $ _POST的var_dump说明了这一点

array(0) { } 

I have no clue why this is giving me so many problems so any help would be greatly appreciated. 我不知道为什么这会给我这么多问题所以任何帮助都会非常感激。

PS I have also tried simply doing this for the post data but it still was not working. PS我也尝试过为帖子数据做这个,但它仍然无法正常工作。

var post = {fname: $('#fname').val(), lname: $('lname').val(), ...} //you get the idea

The console.log looked like this console.log看起来像这样

{fname: "Dan", lname: "Jarvis", ...}

But when I var_dumped the $_POST variable it still said 但是当我var_dumped $ _POST变量时,它仍然说

array(0) { } 

*There are several issues in your code, *您的代码中存在多个问题,

1.Add event which will trigger ajax. 1.添加将触发ajax的事件。

2.Your php script does not echo any data. 2.您的PHP脚本不回显任何数据。

3.No return false or prevent defaul to stop form submission manually.(I think this is the main issue) 3.不返回false或阻止默认手动停止表单提交。(我认为这是主要问题)

check the solution:* 检查解决方案:*

HTML: HTML:

    <form id="form">

    <input type="text" name="fname">
    <input type="text" name="lname">
    <input type="text" name="email">
    <input type="text" name="phone">
    <input type="text" name="comments">
    <input type="submit" name="submit" value="submit">

    </form>

    <span id="message"></span>

Javascript: 使用Javascript:

    $("#form").submit(function(){

        var post = $('#form').serialize();
        console.log("POST DATA: " + post);
        $.post('target.php', post, function(data){
        document.getElementById('message').innerHTML = data;
        $('#message').slideDown('slow');
        $('#cform img.contact-loader').fadeOut('slow',function(){$(this).remove()});
        $('#submit').removeAttr('disabled');
        if(data.match('success') != null) $('#cform').slideUp('slow');

     });
    return false;
    })

PHP(I assume that your php script is target.php): PHP(我假设你的php脚本是target.php):

  $fname  = $_POST['fname'];
  $lname  = $_POST['lname'];
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $comments = $_POST['comments']; 

  echo $fname.$lname.$email.$phone.$comments;

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

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