how to insert data into database by using onchange event .

Member Avatar

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

            //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>';


    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

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

quantity (id,value)

Sorry, makes no sense to me.

cart id,product id,quantityid,total


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

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

In that case . maybe here

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.

<!-- include jquery library file-->
<script type="text/javascript" src=""></script>
<!-- The ajax/jquery stuff -->
<script type="text/javascript">

//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...
//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;
<label>quantity: </label> <input id="Id" type="number" />

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


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

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

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

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

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

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

<script src="//"></script>
        var id = $(this).data('id');
        var num = $(this).val();


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;