Forum Moderators: coopster

Message Too Old, No Replies

Passing 2 variables in URL

         

powerwings

5:00 pm on Jul 18, 2006 (gmt 0)

10+ Year Member



I'm having a problem getting a URL to display 2 variables correctly and carry them across to another PHP page.

I have one hardcoded variable which is easy to pass, but the second variable is causing problems, I think my syntax is wrong. I am using Postgres, which has slight differences to MySQL.

The following line in my code shows that the second variable is indeed available:
echo $carryaddressid; // shows the value 16

Here are 2 variations of code I have tried in order to to create a hyperlink to another PHP page, taking with it the 2 variables:

echo '<a href="addressadd.php?postcodeid=123&$carryaddressid"/>Link</a>';

echo '<a href="addressadd.php?postcodeid=123&carryaddressid=<? php echo $carryaddressid;?>"/>Link</a>';

The first variation simply carries the variable name rather than the code as follows:
[192.x.x.x...]

The second variation similarly carries the whole php enclosed line of code, rather than an echoed value.
I thought the second method would be correct, but as the echo statement is already inside a php section, perhaps the additional php statement is causing problems.

I realise I could use a form, but I'd prefer to do it this way if possible. I feel I must be close to getting it to work. Thanks.

eelixduppy

5:02 pm on Jul 18, 2006 (gmt 0)



Change it to this:

echo "<a href='addressadd.php?postcodeid=123&carryaddressid=$carryaddressid' />Link</a>";

Good luck!

powerwings

5:15 pm on Jul 18, 2006 (gmt 0)

10+ Year Member



Thanks, your suggestion works perfectly!
Very much appreciated :)

RonPK

6:23 pm on Jul 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Powerwings, have a look at the output of this script:

<?php
$name = 'Jill';
echo 'hello $name';
echo '<br>';
echo "hello $name";
?>

It may help you to understand your issue. More on this in the manual [nl2.php.net].

Sekka

9:46 pm on Jul 18, 2006 (gmt 0)

10+ Year Member



Just for reference,

Change it to this:

echo "<a href='addressadd.php?postcodeid=123&carryaddressid=$carryaddressid' />Link</a>";

Good luck!

This is not W3C compliant. The ampersand (&) will cause you an error. Change it to,

echo "<a href='addressadd.php?postcodeid=123&amp;carryaddressid=$carryaddressid' />Link</a>";

Also, whenever I use a variable inside a string, I add brackets to be on the safe side,

echo "<a href='addressadd.php?postcodeid=123&amp;carryaddressid={$carryaddressid}' />Link</a>";

Very small things, but good to do for safer programming.

eelixduppy

2:39 am on Jul 19, 2006 (gmt 0)



Sekka, the syntax for the url query is correct. The ampersand is used to separate the get variables. To include an ampersand in the string, you must use its hex equivalent(%26).

Sekka

8:12 am on Jul 19, 2006 (gmt 0)

10+ Year Member



But when you echo it into the HTML page it will error!

Please see this example,

<snip>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>[/pre]

<body>

With normal &amp;<br /> 
<a href="http://www.mydomain.com/index.php?
vara=test&varb=test">http://www.mydomain.com/index.php?vara=test&amp;varb=test</a> <br /><br />

With HTML code &amp;amp;<br /> 
<a href="http://www.mydomain.com/index.php?
vara=test&amp;varb=test">http://www.mydomain.com/index.php?vara=test&amp;varb=test</a>

Validate the document and you'll see which produces an error<br /> 
<a href="http://validator.w3.org/check?uri=http%3A%2F%2Fprojects.example.co.uk%2Ftest%2Ftest.html">W3C Validator</a>

</body> 
</html>

Validate this.

The first link will produce an error because you used "&" and not "&amp;".

[edited by: jatar_k at 3:35 pm (utc) on July 19, 2006]
[edit reason]
[1][edit reason] sidescroll and urls [/edit]
[/edit][/1]

coopster

4:36 pm on Jul 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The PHP manual pages for urlencode [php.net] demonstrate the proper handling of building links with an informative discussion from the W3 as well.

wheelie34

10:54 am on Jul 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am having a similar problem, I am able to pass both variables without problems BUT the first variable can be 1 or 2 seperate words so if the link has 2 words ie, wid get&file=$file when it goes to the page in the address box of the browser it shows

.com/page.php?page=wid%20get&filename.php

is this safe for search engines? if not how do I get rid of the %20

@ the OP not trying to steal the thread but my question is similar and could help someone else :)

edit reason: formatting URL string

Sekka

5:41 pm on Jul 20, 2006 (gmt 0)

10+ Year Member



Should be fine. I can't see any problems doing that.