arok
arok

Reputation: 1845

how to pass dropdown selected values and insert into mysql database using ajax

The code below takes suppliers from database, I want to insert the selected supplier to another database, I tried but getting the value is undefined , can anyone guide me how to do that:

<select name="supplier" id="supplier"> 
    <option value="">Select supplier</option>

    <?php
    $sqlsupplier=mysql_query("SELECT supplier_id  FROM supplier");
    while($row=mysql_fetch_assoc($sqlsupplier)){
        echo "<option value = '{$row['supplier_id']}'";
        if ($selected_supplier == $row['supplier_id'])
            echo "selected = 'selected'";
        echo "> {$row['supplier_id']} </option>";
    }
   ?>

    </select>

ajax

$(function() {
    $(".billingadddet_button").click(function() {


    var CPH_GridView1_supplier =  $("#supplier option:selected").val();

    var dataString = 'CPH_GridView1_supplier='+CPH_GridView1_supplier;


    if(CPH_GridView1_supplier=='')
    {
    alert("Please Enter Some Text");
    }
    else
    {

    $.ajax({
    type: "POST",
    url: "insertdetailed.php",
    data: dataString,
    cache: false,
    success: function(html){
    $("#display").after(html);

    window.location = '?action=billingdatainputandexportdetailedreport';

    }
    });
    } return false;
    });
    });

insertdetailed.php

if(isSet($_POST['CPH_GridView1_supplier']))

{

$supplier=$_POST['CPH_GridView1_supplier'];     


$sql_insert="insert into billingdetailedreport(supplier,created) values ('$supplier',".time().")";
//print "Here";
print $sql_insert;
mysql_query($sql_insert);
}

Upvotes: 1

Views: 5659

Answers (5)

Jai
Jai

Reputation: 74738

try changing this:

if(isSet($_POST['CPH_GridView1_supplier']))
//---^-----------------------------this upper case "S"

to this:

if(isset($_POST['CPH_GridView1_supplier']))

Try this:

if(isset($_POST['CPH_GridView1_supplier'])){

    $supplier=$_POST['CPH_GridView1_supplier'];     


    $sql_insert="insert into billingdetailedreport(supplier,created) 
                 values ('$supplier',".time().")";
    if(mysql_query($sql_insert)){
       echo 'SUCCESS';
       print $sql_insert;
    }else{
       echo 'FAILED';
       print $sql_insert;
    }
}

Upvotes: 0

rajesh kakawat
rajesh kakawat

Reputation: 10896

try something like this

$(function() {
     $(".billingadddet_button").click(function() {
        var supplier_val =  $("#supplier").val();
        if(supplier_val== '')
        {
            alert("Please Enter Some Text");
        }else{
            $.ajax({
                type: "POST",
                url: "insertdetailed.php",
                data: {CPH_GridView1_supplier : supplier_val},
                cache: false,
                success: function(html){
                    $("#display").after(html);
                    window.location = '?action=billingdatainputandexportdetailedreport';
                };
            });
        }
        return false;
     });
});

Upvotes: 4

naoxink
naoxink

Reputation: 597

Try with JSON notation:

$.ajax({
    type: "POST",
    url: "insertdetailed.php",
    data: { supplier_id: CPH_GridView1_supplier },
    success: functi..

Upvotes: 0

Petar Vasilev
Petar Vasilev

Reputation: 4735

This is the HTML and JavaScript you need:

<!doctype html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">

        function saveToDatabase() {
            var selectValue = $('#selectBoxID').val();

            // post to php script
            $.ajax({
                type: 'POST',
                url: 'insertdetailed.php',
                data: {selectValueBox: selectValue }
            }
        }

    </script>
</head>
<body>
    <select id="selectBoxID" onselect="saveToDatabase()">
       <option value="1">Value 1</option>
       <option value="2">Value 2</option>
    </select>
</body>
</html>

Upvotes: 0

Learner
Learner

Reputation: 231

pass dataString as an object to data:{dataString}

Upvotes: 0

Related Questions