Page 1 of 2

Submitting Text with "&" character

Posted: Tue Jul 08, 2003 7:51 pm
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.

Posted: Tue Jul 08, 2003 8:50 pm
by McGruff
What do you see if you:

Code: Select all

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

?>

Posted: Tue Jul 08, 2003 10:50 pm
by bionicdonkey
you'll need to use

Code: Select all

&

Posted: Wed Jul 09, 2003 12:23 am
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
)

Posted: Wed Jul 09, 2003 12:44 am
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.

Posted: Wed Jul 09, 2003 12:50 am
by style
oh Thx~i learned 1 more thing~
but how to slove this problem?

Posted: Wed Jul 09, 2003 6:14 am
by McGruff
Well, what do you see when you print out the POST array?

Posted: Wed Jul 09, 2003 7:13 am
by Tubbietoeter

Posted: Wed Jul 09, 2003 7:28 am
by style
i posted
name = tammy&john

the result is:
Array
(
[name] => tammy
)

Posted: Wed Jul 09, 2003 7:31 am
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?

Posted: Wed Jul 09, 2003 7:39 am
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.

Posted: Wed Jul 09, 2003 7:59 am
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?

Posted: Wed Jul 09, 2003 8:39 am
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.

Posted: Wed Jul 09, 2003 8:41 am
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

Posted: Wed Jul 09, 2003 9:50 am
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...