简体   繁体   English

为什么我没有收到从JS到PHP的变量?

[英]Why am I not receiving variable from JS to PHP?

I am trying to send a variable from JS to php through ajax but I'm not able to get in php file. 我正在尝试通过ajax将变量从JS发送到php,但无法进入php文件。

JS JS

var names = ['lee','carter'] ;
$.ajax({
    type: "POST",
    url: "http://localhost/test/ajax.php",
    data: {name:names},
}).done(function() {
    location.href = 'http://localhost/test/ajax.php' ;
});

PHP 的PHP

print_r($_POST);

this is showing an empty array but when I do console.log(data) it shows an array in console.log 这显示了一个空数组,但是当我执行console.log(data)时,它在console.log中显示了一个数组

   var names = ['lee','carter'] ;
    $.ajax({
        type: "POST",
        url: "http://localhost/test/ajax.php",
        data: {name:names},
    }).done(function(data) {
        console.log(data) ;
    });

Edit: (by mega6382) I believe OP wants to open a page in browser with post params, which cannot be done by AJAX. 编辑:( 按mega6382)我相信OP希望在浏览器中使用post params打开页面,而AJAX无法完成。 All others who answered got mistaken by the AJAX code in the question and started providing AJAX solutions, without realizing what OP is trying to do. 回答问题的所有其他人都被问题中的AJAX代码所误解,并开始提供AJAX解决方案,而没有意识到OP试图做什么。 If you were to read OP's comments on Jeroen's answer. 如果您要阅读OP对Jeroen答案的评论。

The problem is with what you do when the ajax request finishes: 问题在于ajax请求完成时的操作:

}).done(function() {
    location.href = 'http://localhost/test/ajax.php' ;
});

Here you are re-directing to http://localhost/test/ajax.php (requesting it a second time...), using a GET request so $_POST is indeed empty. 在这里,您使用GET请求$_POSThttp://localhost/test/ajax.php (第二次请求...),因此$_POST确实为空。

只需在您的php文件中执行以下操作即可接收json格式的字符串

echo json_encode(['success' => true]);

Instead of ajax try sending a dynamically generated form like: 代替ajax,尝试发送动态生成的表单,例如:

var names = ['lee','carter'] ;
var newForm = $('<form>', {
    'action': "http://localhost/test/ajax.php",
    'target': '_top',
    'method': 'POST'
});
names.forEach(function (item, index)
{
    newForm.append($('<input>', {
        'name': 'name[]',
        'value': item,
        'type': 'hidden'
    }));
});
$(document.body).append(newForm);
newForm.submit();

This will send the values over POST via a form. 这将通过表单通过POST发送值。 It will do both redirect to the new page and send post vals. 它将同时重定向到新页面并发送后价。

Since you're using an AJAX request, in this case POST , you could use the _REQUEST method in php for obtaining the JS variable. 由于您使用的是AJAX请求,在本例中为POST ,可以在php中使用_REQUEST方法来获取JS变量。 In your case try: 在您的情况下,请尝试:

 $names = $_REQUEST['name']

 echo $names;

/* Or try JSON_ENCODE if echo isn't working*/

$json = json_encode($names)

echo ($json);
  var names = ['lee','carter'] ;
    JSON.stringify(names);
    $.ajax({
        type: "POST",
        dataType: 'json',
        url: "http://localhost/test/ajax.php",
        data: {name:names}
    }).done(function() {
            console.log('ok');
    });

This is a successful ajax call. 这是一个成功的ajax调用。 Now in order to "check by yourself" that is working you don't have to redirect to the other page because that way you refresh and lose the data. 现在,为了“自己检查”是否有效,您不必重定向到另一页,因为那样可以刷新并丢失数据。 What you have to do is: 您要做的是:

  1. Open developer tab in your browser 在浏览器中打开开发人员标签
  2. Go to network tab 转到网络标签
  3. Locate your ajax call (it has the name of your php class that you do the call) 找到您的ajax调用(它具有您执行该调用的php类的名称)
  4. go to response tab and there you have your php output 去响应选项卡,那里你有你的PHP输出

This link is to help you understand how to debug ajax call 该链接可帮助您了解如何调试Ajax调用

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

相关问题 为什么我在这个PHP代码中收到通知字符串偏移量? - Why am I receiving a notice string offset in this php code? 为什么我收到 null 作为对包含数据的 sql 列的响应? (PHP) - Why am I receiving null as a response for an sql column with data in it? (PHP) 为什么在此php代码中收到未初始化的字符串偏移量? - Why am I receiving an uninitialized string offset in this php code? 我没有收到邮件php - I am not receiving mail php 我收到一个未定义的变量,但是我的变量是在从另一个页面接收主变量(myID)的同时定义的 - I am receiving an undefined variable but my variables are defined while receiving main variable (myID) from another page 为什么我无法在PHP中传递变量 - Why I am unable to pass a variable in PHP 我通过PHP从MSSQL查询中收到奇怪的输出 - I am receiving strange output from a MSSQL query via PHP 为什么我从请求中收到的响应没有正确过滤? - Why is the response I am receiving from my request not filtering correctly? 为什么这个 JS 和 PHP 代码不起作用? (我正在尝试将值从 JS 发送到 PHP) - Why doesn't this JS & PHP code work? (I am trying to send a value from JS to PHP) 将变量从.js传递给PHP,然后在响应中接收新的var返回? - Passing variable from .js to PHP and then receiving a new var back in response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM