简体   繁体   中英

Can't insert new record to mysql db with PHP and JQuery Ajax

I have this database :

Table User : username, password

Table Product : productID, productName

In my admin.php, if I click button with id btn, I want to display insert.php in a div with id show

admin.php :

 <?php
      session_start();
      include("connection.php");
 ?>
 <html>
 <head>

     <script type="text/javascript" src="jquery.js"></script>
     <script type="text/javascript">
         $(document).ready(function(){
             $("#btnInsert").click(function(){
                 $("#show").load("insert.php");
             });

             $("#btnEdit").click(function(){
                 $("#show").load("edit.php");
             });

             $("#btnAdd").click(function(){
                 $("#show").load("insertbrg.php");
             });

         });
     </script>

 </head>

 <body>
     <div id = "menu">
         <button id = "btn">Insert</button><BR>
     </div>
     <div id = "show">
         &nbsp;
     </div>

 </body>
 </html>

And in insert.php :

 <?php
 session_start();
 include("connection.php");
 ?>

 Product ID : <input type = "text" name = "txtID"><BR>
 Product Name : <input type = "text" name = "txtName"><BR>
 <input type = "button" name = "btnAdd" id = "btnAdd" value = "Add Item to DB">

if I click btnAdd, the jquery in admin.php will load insertbrg.php to add Item to database

insertbrg.php :

 <?php
     if(isset($_REQUEST['btnAdd'])){
         $newID= $_REQUEST['txtID'];
         $newName= $_REQUEST['txtName'];

         $query = "Select * from Product";
         $result = mysql_query($query,$conn);

         $idExist = false;

         if($result){
             while ($row= mysql_fetch_array($result))
             {
                 if ($row["productID"] == $newID){
                     $idExist = true;
                 }
             }
         }

         if ($idExist){
             echo "ID Exist ";

         }else{
             $query2="insert into product values('$newID','$newName')";
             $result = mysql_query($query2,$conn);
             header("location:insert.php");
         }

     }

 ?>

I can't insert the item to my db if I clicked the btnAdd button

And also I get this warning :

event.returnValue is deprecated. Please use the standard event.preventDefault() instead.

everytime I click the btn button in admin.php

Anyone know what's wrong? I'm a newbie in JQuery AJAX. Please Help...

it's because nothing was sent in

$("#show").load("insertbrg.php");

it must accept parameters such as

$("#show").load("insertbrg.php?txtid="+txtid);

update your jQuery file and also make sure you pass the data you want to insert correctly so php can grab them and insert into the database.

you can send the data by get or post and its better to use the ajax function in jQuery.

you can find details about that here: http://api.jquery.com/jQuery.ajax/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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