Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing an ampersand

Status
Not open for further replies.

bigbird3156

Programmer
Feb 20, 2001
183
0
0
AU
Hi... if I am passing a string variable from one page to another that contains an ampersand I get all sorts of problems...

I know there are special characters like & and & but if these values are saved in an SQL file then it still passes the ambersand and not the special charcter...

does anyone know if there is any way around this...

for example I have a designer who's name is 'Melly & Me' in my database, I want to pass this value to a page where I get to see the designers profile (and no I can't use the associated record number) ... the designer is insistant on having the ambersand in her name... is there any way around this??

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
Thanks for the hint...

I have been looking around a bit and cant for the life of me figure out how to use the urlencode function on a href like this...

Code:
 <a href="catalogue.php?page=des_cat&designer="<?php echo $row_view_catalogue['designer']; ?>">

can anyone give me an idea? (obviously it is the php bit that needs the encoding)

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
Hi

This :
Code:
<a href="catalogue.php?page=des_cat[red]&amp;[/red]designer=<?php echo [red]urlencode([/red]$row_view_catalogue['designer'][red])[/red]; ?>">
Will make the output like this :

[tt]<a href="catalogue.php?page=des_cat[red]&amp;[/red]designer=[red]Melly+%26+Me[/red]">[/tt]

If we understood your problem correctly...

