如何传递下拉选择的值并使用ajax插入到mysql数据库中

时间:2014-01-14 11:00:54

标签: php jquery mysql ajax

下面的代码从数据库中获取供应商,我想将选定的供应商插入到另一个数据库中,我尝试但是获得的价值是未定义,任何人都可以指导我如何做到这一点:

<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);
}

5 个答案:

答案 0 :(得分:3)

尝试这样的事情

$(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;
     });
});

答案 1 :(得分:0)

将dataString作为对象传递给数据:{dataString}

答案 2 :(得分:0)

尝试改变这一点:

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

到此:

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

试试这个:

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;
    }
}

答案 3 :(得分:0)

这是您需要的HTML和JavaScript:

<!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>

答案 4 :(得分:0)

尝试使用JSON表示法:

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