1

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);
}
3
  • $("#flash").fadeIn(400).html; what this is doing? Commented Jan 14, 2014 at 11:02
  • i removed $("#flash").fadeIn(400).html;it not doing anything Commented Jan 14, 2014 at 11:04
  • i think your issue is here if(isSet do not use uppercase "S" Commented Jan 14, 2014 at 11:04

5 Answers 5

4

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;
     });
});
Sign up to request clarification or add additional context in comments.

Comments

0

pass dataString as an object to data:{dataString}

Comments

0

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>

Comments

0

Try with JSON notation:

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

Comments

0

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

2 Comments

as i see your code you are not echoing anything from the serverside, so what you want to show from there?
see i tried to run my query it like this insert into billingdetailedreport(billingmonthid,clientid,clientname,billingyear,billingmonth,supplier,created) values ('26','297in','infob','2014','12','undefined',1389697724)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.