简体   繁体   English

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

[英]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 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);
}

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

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

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

This is the HTML and JavaScript you need: 这是您需要的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>

Try with JSON notation: 尝试使用JSON表示法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM