News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Double $_GET[]

Started by Tomer, September 15, 2004, 06:47:47 AM

Previous topic - Next topic

Tomer

I hope someone can help me out, I've never successfully got this to work.

index.php?action=page;subpage=9

I know hot to get the index.php?action=page
Can someone tell me how exactly you add the subpage part?

Chris Cromer

Well $_GET['subpage'] should be set to 9. Well assuming that you have some code to parse that type of query string(because of the ;), or have php.ini set to use that the ;.

If not, you will either have to write code to parse that query string. Or use & instead of ;.
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

Parham

Quote from: Chris Cromer on September 15, 2004, 07:02:21 AM
Well $_GET['subpage'] should be set to 9. Well assuming that you have some code to parse that type of query string(because of the ;), or have php.ini set to use that the ;.

If not, you will either have to write code to parse that query string. Or use & instead of ;.

I was confused at first too Chris, but this is actually valid and it will be parsed properly (because of the new XHTML standard).  XHTML requires that even the deliminator in the query string be escaped so:

http://www.url.com/script.php?form1=foo&form2=bar

the above is not proper xhtml and the validator won't validate it.  instead you need:

http://www.url.com/script.php?form1=foo&form2=bar

that will validate correctly.  but as unknown explained to me before, what is mostly checked for is that ending semicolon to know when to go to the next variable.  So in essence, the above and the one below are the same:

http://www.url.com/script.php?form1=foo;form2=bar

and both will work.


Chris Cromer

What I meant was that php won't parse it. ;)

I am not talking about XHTML or the browser. And I don't beleive he was talking about that either.

PHP by default only seperates the query string by &. So just placing a ; in the url does not work unless you set php to parse ; instead of & or have code that seperates the query string and places the values into $_GET manually.

And yes in html/xhtml you have to put it as & in the html. Although... whoever mentioned anything about it being in the output(html)? All he said was that he couldn't get it to work, not that it didn't validate. ;)
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

Ardenn

Your right, PHP will not parse it.  I has this EXACT same trouble in my script.  You can see it under "Weird Variable Trouble"

