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

Use button to change PHP variable

Was wondering how to do this?

It's basically for a database table, so the button can be used to sort the results as ascending/descending.

Googled a bit, but there wasn't much on it as I could see.
Thanks
Dec 9 '09 #1
4 3684
Markus
6,050 Expert 4TB
To change a server-side variable (in this case, PHP) from the client-side (browser), you generally pass a parameter in the URL of the page indicating the value. See the following:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /** To obtain values from the URL, use the super-global GET array.
  4. if (isset($_GET['order']) && strlen($_GET['order']) > 0) {
  5.     $order = $_GET['order']; // Read: SQL Injection
  6. } else {
  7.     $order = "asc"; // Default to ascending.
  8. }
  9.  
  10. printf("Selected order: %s", $order);
  11.  
If you're going to be using data that can be manipulated by the user in a database query, read up on SQL Injection.
Dec 9 '09 #2
Many thanks for the reply, will go ahead and test it out =)
Dec 9 '09 #3
kovik
1,044 Expert 1GB
Generally, empty() is the accepted way of performing both "isset()" and "strlen() > 0" at the same time.

Also, since there are only two valid states of the query variable ("desc" and "asc"), it is considered more proper to treat is as a boolean (i.e. isdescending). Then you'd send the integer value 1 or 0 corresponding to true or false, respectively. This allows you to forcefully typecast the $_GET data to an integer, thus avoiding any SQL injection dangers.


When you are using pagination (separating database data into pages) via server-side queries, then using a query variable for your database query is the best route. However, if you are retrieving all of the data items at once, then JavaScript may be a better choice to reduce page loads and database queries. Though, if you are unfamiliar with JavaScript and not interested in learning it yet, then stick with PHP.
Dec 11 '09 #4
Thanks for the detailed post, I'm gonna explore it and see how I can make this work as well :)

thanks again
Dec 11 '09 #5

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

Similar topics

14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
1
by: dspearing | last post by:
I'm new to asp, and was wondering how I could change the value of a server variable when the user clicks a button. The asp page displays a report using server side vbscript, and I have added an...
5
by: Wolfgang | last post by:
Dear all, I've a lot of buttons named button1..button100 and want to change some of there propertys. Maintaining the overview I'm looking for a possibility for something like: ...
11
by: tshad | last post by:
I have the following: sub submitQuestion_click(Sender as Object, e as EventArgs) Dim submitButton as Button = CType(sender,Button) This gives me an error that says: "...
4
by: Greg Scharlemann | last post by:
I'm trying to setup a dyamic dropdown list that displays a number of text fields based on the selected number in the dropdown. The problem I am running into is capturing the data already entered...
3
by: GauravGupta | last post by:
i want to know that is it posible to call button click event before page load event on post back.... please help me....
1
by: esteban40oz | last post by:
I have 9 buttons on my form. Button1, Button2, Button3, etc I want to create an array to access the value of each of them. Dim buttonArray() As Button = {Button1, Button2, Button3, Button4,...
0
by: sweatha | last post by:
Hello Friends I have created the project with 2 forms. In the first form, I have 2 buttons. The first button has the backcolor as "fusia" by default. If I click the button means then backcolor of...
4
by: Sean | last post by:
I have a situation whereby I need to modify the text string appearing on an ASP button with some text derived from a Javascript function. But I am unsure of the correct syntax to do so. ...
8
by: Harvey Schmidlapp | last post by:
I have a fairly complex form (generated by means of an ASP 3 page). The form is used to define a query against a database. After running a query, the user hits their browser's back button and goes...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.