Created
July 19, 2017 17:06
-
-
Save arn-ob/2009aa6a7f8cfa18e4615755c4d655b6 to your computer and use it in GitHub Desktop.
Multiple Data input into DB by ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $servername = "localhost"; | |
| $username = "root"; | |
| $password = ""; | |
| $dbname = "try"; | |
| $name = $_POST["name"]; | |
| // Create connection | |
| $conn = new mysqli($servername, $username, $password, $dbname); | |
| // Check connection | |
| if ($conn->connect_error) { | |
| die("Connection failed: " . $conn->connect_error); | |
| } | |
| $sql = "INSERT INTO one (name) VALUES ('$name')"; | |
| if ($conn->query($sql) === TRUE) { | |
| echo "New record created successfully"; | |
| } else { | |
| echo "Error: " . $sql . "<br>" . $conn->error; | |
| } | |
| $conn->close(); | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <script src="http://code.jquery.com/jquery-1.9.1.js"></script> | |
| <script> | |
| //create array that will hold all ordered products | |
| var shoppingCart = []; | |
| //this function manipulates DOM and displays content of our shopping cart | |
| function displayShoppingCart(){ | |
| var orderedProductsTblBody=document.getElementById("orderedProductsTblBody"); | |
| //ensure we delete all previously added rows from ordered products table | |
| while(orderedProductsTblBody.rows.length>0) { | |
| orderedProductsTblBody.deleteRow(0); | |
| } | |
| //variable to hold total price of shopping cart | |
| var cart_total_price=0; | |
| //iterate over array of objects | |
| for(var product in shoppingCart){ | |
| //add new row | |
| var row=orderedProductsTblBody.insertRow(); | |
| //create three cells for product properties | |
| var cellName = row.insertCell(0); | |
| var cellDescription = row.insertCell(1); | |
| var cellPrice = row.insertCell(2); | |
| cellPrice.align="right"; | |
| //fill cells with values from current product object of our array | |
| cellName.innerHTML = shoppingCart[product].Name; | |
| cellDescription.innerHTML = shoppingCart[product].Description; | |
| cellPrice.innerHTML = shoppingCart[product].Price; | |
| cart_total_price+=shoppingCart[product].Price; | |
| } | |
| //fill total cost of our shopping cart | |
| document.getElementById("cart_total").innerHTML=cart_total_price; | |
| } | |
| function AddtoCart(description,price){ | |
| //Below we create JavaScript Object that will hold three properties you have mentioned: Name,Description and Price | |
| var singleProduct = {}; | |
| //Fill the product object with data | |
| singleProduct.Name=document.getElementById("field1").value; | |
| singleProduct.Description=description; | |
| singleProduct.Price=price; | |
| //Add newly created product to our shopping cart | |
| shoppingCart.push(singleProduct); | |
| //call display function to show on screen | |
| displayShoppingCart(); | |
| } | |
| //Add some products to our shopping cart via code or you can create a button with onclick event | |
| //AddtoCart("Table","Big red table",50); | |
| //AddtoCart("Door","Big yellow door",150); | |
| //AddtoCart("Car","Ferrari S23",150000); | |
| function recoed(){ | |
| for(var product in shoppingCart){ | |
| var userInput = shoppingCart[product].Name.toString(); | |
| var text ='{"name":"'+userInput+'"}'; | |
| var obj = JSON.parse(text); | |
| $.ajax({ | |
| type: 'post', | |
| url: 'InData.php', | |
| //data: $('form').serialize(), | |
| data : obj, | |
| success: function () { | |
| alert('form was submitted'); | |
| } | |
| }); | |
| } | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <table cellpadding="4" cellspacing="4" border="1"> | |
| <tr> | |
| <td valign="top"> | |
| <table cellpadding="4" cellspacing="4" border="0"> | |
| <thead> | |
| <tr> | |
| <td colspan="2"> | |
| Products for sale | |
| </td> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <tr> | |
| <td> | |
| Table | |
| </td> | |
| <td> | |
| <input type="text" id="field1" ><br> | |
| <input type="button" value="Add to cart" onclick="AddtoCart('Big red table',50)"/> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td> | |
| Door | |
| </td> | |
| <td> | |
| <input type="button" value="Add to cart" onclick="AddtoCart('Door','Yellow Door',150)"/> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td> | |
| Car | |
| </td> | |
| <td> | |
| <input type="button" value="Add to cart" onclick="AddtoCart('Ferrari','Ferrari S234',150000)"/> | |
| </td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| </td> | |
| <td valign="top"> | |
| <table cellpadding="4" cellspacing="4" border="1" id="orderedProductsTbl"> | |
| <thead> | |
| <tr> | |
| <td> | |
| Name | |
| </td> | |
| <td> | |
| Description | |
| </td> | |
| <td> | |
| Price | |
| </td> | |
| </tr> | |
| </thead> | |
| <tbody id="orderedProductsTblBody"> | |
| </tbody> | |
| <tfoot> | |
| <tr> | |
| <td colspan="3" align="right" id="cart_total"> | |
| </td> | |
| </tr> | |
| </tfoot> | |
| <input name="submit" type="submit" value="Submit" onclick="recoed()"/> | |
| </table> | |
| </td> | |
| </tr> | |
| </table> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment