Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过AJAX将数组发送到php_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 通过AJAX将数组发送到php

Javascript 通过AJAX将数组发送到php,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我尝试通过ajax将JS数组发送到php文件 JS代码: $('#basket').on('click',function(){ $.ajax({ type: "GET", url: "basket.php", data: {vektor: itemNameArray}, success: function(){ window.location.href ="basket.php";

我尝试通过ajax将JS数组发送到php文件

JS代码:

$('#basket').on('click',function(){

    $.ajax({
        type: "GET",
        url: "basket.php",
        data: {vektor: itemNameArray},
        success: function(){

            window.location.href ="basket.php";
            console.log(itemNameArray);

        }
    });

});
php代码:

<?php
    echo("<script>console.log('PHP: test 1');</script>");
    if(isset($_GET['vektor'])){
        echo("<script>console.log('PHP: test 2');</script>");
        $vektor = $_post['vektor'];
        echo("<script>console.log('PHP: ".$vektor."');</script>");
    }
<?php
echo("<script>console.log('PHP: test 1');</script>");
  if(isset($_POST['vektor'])){
     echo("<script>console.log('PHP: test 2');</script>");
     $vektor = $_POST['vektor'];
     echo("<script>console.log('PHP: ".$vektor."');</script>");
 }

我已将GET和post更改为post。你搞乱了帖子,这是第一个重大错误

但是,我的get键vektor似乎不起作用。我进入日志 我的阵列和测试1的第一个回声,但不是第二个(post not post)。我需要发送 一个数组到php文件来创建html代码。(对于那种类型的数据,最好是POST),这是因为我需要 打开“basket.php”查看结果

php代码:

<?php
    echo("<script>console.log('PHP: test 1');</script>");
    if(isset($_GET['vektor'])){
        echo("<script>console.log('PHP: test 2');</script>");
        $vektor = $_post['vektor'];
        echo("<script>console.log('PHP: ".$vektor."');</script>");
    }
<?php
echo("<script>console.log('PHP: test 1');</script>");
  if(isset($_POST['vektor'])){
     echo("<script>console.log('PHP: test 2');</script>");
     $vektor = $_POST['vektor'];
     echo("<script>console.log('PHP: ".$vektor."');</script>");
 }

您好,您可以这样做:

您的php脚本:

if (isset($_POST["action"])) {
   $action = $_POST["action"];
   switch ($action) {
    case 'SLC':
     if (isset($_POST["id"])) {
       $id = $_POST["id"];
       if (is_int($id)) {
       $query = "select * from alumni_users where userId = '$id' ";
        $update = mysqli_query($mysqli, $query);
         $response = array();
         while($row = mysqli_fetch_array($update)){
           .......fill your response here

         }
         echo json_encode($response);
        }
       }
      break;

    }
 }
Where action is a command you want to do SLC, UPD, DEL etc and id is a parameter

then in your ajax:



function getdetails() {
 var value = $('#userId').val(); // value can be your array ! note: if you send a object json_encode(json_decode(,MyObj,true))
                         return $.ajax({
                              type: "POST",
                              url: "getInfo.php",
                              data: {action: "SLC",id: value } 
                          })
                      }
                      call it like this:



   getdetails().done(function(response){
                  var data=JSON.parse(response);
                  if (data != null) {
                  //do somthing with your Data
                  }
                  })

您有$\u post而不是$\u GET here:
$vektor=$\u post['vektor']。第二:在收到php的回复后,您直接重定向,但甚至没有将php发送回ajax(在成功函数中)ajax在后台发送数据,通常在成功函数中处理结果。但是,您的代码看起来像是要模拟表单提交,在这种情况下,AJAX是错误的方法。您需要的是
;然后您将希望用1替换该AJAX调用。将输入值设置为
JSON.stringify(itemNameArray)
和2。在表单上调用
submit()
。@Jeff如果我删除第一个回显,它会工作吗?我使用POST-before代替GETNo,因为
window.location.href=“basket.php”
是一个新的GET请求,将导致
$\u GET
$\u POST
都为空。是的,我也这么认为。这意味着您不需要为此使用AJAX。你所要做的就是伪造一个表单提交;正如我在第一篇评论中解释的,AJAX是错误的方法。使用这个:答案只解决了许多问题中的一个。另外,解释一下你改变了什么以及为什么会改变也会很有帮助。我会给出一个答案。修复了我看到的错误,可能会有问题。这看起来像是一个代码示例。