problem with php inside javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swethak
    New Member
    • May 2008
    • 118

    problem with php inside javascript

    Hi,


    i am using one button.When i click on that button it goes to some javascript function using onclick event.Inside that function i used the some php mysql delete query.In that i am facing one problem. i,e When i open that page that mysql query automatically run without calling the function .

    My requirement is that delete query execute only when click on that button .How can i achieve my requirement.I am trying that from long.But no use.Anybody please help me.Its very urgent.


    Thanks & Regards
    K.Swetha
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    any server side code (PHP, SQL, ...) is run before the site is sent to the browser

    2 possibilities
    - make a form and submit the values
    - use AJAX (which is nearly the same, except that you don't reload the page or submit a form)

    Comment

    • swethak
      New Member
      • May 2008
      • 118

      #3
      Hi,


      Thank you for your Reply.As you mentioned like that i used Ajax.And write the ajax code.But it is not working.Please help me what's the problem in my code.

      Code:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>Untitled Document</title>
      <script>
      function one(){
      xmlhttp=null;
      url='newpage.php';
      if (window.XMLHttpRequest)
        {// code for all new browsers
        xmlhttp=new XMLHttpRequest();
        }
      else if (window.ActiveXObject)
        {// code for IE5 and IE6
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      if (xmlhttp!=null)
        {
        xmlhttp.onreadystatechange=state_Change;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
        }
      else
        {
        alert("Your browser does not support XMLHTTP.");
        }
      
      }
      
      function state_Change()
      {
      if (xmlhttp.readyState==4)
        {// 4 = "loaded"
        if (xmlhttp.status==200)
          {// 200 = OK
          // ...our code here...
          }
        else
          {
          alert("Problem retrieving XML data");
          }
        }
      }	
      
      </script>
      </head>
      <body>
      <a href="" onclick="one()">Some Example</a>
      </body>
      </html>

      newpage.php

      Code:
      <?
      mysql_query("delete from tbl_ads_productinfo");
      ?>

      When i used that above code in firefox i get the error as

      "Problem retrieving XML data"

      in IE i got the problem as when i click on that hyperlink the page goes working directory folder.Please tell the solution how can i execute my code.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        get the status code, this may help telling what's wrong.

        further, the SQL query seems invalid (you don't tell what to delete)

        if you dont have short tags enabled, your code won't be parsed.

        there is no return value for ajax.

        Comment

        • gregerly
          Recognized Expert New Member
          • Sep 2006
          • 192

          #5
          ok, you've got a whole slew of problems here. First, the reason your always getting the error alert is due to scope problems. In state_Change() you are refferencing a variable that does not exist to the function. You need to pass it in or make it somehow available to the function. Right now it's being created and is only accessible by the function one(). This is probably the basis of your problem. Is any data being deleted? If so, your ajax is working but your just not seeing the return because of the reason outlined above.

          Also, you may want to make sure the function one() returns false, which will cancel out any thing the href would have done. This will make your ajax fire, but nothing else should happen.

          Lastly, your crazy to put any delete code on a page without some proper validation first. One example would be as Dormilich mentioned, make it a form that submits a set of values. You can then check to ensure the values have been submitted before running the delete. That way if someone tries to access your page directly, it won't run the delete code.

          Hope that helps!

          Greg

          Comment

          Working...