There is a section of the PHP manual you need to look at: (Im going to copy it from the CHM because I cant find it on the website:

Quote

Chapter 51. PHP and HTML
PHP and HTML interact a lot: PHP can generate HTML, and HTML can pass information to PHP. Before reading these faqs, it's important you learn how to retrieve variables from outside of PHP. The manual page on this topic includes many examples as well. Pay close attention to what register_globals means to you too.

1. What encoding/decoding do I need when I pass a value through a form/URL?
2. I'm trying to use an <input type="image"> tag, but the $foo.x and $foo.y variables aren't available. $_GET['foo.x'] isn't existing either. Where are they?
3. How do I create arrays in a HTML <form>?
4. How do I get all the results from a select multiple HTML tag?
5. How can I pass a variable from Javascript to PHP?
1. What encoding/decoding do I need when I pass a value through a form/URL?

There are several stages for which encoding is important. Assuming that you have a string $data, which contains the string you want to pass on in a non-encoded way, these are the relevant stages:


HTML interpretation. In order to specify a random string, you must include it in double quotes, and htmlspecialchars() the whole value.

URL: A URL consists of several parts. If you want your data to be interpreted as one item, you must encode it with urlencode().


Example 51-1. A hidden HTML form element

<?php
    
echo "<input type='hidden' value='" htmlspecialchars($data) . "' />\n";
?>  



Note: It is wrong to urlencode() $data, because it's the browsers responsibility to urlencode() the data. All popular browsers do that correctly. Note that this will happen regardless of the method (i.e., GET or POST). You'll only notice this in case of GET request though, because POST requests are usually hidden.

Example 51-2. Data to be edited by the user

<?php
    
echo "<textarea name='mydata'>\n";
    echo 
htmlspecialchars($data)."\n";
    echo 
"</textarea>";
?>  
 


Note: The data is shown in the browser as intended, because the browser will interpret the HTML escaped symbols.

Upon submitting, either via GET or POST, the data will be urlencoded by the browser for transferring, and directly urldecoded by PHP. So in the end, you don't need to do any urlencoding/urldecoding yourself, everything is handled automagically.

Example 51-3. In a URL

<?php
    
echo "<a href='" htmlspecialchars("/nextpage.php?stage=23&data=" .
        
urlencode($data)) . "'>\n";
?>  
 


Note: In fact you are faking a HTML GET request, therefore it's necessary to manually urlencode() the data.

Note: You need to htmlspecialchars() the whole URL, because the URL occurs as value of an HTML-attribute. In this case, the browser will first un-htmlspecialchars() the value, and then pass the URL on. PHP will understand the URL correctly, because you urlencoded() the data.

You'll notice that the & in the URL is replaced by &amp;. Although most browsers will recover if you forget this, this isn't always possible. So even if your URL is not dynamic, you need to htmlspecialchars() the URL.


2. I'm trying to use an <input type="image"> tag, but the $foo.x and $foo.y variables aren't available. $_GET['foo.x'] isn't existing either. Where are they?

When submitting a form, it is possible to use an image instead of the standard submit button with a tag like: <input type="image" src="image.gif" name="foo" />

When the user clicks somewhere on the image, the accompanying form will be transmitted to the server with two additional variables: foo.x and foo.y.

Because foo.x and foo.y would make invalid variable names in PHP, they are automagically converted to foo_x and foo_y. That is, the periods are replaced with underscores. So, you'd access these variables like any other described within the section on retrieving variables from outside of PHP. For example, $_GET['foo_x'].

3. How do I create arrays in a HTML <form>?

To get your <form> result sent as an array to your PHP script you name the <input>, <select> or <textarea> elements like this: <input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />

Notice the square brackets after the variable name, that's what makes it an array. You can group the elements into different arrays by assigning the same name to different elements: <input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyOtherArray[]" />
<input name="MyOtherArray[]" />

This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script. It's also possible to assign specific keys to your arrays: <input name="AnotherArray[]" />
<input name="AnotherArray[]" />
<input name="AnotherArray[email]" />
<input name="AnotherArray[phone]" />

The AnotherArray array will now contain the keys 0, 1, email and phone.


Note: Specifying an arrays key is optional in HTML. If you do not specify the keys, the array gets filled in the order the elements appear in the form. Our first example will contain keys 0, 1, 2 and 3.


See also Array Functions and Variables from outside PHP.

4. How do I get all the results from a select multiple HTML tag?

The select multiple tag in an HTML construct allows users to select multiple items from a list. These items are then passed to the action handler for the form. The problem is that they are all passed with the same widget name. I.e. <select name="var" multiple="yes">

Each selected option will arrive at the action handler as: var=option1
var=option2
var=option3

Each option will overwrite the contents of the previous $var variable. The solution is to use PHP's "array from form element" feature. The following should be used: <select name="var[]" multiple="yes">

This tells PHP to treat $var as an array and each assignment of a value to var[] adds an item to the array. The first item becomes $var[0], the next $var[1], etc. The count() function can be used to determine how many options were selected, and the sort() function can be used to sort the option array if necessary.

Note that if you are using JavaScript the [] on the element name might cause you problems when you try to refer to the element by name. Use it's numerical form element ID instead, or enclose the variable name in single quotes and use that as the index to the elements array, for example: variable = documents.forms[0].elements['var[]'];



5. How can I pass a variable from Javascript to PHP?

Since Javascript is (usually) a client-side technology, and PHP is (usually) a server-side technology, and since HTTP is a "stateless" protocol, the two languages cannot directly share variables.

It is, however, possible to pass variables between the two. One way of accomplishing this is to generate Javascript code with PHP, and have the browser refresh itself, passing specific variables back to the PHP script. The example below shows precisely how to do this -- it allows PHP code to capture screen height and width, something that is normally only possible on the client side.


<?php
if (isset($_GET['width']) AND isset($_GET['height'])) {
  
// output the geometry variables
  
echo "Screen width is: "$_GET['width'] ."<br />\n";
  echo 
"Screen height is: "$_GET['height'] ."<br />\n";
} else {
  
// pass the geometry variables
  // (preserve the original query string
  //   -- post variables will need to handled differently)

  
echo "<script language='javascript'>\n";
  echo 
"  location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
            
"&width=\" + screen.width + \"&height=\" + screen.height;\n";
  echo 
"</script>\n";
  exit();
}
?>  



The part you need to pay attention to is the HTMLspecialchars() and the URLencode(); functions.

Here is an example:


<a href="'.htmlspecialchars("http://www.mysite.com/test.php?sort=1&getvariable1=".urlencode($getvariable1value)."&getvariable2=".urlencode($getvariable2value)).'">


This type of statement will allow you to send a multiple $_GET[],  I hope this helps, as I am still a PHP newb, but I think it should.
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

Chris Cromer

Yet again, that isn't what he is talking about.

You are missunderstanding the reason why PHP won't parse it. He is using a ;, but PHP by default only parses a &.

Your post and your code will not fix his problem, because your talking about something else entirely.

To have variables in the url ; seperated, you must either alter php.ini or have special code in the source file to parse it. The only reason SMF uses ; is because it has special code in it to use a ; instead of a & in the url.
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

Parham


Ardenn

#8
Chris,

The tone of your post is somewhat blunt, Im not taking it personally, but others would.  Im just trying to help and to learn. 

Now, for the technical aspect of this post.  I believe the question was :

Quote from: Tomer Dean on September 15, 2004, 06:47:47 AM
I hope someone can help me out, I've never successfully got this to work.

index.php?action=page;subpage=9

I know how to get the index.php?action=page
Can someone tell me how exactly you add the subpage part?

Basically the way I interpret this is.... "HOW do I get two variables in my $_GET array?"  If this is the same way that you interpret things, then I think I understand the problem.

Now lets say that this fellow has a simple script:

<?
//FILENAME IS TEST.PHP

echo '<a href="test.php?xyz=1">CLICK THIS LINK </a>';

IF (
$_GET['xyz']==1)
{
  echo '<a href="test.php?xyz=2;abc=2">NOW CLICK THIS LINK </a>';
}
ELSEIF (
$_GET['xyz']==2)
{
print_r ($_GET);
}
?>


When you look at the print_r (); your going to see something like:


Array ( [XYZ] => 2;abc=2 )

Now if you do it the way I suggested  with something like :


echo '<a href="'.htmlspecialchars("test.php?xyz=2&abc=".urlencode(2)).'">';


and print_r(); again, your going to see something like:

Array ( [xyz] => 2,  [abc] => 2 )   

And then you can call either $_GET['xyz'] or $_GET['abc'].

Now you may be right that I am misunderstanding what is being ask.  And by no means do I consider myself an expert.  I may be mistaken, but I thought this forum is here to offer HELP and to LEARN from.  Let me know if I should just shut up..... or..... explain to me why the coding is irrelevant in baby language so that I can learn something from my mistakes.
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

Parham

Quote from: Chris Cromer on September 15, 2004, 07:14:13 PM
Yet again, that isn't what he is talking about.

You are missunderstanding the reason why PHP won't parse it. He is using a ;, but PHP by default only parses a &.

Your post and your code will not fix his problem, because your talking about something else entirely.

To have variables in the url ; seperated, you must either alter php.ini or have special code in the source file to parse it. The only reason SMF uses ; is because it has special code in it to use a ; instead of a & in the url.

d'oh... you learn something new everyday... you're right, php won't parse the damn thing (which is why the chatroom on my site hasn't worked for the last little bit).

Parham

just on a sidenote, i realized my mistake... the ampersands only need to be changed in the xhtml, i was also using them in my redirects (d'oh).  so i was using the header() function and the ampersands were very stupidly escaped...

Chris Cromer

Didn't mean to sound blunt. So very sorry about that.
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

Advertisement: