randytan
randytan

Reputation: 1039

Inserting Variables From Jquery Always Fails

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

Upvotes: 0

Views: 141

Answers (2)

Mark
Mark

Reputation: 1386

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.

Upvotes: 1

Abhishek Punj
Abhishek Punj

Reputation: 289

Although I do not get what you are saying completely, still based on your description the problem can be you sending the variable using ajax call. If the ajax call is async you might not get the value by the time you use it.

So you can either set async:false in the ajax call (which is not a good way) or trigger any thing that uses that variable from within the success callback handler of the ajax call.

It would be really helpful if you can share the piece of code which explains the following statement of yours.... "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"

Upvotes: 0

Related Questions