arok
arok

Reputation: 1845

how to post the database value to next page using ajax

i am trying to post the database retrieved value to next page using ajax.i tried but it not posting, can any one guide me how to do it

php

connected with database

$sql = "SELECT * FROM `billingdatainputandexport` WHERE id='1'"; 
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
//print $sql;
 $billingmonth=$rows['billingmonth'];

i want to pass this billing month to selectsuppliercostprice.php.

javascript

$(document).ready(function() {
    $("#supplier").on("change", function() {

        var billingmonth='$billingmonth';
        var supplier = document.getElementById("supplier").value;
        var mcc = document.getElementById("mcc").value;
        var mnc = document.getElementById("mnc").value;
        $.post('selectsuppliercostprice.php', { org: supplier,mcc: mcc,mnc: mnc,billingmonth: billingmonth }, function(result) {
            $('#suppliercost').html(result);
            }
        );
    });
});

selectsuppliercostprice.php.

   $billingmonth=$_POST['billingmonth'];

Upvotes: 0

Views: 209

Answers (5)

Jenson M John
Jenson M John

Reputation: 5689

Just Try.

Store $billingmonth value into any hidden input.

<input type="hidden" name="billing_month" id="billing_month" value="<?php echo $billingmonth; ?>"/>

Retrieve $billingmonth value in jQuery ready function using Hidden input id.

$(document).ready(function() {
    $("#supplier").on("change", function() {

        var billingmonth=$("#billing_month").val();
        var supplier = $("#supplier").val();
        var mcc = $("#mcc").val();
        var mnc =$("#mnc").val();
        $.post('selectsuppliercostprice.php', { org: supplier,mcc: mcc,mnc: mnc,billingmonth: billingmonth }, function(result) {
            $('#suppliercost').html(result);
            }
        );
    });
});

Upvotes: 1

Krish R
Krish R

Reputation: 22721

Can you try this,

PHP

$billingmonth=$rows['billingmonth'];

echo '<input type="hidden" id="billingmonth" name="billingmonth" value="'.$billingmonth.'">'; // added hidden input

Javascript:

$("#supplier").on("change", function() {
    var billingmonth= $('#billingmonth').val();
    ...
 });

Upvotes: 2

rajesh kakawat
rajesh kakawat

Reputation: 10906

make hidden field and store your value in that and read that value in javascaript and pass it with ajax

        <input type="hidden" name="some_name" value="<?php echo $billingmonth?>" id="some_name"/>

javascript code

$(document).ready(function() {
    $("#supplier").on("change", function() {

        var billingmonth=document.getElementById('billingmonth').value;
        var supplier = document.getElementById("supplier").value;
        var mcc = document.getElementById("mcc").value;
        var mnc = document.getElementById("mnc").value;
        $.post('selectsuppliercostprice.php', { org: supplier,mcc: mcc,mnc: mnc,billingmonth: billingmonth }, function(result) {
            $('#suppliercost').html(result);
            }
        );
    });
});

if you don't want to use it anyother place than

   var billingmonth= '<?php echo $billingmonth?>';

Upvotes: 1

Suraj Rawat
Suraj Rawat

Reputation: 3763

Here is the latest code : u missed i guess the php tag inside ur script tag now try :)

       $(document).ready(function() {
        $("#supplier").on("change", function() {

            var billingmonth='<?php echo $billingmonth ?>';
            var supplier = document.getElementById("supplier").value;
            var mcc = document.getElementById("mcc").value;
            var mnc = document.getElementById("mnc").value;
            $.post('selectsuppliercostprice.php', { org: supplier,mcc: mcc,mnc: mnc,billingmonth: billingmonth }, function(result) {
                $('#suppliercost').html(result);
                }
            );
        });
    });

Upvotes: 1

Yannici
Yannici

Reputation: 736

The post variable is $_POST not $POST so ... that should be right:

$billingmonth = $_POST['billingmonth'];

Also in Javascript you have to print the billingmonth variable. Javascript can't handle php-variables:

var billingmonth='$billingmonth';

to

var billingmonth='<?php echo $billingmonth; ?>';

Upvotes: 1

Related Questions