简体   繁体   English

将 PHP $_POST 数组传递给 javascript/jQuery 以通过 ajax 发送回 PHP

[英]Passing a PHP $_POST array to javascript/jQuery to send back to PHP via ajax

I need to pass the $_POST array currently available on the page into my javascript.我需要将页面上当前可用的$_POST数组传递到我的 javascript 中。 jQuery needs to $.post this array back to php and get it processed. jQuery 需要将该数组$.post回 php 并对其进行处理。

I've tried the below and my jquery and post is fine but my echo $items;我已经尝试了以下和我的 jquery 和帖子很好,但我的echo $items; is drawing a blank on receipt on the php side.在 php 侧的收据上绘制空白。

any ideas please?请问有什么想法吗?

php : php

$items = json_encode($_POST);

jquery : jquery

var data = {
'action': 'someaction',
'items': <?php echo $items;?>
 } ;

$.post(ajaxurl, data, function(response) {
                    console.log( response ) ;
});

I'm expecting the $_POST array to be passed via $.post in jquery back to my php function.我期待$_POST数组通过$.post在 jquery 中传递回我的 php function。

yes i've found an answer.是的,我找到了答案。

The line i'm missing is:我缺少的行是:

<?php echo "var items = " . $items . ";" ;?>

so this works for me:所以这对我有用:

**PHP**
$items = json_encode( $_POST ) ;

**JS**
var items;
<?php echo "var items = " . $items . ";" ;?>

Yes a form was already submitted....so ihave to capture what was submitted.是的,已经提交了表单……所以我必须捕获提交的内容。 Timeouts occur on server side so i compare what was posted with what has been brought back... i use ajax to process the two.服务器端发生超时,因此我将发布的内容与已恢复的内容进行比较...我使用 ajax 来处理两者。 Messy.凌乱。 But if i find a better solution i'll use it.但如果我找到更好的解决方案,我会使用它。

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

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