Note, that I removed the extra double quote ( " ).
Also note, that you have to write the ampersands ( & ) used as GET parameter delimiter with character entity.

Feherke.
 
...and then there is the question of urldecode...

Again I am trying to get my head around it, but I just can't figure it out...

I assume that the function needs to affect the following code?? - am I getting warm or am I way off... can you give me any hints???

Code:
$colname_view_des_cat = "-1";
if (isset ($_GET ['designer'])) {
  $colname_view_des_cat = $_GET ['designer'];
}

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
Hi

I do not really understand your problem/question.

In [tt]$_GET['designer'][/tt] you will get the original value : 'Melly & Me'.

When you are doing [tt]echo[/tt] in PHP, the interpreter not knows where it will appear in the document, so if it needs URL encoding or HTML character entity encoding. ( Or it will not be HTML at all... ) So you have to encode it properly as needed.

But when PHP receives a request, it knows that the query string is URL encoded, so automatically decodes it.

Or I misunderstood you...

Feherke.
 
ah yes, sorry ... I had another look at what I was doing and it all makes sense now...

it was changing the ambersand in the href above that was causing me problems and made me think that I needed to use a urldecode... once I changed the &amp; back to & it all worked fine...

what you said about PHP interpreting the string was kind of what everything i read was saying, I was just a bit slow in picking that up...

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
Hi

Bigbird said:
once I changed the &amp; back to & it all worked fine...
Hmm... You mean this red ampersand ?
Bigbird at 26 May 09 6:18 said:
Code:
<a href="catalogue.php?page=des_cat[COLOR=red pink]&[/color]designer="<?php echo $row_view_catalogue['designer']; ?>">


Feherke.
 
yes sorry... thats the one

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
Hi

The only difference is :
HTML:
[gray]<!-- valid HTML -->[/gray]
<a href="catalogue.php?page=des_cat[red]&amp;[/red]designer=Melly+%26+Me">

[gray]<!-- invalid HTML -->[/gray]
<a href="catalogue.php?page=des_cat[red]&[/red]designer=Melly+%26+Me">
But that difference should not affect PHP at all.

I would say, the problem was/is somewhere else.

But without seeing more from that site, I can not imagine where, when and what are you actually doing with the data.

Feherke.
 
This particular link is in a page that gives a bio of the particular designer... the link accesses another record set in my database that contains a catalogue of all products... by selecting the link the user is selecting to see only products created by that particular designer...

all pages are php based so it works fine with the basic &

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
Hi

That must work fine with both valid ( &amp; ) and invalid ( & ) ampersands :
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
</head>
<body>
<pre>
Query String : <?php echo $_SERVER['QUERY_STRING']; ?>

GET Variables : <?php print_r($_GET); ?>
</pre>
<p>
<a href="test.php?page=des_cat&amp;designer=Melly+%26+Me">Valid HTML</a>
<a href="test.php?page=des_cat&designer=Melly+%26+Me">Invalid HTML</a>
</p>
</body>
</html>
Code:
Query String : page=des_cat&designer=Melly+%26+Me
GET Variables : Array
(
    [page] => des_cat
    [designer] => Melly & Me
)
Code:
Query String : page=des_cat&designer=Melly+%26+Me
GET Variables : Array
(
    [page] => des_cat
    [designer] => Melly & Me
)
As you can see, the browser sends both request in the same way. There is no difference for PHP.

I guess I am becoming boring, so I will not insist on this.


Feherke.
 
thanks feherke...

OK point taken I changed the link back to &amp; and it DID work...

I do not know why it was not working before I changed it back to the & as the only thing that I changed was this link and then BOOM it worked... now I change it back and it still works...

anyway thanks for your insistance in all of this, I understand the relivance of having it HTML valid.

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
I also am having a problem passing an ampersand.

People fill in a search form and sometimes use an ampersand. If they typed in the form field "j&" it gets passed to a url in this format:

j&amp;

This is passed to index.php by apache rewrite engine as the value "n1".

Then it is picked up by the index.php page with this get:

$n1 = $_GET['n1'];

I have tried many combinations of urlencode/urldecode and str_replace, used backslashes, tried %26, but I cannot get the ampersand to pass. It passes letters, numbers and spaces fine.

Any ideas?

Thanks
Mike
 
if you are using a form, then it would be better to use the POST method.

if you must use GET then make sure you set the form encoding attribute.
Code:
<form enctype="application/x-[URL unfurl="true"]www-form-urlencoded"[/URL] action="somepage.php" method="get" >
 
Hi

jpadie said:
if you must use GET then make sure you set the form encoding attribute.
I would say, that is pointless.
W3C said:
enctype = content-type [CI]
This attribute specifies the content type used to submit the form to the server (when the value of [tt]method[/tt] is "post"). The default value for this attribute is "application/x- The value "multipart/form-data" should be used in combination with the INPUT element, [tt]type="file"[/tt].

HTML 4.01 Specification | Forms | The [tt]FORM[/tt] element



Feherke.
 
Thanks jpadie, I just tried both post and form enctype, no joy there.

feherke, here are a few details

Code:
[URL unfurl="true"]http://www.castleyellow.com/byname/commercial-property-management//1/j&amp;-/0[/URL]

There is a debugging entry to post the $n1 value to the upper left corner of the page. It only shows J.

Code:
[URL unfurl="true"]http://www.castleyellow.com/byname/commercial-property-management//1/j%20&amp;-/0[/URL]

Likewise only shows J.


Code:
[URL unfurl="true"]http://www.castleyellow.com/byname/commercial-property-management//1/j%20a-/0[/URL]

This shows j a -- passing both letters and the space

httpd.conf rewrite rule

Code:
RewriteCond %{REQUEST_URI} ^/byname/(.*)/(.*)/(\d)/(.*)-(.*)/(\d+)$ [NC]
RewriteRule ^/ /index.php?byname=byname&catid=%1&s_name=%2&startswith=%3&n1=%4&c1=%5&start=%6 [NC,PT]


index.php:

Code:
$n1 = $_GET['n1'];

echo $n1;
-- this is the letters at the top left of the page


Thanks again
 
you are right, of course, feherke. get method should by default use url encoding as per berners-lee's rfc. but something is clearly mangling the encoding for the OP. perhaps a faulty doctype (unlikely) or perhaps an alternative enctype is set and the browser is not standards compliant.

failing that we will have to get more involved in the actual scripts and architecture involved in creating the test case
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top