Php 在AJAX jquery中使用数组

Php 在AJAX jquery中使用数组,php,jquery,ajax,json,Php,Jquery,Ajax,Json,在json-encode()中的AJAX jquery函数中使用数组 嗨,我是阿贾克斯的新手 我有一个un页面,我想调用ajax请求在此页面上添加一些内容。 export.php <div class="row"> <div class="span12"> <select id="listTable" name="listTable"> <option value="appel">Appels<

在json-encode()中的AJAX jquery函数中使用数组

嗨,我是阿贾克斯的新手

我有一个un页面,我想调用ajax请求在此页面上添加一些内容。 export.php

<div class="row">
    <div class="span12">
        <select id="listTable" name="listTable">
            <option value="appel">Appels</option>
            <option value="dossier">Dossiers</option>
            <option value="commande">Commandes Fournisseur</option>
         </select>
    </div>
</div>

<div class="row">
    <div class="span12">
         <button class="btn btn-primary" onClick="selectTable()">Select</button>
    </div>
</div>

<div id ="columns" class="row" style="display:none;">
    <div class="span12">
         <div id="columns-check" style="">
            <!-- Here will be displayed the content of the ajax request-->
         </div>
    </div>
</div>

<script type="text/javascript" src="_module/ExportFichier/exportFile/ajax/requestExport.js"></script>
我没有办法从requestColumns.php文件返回数组到jquery Ajax请求,然后用它修改页面export.php的DOM。 谢谢你的帮助。

这会有帮助吗?

它将变成这样:

