how to insert data into database by using onchange event .

Member Avatar

diafol

This is probably Ajax. Look it up - come back with a specific question and examples of what you've tried.

thank you for yur reply .i am using input type number for getting values from user and i need to insert those values into my database table. i want to insert the data into table with onchange event ..i dont want to use submit button

this is how im displaying my items
<?php

            //loop through all table rows

            while ($row=mysql_fetch_array($result))
            {
                echo '<figure><a href=images/'. $row['food_photo']. ' alt="click to view full image" target="_blank"><img src=images/'. $row['food_photo']. ' width="80" height="70"></a>';
                echo "<figcaption>" . $row['food_name']."<br>";
                echo "<p text-align:center-right>" . $row['food_description']."</figcaption></p>";
                echo " " . $row['food_type']."";
                echo "PRICE" . $row['food_price']."";


                echo '<a href="cart-exec.php?id=' . $row['food_id'] . '">Add To Cart</a></figure>';

            }      



        mysql_free_result($result);
        mysql_close($link);
    ?>
    and now i want to add quantity text box as the user enter it should to inserted into database tale "quantity" i dont want to use submit button .the data should be inserted onchange event occured ..
    thank you in advance 
Member Avatar

diafol

We need to see the form and your SQL table

i have tables such as product(contains details of product),cart_details,quantity.the cart_details has cart id,product id,quantityid,total.
quantity (id,value)

when i clk add to cart i will insert the product details into cart_details table which i done .now i need to add to quantity table fr which i used text box to get the value.the think is i need to insert the user value in table quantity
without using the submit button

Member Avatar

diafol

quantity (id,value)

Sorry, makes no sense to me.

cart id,product id,quantityid,total

OK.

Do you want to use jQuery or plain javascript Ajax?

ok let me clear i want to get value from user in text box and insert it into a table .i dont want to use submit button. im tring to do it with onchange function.

Member Avatar

diafol

Yes, I understand that. What I don't know is whether you want to use jQuery or plain javascript to handle the ajax call.

Member Avatar

diafol

In that case . maybe here

http://blogs.digitss.com/javascript/simple-plain-ajax-without-any-javascript-library/

I can't see the quantity texbox in your code. Did you forget to include it? If so, you're probably looking to do something like this...

<input name="food_qty[34]" value="0" type="number" min="0" />

Where the array index in the name is the id of the product. Something like this:

echo "<input name='food_qty[{$row['food_id']}]' value='0' type='number' min='0' class='qty' />"

Now create an event handler based on changes to the number inputs (easier to base it on Class qty).

ok thanks.

<html>
<head>
<title>insert</title>
<!-- include jquery library file-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- The ajax/jquery stuff -->
<script type="text/javascript">

$(document).ready(function(){
//Get the input data using the post method when Push into mysql is clicked .. we pull it using the id fields of ID, Name and Email respectively...
$("#insert").click(function(){
//Get values of the input fields and store it into the variables.
var quantity_value=$("#Id").val();


//use the $.post() method to call insert.php file.. this is the ajax request
$.post('insert.php', {quantity_value: quantity_value});
return false;
});
});
</script>
</head>
<body>
<label>quantity: </label> <input id="Id" type="number" />

<a id="insert" title="Insert Data" href=#>Push into mysql</a>
 <!-- For displaying a message -->

</body>
</html>


<?php
//Configure and Connect to the Databse
 $con = mysql_connect("localhost","root","");
 if (!$con) {
 die('Could not connect: ' . mysql_error());
 }
 mysql_select_db("dinecart", $con);
 //Pull data from home.php front-end page
 $id=$_POST['quantity_value'];

 //Insert Data into mysql
$query=mysql_query("INSERT INTO quantities(quantity_value) VALUES('$id')");
if($query){

 header("location:home.php");
}
else{ echo "An error occurred!"; }
?>

with this code i have inserted data into my table with the help of a link "push into mysql " but what i need is as soon as i change the value it should be inserted into my table how to do that ?

Member Avatar

diafol

You said that you wanted to use plain javascript, but now you've gone and used jquery.

The insert php code should be in a separate file.

actually i was trying in jquery but i couldnt get so thought to go in simple that y told javascript.
insert php is separate file only ..
i am able to insert it .i used <a> tag to insert but i dont want to use any link buttons .

Member Avatar

diafol

i used <a> tag to insert but i dont want to use any link buttons .

That's confused me.

The usual way to make an ajax call from a html table with textboxes could be...

<html>
<head>
...head stuff...
</head>
<body>
..start table..
...in a while loop...
...<input data-id='{$row['id']}' value='0' type='number' min='0' class='qty' />...
..end table..

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
    $('.qty').change(function(){
        var id = $(this).data('id');
        var num = $(this).val();
        $.post("process.php",{id=id,num=num}).done(function(retValue){
            if(retValue){
                alert("Success");
            }else{
                alert("Fail");
            }
        });
    });

</script>
</body>
</html>

Your process.php file could have something like...

if($_POST && isset($_POST['id']) && isset($_POST['num']))
{
//db connection
//run query using mysqli/PDO with prepared statement 
//test for number rows affected -should return 1 on success, 0 on fail
    echo $num_rows_affected;
}