how do I append a variable to a url that already contains variables?

P

PseudoMega

I'm working with a PHP page I wrote which searches through records in a
MySQL database.

I have a <form method="post"> which currently passes all of search
variables into the session array. I'd like to change the form to GET
instead of POST so that all of the variables are in the URL.

My problem is that I have already been using pagination code which
creates links like <a href="?page=2">. With all of the other variables
in the session array, it works fine. But moving to the GET method, I
need to append that "?page" variable to the existing URL with the other
variables.

I think I may have skipped over this chapter in all of my studies
because I just have no idea how to pull it off :)

Can anyone point me in the right direction?
 
B

BootNic

PseudoMega said:
news: (e-mail address removed)
I'm working with a PHP page I wrote which searches through records in
a MySQL database.

I have a <form method="post"> which currently passes all of search
variables into the session array. I'd like to change the form to GET
instead of POST so that all of the variables are in the URL.

My problem is that I have already been using pagination code which
creates links like <a href="?page=2">. With all of the other
variables in the session array, it works fine. But moving to the GET
method, I need to append that "?page" variable to the existing URL
with the other variables.

I think I may have skipped over this chapter in all of my studies
because I just have no idea how to pull it off :)

Can anyone point me in the right direction?

<form action="<?= $PHP_SELF ?>">
<div>
<?
foreach ($_GET as $key => $value) {
print ('<input type="hidden" name="get_'.$key.'" value="'.$value.'">
');
}
?>
<input type="text" name="willy">
<input type="submit"></div>
</form>
 
N

Neredbojias

I'm working with a PHP page I wrote which searches through records in a
MySQL database.

I have a <form method="post"> which currently passes all of search
variables into the session array. I'd like to change the form to GET
instead of POST so that all of the variables are in the URL.

My problem is that I have already been using pagination code which
creates links like <a href="?page=2">. With all of the other variables
in the session array, it works fine. But moving to the GET method, I
need to append that "?page" variable to the existing URL with the other
variables.

I think I may have skipped over this chapter in all of my studies
because I just have no idea how to pull it off :)

Can anyone point me in the right direction?

Generally it's done so:

<a href="xxx?page=2&chapter=5>
 
J

Jukka K. Korpela

Scripsit Neredbojias:
Generally it's done so:

<a href="xxx?page=2&chapter=5>

But the correct syntax is
<a href="xxx?page=2&amp;chapter=5>

The _URL_ here is of course xxx?page=2&chapter=5.
 
D

dorayme

"Jukka K. Korpela said:
Scripsit Neredbojias:


But the correct syntax is
<a href="xxx?page=2&amp;chapter=5>

I expect it will be a little pedantic to add that the correct
syntax is:

<a href="xxx?page=2&amp;chapter=5">


<g>
 
T

Toby Inkster

BootNic said:
<?
foreach ($_GET as $key => $value) {
print ('<input type="hidden" name="get_'.$key.'" value="'.$value.'">
');
}
?>

or better...

<?php
foreach ($_GET as $key => $value)
{
printf('<input type="hidden" name="%s" value="%s">',
htmlentities($key) ,htmlentities($value));
}
?>

Just in case $key or $values contains lovely quote marks.
 
J

Jukka K. Korpela

Scripsit David Dorward:
You can usually use ";" instead of "&" though, which nicely removes
the need to convert to entities.

I can use a character of my choice, such as ";" or "X", if I am in control
of the server-side script and if I do not need to care about what browsers
do when constructing a form data set. This means, more or less, that
_usually_ I cannot choose the separator between fields but must use "&",
which shall be encoded in HTML as &amp; or using a character reference.
 
T

Toby Inkster

David said:
You can usually use ";" instead of "&" though, which nicely removes the
need to convert to entities.
http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2

You *can* yes, but it means that you need to:

1. Hex encode a literal semicolon as '%3B' when it appears
in a query string; and

2. Program your server-side script to accept both semicolons
*and* ampersands for input (as form submissions will
continue to use ampersands in the query sring), and of
course have a reliable way of detecting which delimiter
has been used for a given submission.
 
P

PseudoMega

Consider this example:

A google search for the word "pagination" with my preferences set to
100 results per page.

After hitting the search button, this is the URL:
http://www.google.com/search?num=100&hl=en&lr=&q=pagination&btnG=Search

The links at the bottom of the page for "previous", "next" and the
individual page numbers are as such:
http://www.google.com/search?q=pagination&num=100&hl=en&lr=&start=100&sa=N
http://www.google.com/search?q=pagination&num=100&hl=en&lr=&start=200&sa=N
http://www.google.com/search?q=pagination&num=100&hl=en&lr=&start=300&sa=N
and so on...

The 'q', 'num' and 'hl' variables remain, while the 'start' variable is
added to the mix as an offset for page numbering. I need to basically
emulate this, but the 'start' variable will be the 'page' variable in
my situation. This needs to fit with my dynamic pagination code which
creates links based on the number of results encountered in the
database.

BootNic and Toby: your suggestions seem along the right track, but I
need these to be text links just like Google's. Also, in addition to
the text links, my pagination code creates a drop down select menu with
an entry for each available page number.

Thanks for all the suggestions so far. I hope this makes it more clear
though.
 
D

David Dorward

1. Hex encode a literal semicolon as '%3B' when it appears
in a query string; and

Which happens a lot less frequently then having multiple pieces of data
there does.
2. Program your server-side script to accept both semicolons
*and* ampersands for input (as form submissions will
continue to use ampersands in the query sring), and of
course have a reliable way of detecting which delimiter
has been used for a given submission.

Most libraries, in my experience, for handling form data do this by default.
The only time I've not been able to do this is when working with a rather
nasty JSP system.
 
D

David Dorward

Jukka said:
I can use a character of my choice, such as ";" or "X", if I am in control
of the server-side script

Or if the script uses a form processing library that follows the W3C
recommendation.
and if I do not need to care about what browsers
do when constructing a form data set.

They'll continue to use "&" (and the script will accept that), you can
type ";" and the script will likely accept that without any extra work
being needed on your part.
 
N

Neredbojias

To further the education of mankind, "Jukka K. Korpela"
Scripsit Neredbojias:


But the correct syntax is
<a href="xxx?page=2&amp;chapter=5>

Yes, quite true. I probably should have written it so.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top