473,383 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

problem with php inside javascript

118 100+
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
Apr 11 '09 #1
4 1390
Dormilich
8,658 Expert Mod 8TB
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)
Apr 11 '09 #2
swethak
118 100+
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.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Untitled Document</title>
  6. <script>
  7. function one(){
  8. xmlhttp=null;
  9. url='newpage.php';
  10. if (window.XMLHttpRequest)
  11.   {// code for all new browsers
  12.   xmlhttp=new XMLHttpRequest();
  13.   }
  14. else if (window.ActiveXObject)
  15.   {// code for IE5 and IE6
  16.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  17.   }
  18. if (xmlhttp!=null)
  19.   {
  20.   xmlhttp.onreadystatechange=state_Change;
  21.   xmlhttp.open("GET",url,true);
  22.   xmlhttp.send(null);
  23.   }
  24. else
  25.   {
  26.   alert("Your browser does not support XMLHTTP.");
  27.   }
  28.  
  29. }
  30.  
  31. function state_Change()
  32. {
  33. if (xmlhttp.readyState==4)
  34.   {// 4 = "loaded"
  35.   if (xmlhttp.status==200)
  36.     {// 200 = OK
  37.     // ...our code here...
  38.     }
  39.   else
  40.     {
  41.     alert("Problem retrieving XML data");
  42.     }
  43.   }
  44. }    
  45.  
  46. </script>
  47. </head>
  48. <body>
  49. <a href="" onclick="one()">Some Example</a>
  50. </body>
  51. </html>
  52.  

newpage.php

Expand|Select|Wrap|Line Numbers
  1. <?
  2. mysql_query("delete from tbl_ads_productinfo");
  3. ?>
  4.  

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.
Apr 11 '09 #3
Dormilich
8,658 Expert Mod 8TB
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.
Apr 11 '09 #4
gregerly
192 Expert 100+
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
Apr 12 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Randall Sell | last post by:
http://www.bytewise.com.au/test/iebug.html I can not see why this doesn't work under IE6. Perhaps someone can shed some light on this, and a possible hack to get it to work. My ultimate goal is...
3
by: Ryh | last post by:
I have the following scritpt. It hides div layer when mouse is out of the div layer. Inside DIV I have IFRAME box. Unfortuantely it does not work in Mozilla or IE 5.5. It hides div when cursor is...
10
by: Mark McLellan | last post by:
Dear all Following the oft-repeated advice here and ciwas I have made my site nearly 4.01 strict (working on it). There are some items on which I would appreciate your advice: 1. Hidden...
9
by: Andy Baxter | last post by:
hello, I'm writing a panoramic image viewer in html / javascript. The idea is I have a div with two identical panoramic photographs embedded in it. The clipping on the div is set to make it a...
7
by: John | last post by:
Hi Everyone, I'm having this extremely annoying problem with Internet Explorer 6, giving me an error message saying "unknown runtime error" whenever I try to alter the contents of a <divelement...
3
by: frerejacques | last post by:
Hi, I've encoutered a problem with javascript. I can read and edit basic java. however I'm working on with a free script that I found on the internet and encountered a problem. First I'll...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
2
by: buntyindia | last post by:
Hi, I have a session variable msgSize that is being set by a Java file in the session. aRequest.getSession().setAttribute("msgSize", msgSize); It's value is variable depends on record count of...
1
by: sheldonlg | last post by:
I have inherited code with a TDC control. In this file, there are two javascripts of interest. One of these is a function, filter(), which is inside <script language=javascript></script>. The...
1
by: raviviswanathan.81 | last post by:
Hello, So we have a webmaster who sets document.domain to some domain. After that, we try to create and inject text inside an iframe by getting the iframeID.contentDocument (or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.