Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

Ajax & PHP delete

Javascript coding ..

Moderators: macek, egami, gesf

Ajax & PHP delete

Postby MikeSpider » Tue Jul 24, 2012 7:50 am

Hi guys,
I have a form with a button and I'm trying to connect with the db via ajax.

form:
Code: Select all
 <form  method="get" >
                    <input type="button" name="submit" onClick="deleteSales()" value="Delete all" class="button">

     </form>



the Ajax:

Code: Select all
<script language="Javascript" type="text/javascript">
        <!--
     var xmlHttp = false;
     if(window.ActiveXObject){
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
     }else if(window.XMLHttpRequest){
         xmlHttp = new XMLHttpRequest();
     }

function deleteSales(){

        var url = "../application/delete-sales.php";
        xmlHttp.open("GET",url,true);
        xmlHttp.onreadystatechange = updateDeleted;
    }

      function updateDeleted(){
        if(xmlHttp.readyState == 4){
            document.getElementById("messageD").innerHTML = xmlHttp.responseText;
        }
    }

//-->
</script>



the PHP:
Code: Select all

<?php require_once("../inc.php");

echo " script arrived here!";

$con = mysql_connect('localhost','root','password') or die ("Could not connect!");
mysql_select_db('quickbeamdb');
//check Session user
 if(isset($_SESSION['admin_user'])){
            $profit = "profit";
            $totals = "totals";
            }else if(isset($_SESSION['regis_user'])){
                $profit = $_SESSION['regis_user']."_profit";
                $totals = $_SESSION['regis_user']."_totals";
            }

           
           
           $sql = "TRUNCATE TABLE $profit" ;
           $result = mysql_query($sql);

                     
            $sql = "UPDATE $totals SET totalexpense ='0',
                                        totalsales ='0',
                                        totalprofit = '0',
                                        date = NOW() WHERE id='1'" ;

             $result = mysql_query($sql);

               
              if(isset($_SESSION['profit_total'])){
                  unset($_SESSION['profit_total']);
              }

              if(isset($_SESSION['expenses_total'])){
                  unset($_SESSION['expenses_total']);
              }
              if(isset($_SESSION['sales_total'])){
                  unset($_SESSION['sales_total']);
              }

       
          echo "All items have been deleted from table!";         
       
                       
mysql_close($con);
?>


Nothing happens. I tried to display a text when the ajax function(deleteSales) is called, and the text displays, so the script goes to ajax but never to php.

Any help will be much appreciated,
Mike
MikeSpider
New php-forum User
New php-forum User
 
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm

Re: Ajax & PHP delete

Postby Nullsig » Tue Jul 24, 2012 8:23 am

Problem #1: You have a submit button on a form that has no action. So when you click the button it calls the JS function but then it refreshes the page immediately after.

Problem #2: You should have all of your JS in one function. Instead of breaking it up however you were trying to do it.

Problem #3: If you ignore step 2, one reason you won't get the update to execute is because "deleteSales" is assigning xmlHttp.onreadystatechange the value of an uninitialized variable instead of calling the intended function.


Fix those and I will help further.
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
 
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Ajax & PHP delete

Postby MikeSpider » Tue Jul 24, 2012 9:11 am

hehehe, you were right though, i've done other form connecting with db via ajax with no action and the ajax script was separated with 2 functions, the diference being that it was passing some parameters in the url.

Thanks
Mike
MikeSpider
New php-forum User
New php-forum User
 
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm

Re: Ajax & PHP delete

Postby MikeSpider » Tue Jul 24, 2012 9:26 am

It's working now,
sorry I forgot to show my changes:

the form:
Code: Select all
<form  method="get" action="<?php echo $_SERVER['PHP_SELF'];?>" >
     <input type="button" name="submit" onClick="deleteSales()" value="Delete all" class="button">

     </form>

Ajax:
Code: Select all
<script type="text/javascript">
<!--

function deleteSales(){

        if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("messageD").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","../application/delete-sales.php?",true);
xmlhttp.send();
}
//-->
</script>


I have one question:
regarding Ajax, which is the correct procedure:

1 - create only once xmlhttp object, and use it in as many ajax functions you like.
2 - create an xmlhttp object for every time you have an ajax function.

Thanks,
Mike
MikeSpider
New php-forum User
New php-forum User
 
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm


Return to JavaScript

Who is online

Users browsing this forum: No registered users and 0 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.