doutuo6048 2015-03-03 20:13
浏览 48
已采纳

通过ajax传递数组和其他数据

This is my javascript function.... But my php controller getting all values but not the array not sure why ? Need help..... Thanks in advance :)

function submitForm(){
    var id = $('#id').val(); 
    var supplier_id = $('#supplier_id').val();
    var description = $('#description').val();
    var numofpro = $('#numofpro').val();

    var product_id = new Array();
    for(var i=0;i<numofpro;i++){
        product_id[i] = $('#product_id'+(i+1)).val();               
    }
    var payment_mode = $('#payment_mode').val();
    //description = description.replace(new RegExp('?
','g'), '<br />');     
    $.ajax({
            url: "<?= base_url(); ?>edit/save_purchase", //The url where the server req would we made.
            data: "id="+id+"&supplier_id="+supplier_id+"&description="+description+"&product_id="+product_id+"&payment_mode="+payment_mode,
            dataType: "html", //Return data type (what we expect).
            beforeSend:function(){
                //alert("asds");
            },
            success: function(data) {
                //alert("Edited");
                alert(data);

            }
        });
}
  • 写回答

2条回答 默认 最新

  • du7133 2015-03-03 20:53
    关注

    because you pass a string, not an array :) try it:

    $.ajax({
            url: "<?= base_url(); ?>edit/save_purchase",
            type: "POST"
            data: {id : id, supplier_id : supplier_id, description : description, product_id : product_id, payment_mode : payment_mode},
            dataType: "json",
            beforeSend:function(){
               //alert("asds");
            },
            success: function(data) {
            //alert("Edited");
                alert(data);
             }
            });
    

    in you php use :

    $arr = json_decode($_POST);
    //some php code
    //some $result;
    // if $result arr then
    echo json_encode($result);
    // else 
    echo json_encode(array('data' => $result))
    

    hope this helps...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符