for (var i in data.<YOUR_KEY>) {
     $('#columns-check').prepend('Line: ' + data.<YOUR_KEY>[i]);
}
for(数据中的变量i){
$(“#列检查”).prepend('行:'+数据[i]);
}
功能选择表(表){
变量表=$(“#列表表”).val();
$.ajax({
url:“\u module/ExportFichier/exportFile/ajax/requestColumns.php”,
类型:“POST”,
数据:“表=”+表,
数据类型:“json”,
成功:功能(数据){
$('#columns').css('display','block');
$(“#列检查”).empty();

对于(i=0;i,您可以根据需要尝试更改我的代码。但总体思路如下:

//Your php code

   //Retrieiving data from AJAX
   $selctValue = $_POST["table"];

    //Send data from php to javascript. Using JSON you can send what you want.
    $arrToJSON = array(
        "dataPHPtoJs"=>"yourData",
        "asYouWant"=>"<div class=\".class1\">soemting</div>"    
    );  
    echo json_encode(array($arrToJSON));


    //Your javascript function
    function selectTable(){



    //Data you want to send to php. As many as you want.....
    var dt={ 
              table:$("#listTable").val()
            };



    var request =$.ajax({//http://api.jquery.com/jQuery.ajax/
                            url: "_module/ExportFichier/exportFile/ajax/requestColumns.php",
                            type: "POST",
                            data: dt,
                            dataType: "json"
                        });     



        //Ajax Done catch JSON from PHP 
        request.done(function(dataset){

            //Retrieiving information from php using JSON. Note, you can create html objects here as you did, or if you want your project more dinamicaly you can send html code from php (as I do)

            for (var index in dataset){ 
                 dataPHPtoJsJS=dataset[index].dataPHPtoJs;
                 asManyasYouWantJS=dataset[index].asYouWant;

                 $('#columns-check').prepend('<div>asManyasYouWantJS</div>');

             }

             //JavaScript conditions. Here you can control the behaivior of your html object, based on your PHP response
             if(dataPHPtoJsJS test your condition){
                //Do what you want to do..... 
                $('#columns').css('display','block');
                $('#columns-check').empty();
             }

        });     

  }
//您的php代码
//从AJAX检索数据
$selctValue=$_POST[“表”];
//将数据从php发送到javascript。使用JSON可以发送您想要的内容。
$arrToJSON=array(
“dataPHPtoJs”=>“yourData”,
“asYouWant”=>“soemting”
);  
echo json_编码(数组($arrToJSON));
//您的javascript函数
函数selectTable(){
//要发送到php的数据。任意数量。。。。。
var dt={
表:$(“#列表表”).val()
};
var请求=$.ajax({//http://api.jquery.com/jQuery.ajax/
url:“\u module/ExportFichier/exportFile/ajax/requestColumns.php”,
类型:“POST”,
数据:dt,
数据类型:“json”
});     
//Ajax从PHP捕获JSON
request.done(函数(数据集){
//使用JSON从php检索信息。注意,您可以像以前一样在这里创建html对象,或者如果您想让项目更直观,可以从php发送html代码(就像我一样)
对于(数据集中的var索引){
dataPHPtoJsJS=dataset[index].dataPHPtoJs;
asManyasYouWantJS=dataset[index].asmyouwant;
$(“#列检查”).prepend('asManyasYouWantJS');
}
//在这里,您可以根据PHP响应控制html对象的行为
如果(dataPHPtoJsJS测试您的条件){
//做你想做的事。。。。。
$('#columns').css('display','block');
$(“#列检查”).empty();
}
});     
}

谢谢大家的支持。这样做对我很有帮助。 这是我的代码:

function selectAllColumns(){

    var table = $("#listTable").val();

        $.ajax({

            url: "_module/ExportFichier/exportFile/ajax/requestAllColumns.php",
            type: "POST",
            data: "table="+ table,
            dataType: 'json',

            success: function(data){
                console.log('The Ajax request WORKS!');

                $('#columns').css('display','block');
                $('#columns-check').empty();

                for (var key in data) {
                    if (data.hasOwnProperty(key)) {
                        var id = Math.random();
                        $('#columns-check').prepend('<div style="display: inline-block; margin: 20px 20px 20px 0;"><input type="checkbox" id="'+id+'" class="field" value="'+data[key]+'" style="display: inline-block; margin:0 5px -1px 0;"><label for="'+id+'" class="field" style="display: inline-block;">'+data[key]+'</label></div>');
                    }
                }
            },

            error: function(){
                console.log('The Ajax request did not works!');
            }
        });
}
函数selectAllColumns(){
变量表=$(“#列表表”).val();
$.ajax({
url:“\u module/ExportFichier/exportFile/ajax/requestAllColumns.php”,
类型:“POST”,
数据:“表=”+表,
数据类型:“json”,
成功:功能(数据){
log('Ajax请求有效!');
$('#columns').css('display','block');
$(“#列检查”).empty();
for(var输入数据){
if(data.hasOwnProperty(key)){
var id=Math.random();
$(“#列检查”).prepend(“”+data[key]+“”);
}
}
},
错误:函数(){
log('Ajax请求不起作用!');
}
});
}

您可以在这里找到如何循环遍历对象[是$columns数组还是$columns对象?$columns它是数组。PHP脚本的响应(您的数组编码为JSON)作为
success
回调函数的第一个参数传递,您调用了
data
,因此您需要迭代数据。之后您要做什么取决于;您没有告诉我们JSON看起来像什么,也没有告诉我们您想用它做什么,我可以看到,因此很难比这更精确。
function selectTable(table){

var table = $("#listTable").val();

    $.ajax({

        url: "_module/ExportFichier/exportFile/ajax/requestColumns.php",
        type: "POST",
        data: "table="+table,
        dataType: 'json',

        success: function(data){

            $('#columns').css('display','block');
            $('#columns-check').empty();

            for(i=0; i<data.length; i++){
                $('#columns-check').prepend('<div>' + data.arraykey + '</div>'); //change arraykey with your index name
            }
        },

        error: function(){
            alert('The Ajax request did not works!');
        }


    });
}
//Your php code

   //Retrieiving data from AJAX
   $selctValue = $_POST["table"];

    //Send data from php to javascript. Using JSON you can send what you want.
    $arrToJSON = array(
        "dataPHPtoJs"=>"yourData",
        "asYouWant"=>"<div class=\".class1\">soemting</div>"    
    );  
    echo json_encode(array($arrToJSON));


    //Your javascript function
    function selectTable(){



    //Data you want to send to php. As many as you want.....
    var dt={ 
              table:$("#listTable").val()
            };



    var request =$.ajax({//http://api.jquery.com/jQuery.ajax/
                            url: "_module/ExportFichier/exportFile/ajax/requestColumns.php",
                            type: "POST",
                            data: dt,
                            dataType: "json"
                        });     



        //Ajax Done catch JSON from PHP 
        request.done(function(dataset){

            //Retrieiving information from php using JSON. Note, you can create html objects here as you did, or if you want your project more dinamicaly you can send html code from php (as I do)

            for (var index in dataset){ 
                 dataPHPtoJsJS=dataset[index].dataPHPtoJs;
                 asManyasYouWantJS=dataset[index].asYouWant;

                 $('#columns-check').prepend('<div>asManyasYouWantJS</div>');

             }

             //JavaScript conditions. Here you can control the behaivior of your html object, based on your PHP response
             if(dataPHPtoJsJS test your condition){
                //Do what you want to do..... 
                $('#columns').css('display','block');
                $('#columns-check').empty();
             }

        });     

  }
function selectAllColumns(){

    var table = $("#listTable").val();

        $.ajax({

            url: "_module/ExportFichier/exportFile/ajax/requestAllColumns.php",
            type: "POST",
            data: "table="+ table,
            dataType: 'json',

            success: function(data){
                console.log('The Ajax request WORKS!');

                $('#columns').css('display','block');
                $('#columns-check').empty();

                for (var key in data) {
                    if (data.hasOwnProperty(key)) {
                        var id = Math.random();
                        $('#columns-check').prepend('<div style="display: inline-block; margin: 20px 20px 20px 0;"><input type="checkbox" id="'+id+'" class="field" value="'+data[key]+'" style="display: inline-block; margin:0 5px -1px 0;"><label for="'+id+'" class="field" style="display: inline-block;">'+data[key]+'</label></div>');
                    }
                }
            },

            error: function(){
                console.log('The Ajax request did not works!');
            }
        });
}