473,385 Members | 1,901 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,385 software developers and data experts.

Execute MySQL only if JS if = true

5
Hey

I've got a mysql query that I'd like to process *only* if a javascript if is true. Is that possible?

Something like this:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. if (condition is true) { 
  3. <?php
  4. $query = "UPDATE table SET example = '3' WHERE id = '1'";
  5. $result = mysql_query($query);
  6. ?>
  7. alert('Example'); 
  8. window.location.reload() 
  9. </script>
Somehow, as soon as the web is loaded, the query is sent.

Thank you :)
Nov 29 '08 #1
6 3246
serdar
88
What you are trying to do above won't work because php code is processed on the server-side (before browser gets the page content) and javascript code on client-side (after the php script processed and page downloaded by the browser).

So you have to find a way to check if javascript is enabled within your php script, and here it is Servers side JavaScript detection in PHP4

EDIT: I thought you want to detect javascript at first, but now I see you need something different. Well you can do what you need through an XMLHttpRequest (AJAX). A simple AJAX tutorial can be found at: AJAX Tutorial

And here is more to show why the code above won't work:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $city='London';
  3. ?>
  4. <html>
  5.     <head></head>
  6.     <body>
  7.         <script language="javascript">
  8.             if (0) {
  9.                 <?php
  10.                     $city='Paris';
  11.                 ?>
  12.             }
  13.         </script>
  14. <?php
  15.     echo $city;
  16. ?>    
  17.     </body>
  18. </html>
  19.  
Browser always displays 'Paris' because php code is already processed and browser just gets the resulting page.

IMPORTANT NOTE: Using AJAX or any other javascript-based validations/tests does not guarantee that your php script cannot be run without passing the validation/test. You can only secure your test through server-side validations.
Nov 30 '08 #2
Atli
5,058 Expert 4TB
Yea, serdar is correct. Doing this the way you are trying is not possible.

The key thing to remember is that PHP is a server-side language, executed by the server to create the content that is to be sent to the client.
Javascript is a client-side language, which is sent as a part of the content that PHP creates and executed by the client's browser.

Neither of them can directly work with the other simply because they are not being executed by the same computer. When the Javascript is executed, the PHP code has already been executed and closed.

But, like serdar suggested, you can use AJAX to get around this.
An AJAX request is basically just your Javascript requesting another page from the server. If you want the client code to execute a SQL query on the server, you will have to create a PHP page on the server that executes the query, and call that page from you Javascript by using AJAX.
Nov 30 '08 #3
kout
5
Thank you both, I'll try it now and let you know how it goes :)
Nov 30 '08 #4
kout
5
Worked just fine, topic solved :)

(PS: no edit button?)
Nov 30 '08 #5
Markus
6,050 Expert 4TB
@kout
Edit button disappears after 5 minutes, I think. What do you want to edit?
Nov 30 '08 #6
kout
5
Nothing, was trying to avoid double posting I did above :P
Nov 30 '08 #7

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

Similar topics

1
by: Sheila King | last post by:
I have searched for an answer to this question on the comp.lang.python archives at Google Groups, and also looked through the (sparse) MySQLdb documentation, and cannot confirm for a FACT what I...
2
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works,...
0
by: Monica Ferrero | last post by:
Hi! I'm not sure if this is the most adequate mySQL list for this post. If not, please indicat me which one I should use... I'm using Tomcat 4.1.24 with Apache 2 and MySQL 4.0.13. I have the...
0
by: Forecast | last post by:
I am using the MySQL++, MySQL 4 in MS Visual C++ 6.0. When I try to compile the program, warning occurs (see below). Besides, the program have runtime error as follows: "The procedure entry point...
7
by: William Gill | last post by:
I have been trying to pass parameters as indicated in the api. when I use: sql= 'select * from %s where cusid = %s ' % name,recID) Cursor.execute(sql) it works fine, but when I try : sql=...
11
by: John Moore | last post by:
Hi, I must be missing something obvious. I cannot get this function to run the update query on Line 6, although the call to run the query evaluates true, and both update_cat_total functions...
8
by: johnlichtenstein | last post by:
I am using cx_Oracle and MySQLdb to pull a lot of data from some tables and I find that the cursor.execute method uses a lot of memory that never gets garbage collected. Using fetchmany instead of...
5
by: loudwinston | last post by:
Hello, I'm encountering a strange error with PDO. The server is FreeBSD 4.4, PHP 5.1.2, mySQL 4.0.20 with 4.1.18 client libs. I have the following code (usernames and passwords changed to...
3
by: FNA access | last post by:
Hello all, I am relitivley new to mySQL. I started by downloading from the mySQL site. I then downloaded mySQL administrator and mySQL Query browser. With my set up I got a user root with a...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.