0

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

4
  • event.returnValue is deprecated. is a notice that is coming (probably) from an old version of jQuery or other included javascript files. You'll need to upgrade, or replace with event.preventDefault. I'm sure there are plenty of questions on SO about it. Commented Dec 10, 2013 at 3:02
  • The warning is because jQuery uses an obsolete method. Commented Dec 10, 2013 at 3:02
  • 1
    You're not sending any parameters to the PHP script when you call .load("insertbrg.php"). What do you expect it to add? Commented Dec 10, 2013 at 3:04
  • Why are you calling header("location:") in an AJAX server function? The whole point of using AJAX is that the page is not reloaded. Commented Dec 10, 2013 at 3:06

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

Comments

1

it's because nothing was sent in

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

it must accept parameters such as

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

Comments

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.