dswfyq6201 2013-10-04 09:36
浏览 49
已采纳

从Jquery插入变量总是失败

i have some problem, i made some local website using php.

I have a file called functions.php which this is the code:

public function saveAll($idmt_salesarea, $thn_bln, $no_pesanan, $tgl_pesan, $idms_langganan, $idms_kodebarang, $quantity, $harga, $jumlah, $disc_cash, $disc_kredit){
    $message = "Waiting input...";
    try{
        $con = new db();
        $conn = $con->connect();
        $query = "INSERT INTO mt_pesanan(idmt_salesarea,thn_bln,no_pesanan,tgl_pesan,idms_langganan, idms_kodebarang,quantity,harga,jumlah,disc_cash,disc_kredit) VALUES ($idmt_salesarea, '$thn_bln', '$no_pesanan', '$tgl_pesan', $idms_langganan, $idms_kodebarang, '$quantity', $harga, $jumlah, '$disc_cash', '$disc_kredit')";
        $result = mysqli_query($conn, $query) or die(mysqli_error($conn) . "  " . mysqli_errno());
        if($result == 1){
            $message = "Success";
        } else if($result == 0){
            $message = "Failed";
        }
    }catch(Exception $exc){
        echo $exc->getCode();
    }
        $con->disconnect();
        return $message;
}

i Take the input parameter from file called: index.php and pass the parameter using AJAX Jquery. The parameter itself is sent and pointing to file called insert.php

here's the insert.php file:

 <?php

  include_once 'functions.php';

    $idmt_salesarea = isset($_GET['salesarea']);
    $thn_bln = isset($_GET['thn_bln']);
    $no_pesanan = isset($_GET['nopes']);
    $tgl_pesan = isset($_GET['tglpes']);
    $idms_langganan = isset($_GET['idlangganan']);
    $idms_kodebarang = isset($_GET['idbarang']);
    $quantity = isset($_GET['quantity']);
    $harga = isset($_GET['harga']);
    $jumlah = isset($_GET['jumlah']);

    $disc_cash = isset($_GET['disc_cash']);
    $disc_kredit = isset($_GET['disc_kredit']);
    if (($disc_cash == null) || ($disc_kredit == null)) {
        $disc_cash = 0;
        $disc_kredit = 0;
    }

    $insert = new functions();
    $insert->saveAll($idmt_salesarea, $thn_bln, $no_pesanan, $tgl_pesan, $idms_langganan, $idms_kodebarang, $quantity, $harga, $jumlah, $disc_cash, $disc_kredit);

 ?>

but when i check the error, that is the variable that cannot get from insert.php file (using $_GET statement).

How proper way to gain the variable? because all the parameter is set.

I know this is combining object oriented style and old fashion php coding. Any ideas?

thanks in advance.

UPDATE

here's the index.php file using jquery ajax to sent the data

    function sendAll(){
            var tgl_pesan = $('#dpc').val();
            var sales_area = $('#sales_area').val();
            var nopes = $('#no_pesanan').val();
            var thnbln = getTahunBulan();
            var id_langganan = $('#kode_langganan').val();
            var id_barang = $('#kode_barang').val();
            var quantity = getQuantity();
            var harga = $('#harga').val();
            var jumlah = $('#jumlah').val();
            var disc_cash = $('#cash').val();
            var disc_kredit = $('#kredit').val();
            var max = $('#max').val();

            $.ajax({
                type:"POST",
                **url:"insert.php",**
                data:{
                  salesarea:sales_area,
                  thn_bln:thnbln,
                  nopes:nopes,
                  tglpes:tgl_pesan,
                  idlangganan:id_langganan,
                  idbarang:id_barang,
                  quantity:quantity,
                  harga:harga,
                  jumlah:jumlah,
                  disc_cash:disc_cash,
                  disc_kredit:disc_kredit,
                  max:max
                },
                success:function(msg){
                   alert("Data Inserted"); 
                },
                error:function(msg){
                   alert("Data Failed to save" + msg); 
                }
            });

the ajax itself is pointing to file insert.php which the insert.php is executing function from another file called functions.php

  • 写回答

2条回答 默认 最新

  • drkwpgrdb092239314 2013-10-04 09:46
    关注

    The problem is with this code:

    $idmt_salesarea = isset($_GET['salesarea']);
    $thn_bln = isset($_GET['thn_bln']);
    $no_pesanan = isset($_GET['nopes']);
    $tgl_pesan = isset($_GET['tglpes']);
    $idms_langganan = isset($_GET['idlangganan']);
    $idms_kodebarang = isset($_GET['idbarang']);
    $quantity = isset($_GET['quantity']);
    $harga = isset($_GET['harga']);
    $jumlah = isset($_GET['jumlah']);
    
    $disc_cash = isset($_GET['disc_cash']);
    $disc_kredit = isset($_GET['disc_kredit']);
    

    For each of those variables, you are assigning the result of isset(), which will evaluate to either TRUE or FALSE. If you want to bind the actual value of your $_GET input, change each line from this syntax:

    $idmt_salesarea = isset($_GET['salesarea']);
    

    To

    $idmt_salesarea = isset($_GET['salesarea']) ? $_GET['salesarea'] : '';
    

    However this code isn't really maintainable, and I would also recommend using arrays instead of passing that many arguments to your saveAll() method.

    In response to your update

    If you are sending an AJAX request with type: "POST", you cannot access your input data via the PHP $_GET super global, you have to use $_POST. However, what I said before is still valid, as you aren't binding values to your variables properly.

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

报告相同问题?

悬赏问题

  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计