Submitting Text with "&" character

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

style
Forum Newbie
Posts: 8
Joined: Tue Jul 08, 2003 7:51 pm

Submitting Text with "&" character

Post by style »

i find that it's hard to obtain "&" character in a submitted text
i cant obtain this by normally using $_POST nor $_GET
how can i slove it?
thx.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

What do you see if you:

Code: Select all

<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';

?>
Last edited by McGruff on Thu Aug 11, 2005 2:23 am, edited 1 time in total.
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

you'll need to use

Code: Select all

&
style
Forum Newbie
Posts: 8
Joined: Tue Jul 08, 2003 7:51 pm

Post by style »

sry, im just a newbie...
i dont understand how to use &...

and what do u expected in the result is this?
echo '<pre>';
print_r($_POST);
echo '</pre>';

e.g. i posted name = tammy&john

the result is
Array
(
[name] => tammy
)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

print_r is to help debug

In general, if you have a problem, go through the script a step at a time echoing out vars or print_r arrays to check what values you have.

That should help you track it down.
style
Forum Newbie
Posts: 8
Joined: Tue Jul 08, 2003 7:51 pm

Post by style »

oh Thx~i learned 1 more thing~
but how to slove this problem?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Well, what do you see when you print out the POST array?
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

style
Forum Newbie
Posts: 8
Joined: Tue Jul 08, 2003 7:51 pm

Post by style »

i posted
name = tammy&john

the result is:
Array
(
[name] => tammy
)
style
Forum Newbie
Posts: 8
Joined: Tue Jul 08, 2003 7:51 pm

Post by style »

sry i still dont know how rawurlencode() can help me to slove the problem
how can i obtain the useful string from $_POST?
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

Can we see more code? We need to see how your form is setup as well as how you handle the passed variable in your script.

I believe, however, the problem rests with your implementation. If you pass a link as http://www.none.com/test.php?name=tammy&john, the script only assigns Tammy to name, as you are seeing. This is because the Ampersand is a delimeter that says we have another variable coming your way. Therefore, john is becoming a variable.

What urlencode does is replace the ampersand (&) with a different representation of it so your URL will work properly. If these pieces of advice don't help, you NEED to post some code so we can manipulate and explain it for you.
style
Forum Newbie
Posts: 8
Joined: Tue Jul 08, 2003 7:51 pm

Post by style »

here is my sample

in html:
<form action="action.php" method="post">
<input name="name" type="text">
</form>

the value in name is tammy&john

in php:

<?
echo $_POST['name']
?>

the result is tammy only

is these enough?
Last edited by style on Wed Jul 09, 2003 9:44 am, edited 1 time in total.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

That's odd. If you've got an & in a POST var it should echo out as an &. GET is a different story.

The input tag isn't closed in the posted code, you've got a brace closing the POST key rather than a square bracket, and no semi-colon to terminate the line but I assume your actual form script is working if you're getting at least half a POST var.
Last edited by McGruff on Wed Jul 09, 2003 8:45 am, edited 2 times in total.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

style: it appears you never looked at html before.

if you did you'd know that & is a special character there and a flag that you're dealing with a new variable. therefore php stops parsing at it you cannot put it in unless via &, and even then i'm not sure that php would grab it correctly.
put a disclaimer over the text box not to use &


also go grab a html prep book. i think you might need it.
try the oreilly book webmaster in a nutshell it's a great reference book. i have edition 2 and it's still rather relevant. the isbn of the latest version is 0596003579

it strikes me that if you had a good reference book to check this issue you could clear up on your own. you learn more when you do it on your own. so that should be a worthwhile buy for you
style
Forum Newbie
Posts: 8
Joined: Tue Jul 08, 2003 7:51 pm

Post by style »

thx to ur suggestion ^^

but i think puting a disclaimer over the text box not to use &
is not a good method to slove the problem ^^~
i used & statement also, but it seems the same...
Post Reply