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

Php 如何使用ajax传递下拉选择的值并插入mysql数据库,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,下面的代码从数据库中获取供应商,我想将所选供应商插入另一个数据库,我尝试了,但获取的值未定义,有人可以指导我如何操作: <select name="supplier" id="supplier"> <option value="">Select supplier</option> <?php $sqlsupplier=mysql_query("SELECT supplier_id FROM supplier"); w

下面的代码从数据库中获取供应商,我想将所选供应商插入另一个数据库,我尝试了,但获取的值未定义,有人可以指导我如何操作:

<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>
insertdetailed.php


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

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

尝试更改以下内容:

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

if(isset($_POST['CPH_GridView1_supplier']))
试试这个: 尝试更改此选项:

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

if(isset($_POST['CPH_GridView1_supplier']))
试试这个:
试试这样的

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

试试这样的

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

这是您需要的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>

这是您需要的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>

尝试使用JSON表示法:

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

尝试使用JSON表示法:

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

$flash.fadeIn400.html;这是在做什么?我删除了$flash.fadeIn400.html;它没有做任何事情我认为你的问题就在这里,如果设置不使用大写字母S$flash.fadeIn400.html;这是在做什么?我删除了$flash.fadeIn400.html;它没有做任何事情我想你的问题就在这里如果设置不使用大写,因为我看到你的代码没有从服务器端回显任何内容,那么你想从那里显示什么?看,我尝试像这样运行我的查询插入billingdetailedreportbillingmonthid、clientid、clientname、billingyear、billingmonth、supplier、created Value“26”,“297in”,“infob”,“2014”,“12”,“undefined”,1389697724当我看到你的代码时,你没有从服务器端回显任何东西,所以你想从那里显示什么?我试着像这样运行我的查询,插入billingdetailedreportbillingmonthid,clientid,clientname,billingyear,billingmonth,supplier,创建值“26”,“297in”,“infob”,“2014”,“12”,“未定义”,1389697724