how do i echo strings with quotes on an input form?

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

Post Reply
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

how do i echo strings with quotes on an input form?

Post by pedrotuga »

I am tring to use this code

Code: Select all

$query=$_GET["query"];
<input maxlength=2048 size=70 name="query" value="<?php echo "$query"; ?>" title="search">
but i came to a problem. If $query has quotes only a escape slash shows on the input field...

i tried stripslashes() but then it mixed up the html around...
i cant thing of anything else...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

if the data contains both single (') and double (") quotes in it you can use backtick (`) to quote your values. now did anyone know this 8)
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post by pedrotuga »

i didnt know but i dont think that should do it here as i am using it inside an html tag... the goal is exaclty not to echo quotes or other sepcial charaters...

i will try with htmlentities()
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

well you should have said so, you topic title reads: how do i echo strings with quotes on an input form? see...
if the goal is to echo without any quotes try str_replace().
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post by pedrotuga »

well.. i dont want to start a fight here or anything...
but i think the thrad opening mesage is very clear...i even post the code.

echoing a quote just the way it is would break the html, escaping the quote would put extra backslashes before the special characters... so i didnt know what to do. So basically i couldn't echo a quote itself... of course it had to be a html entity... i would think about that at once i had more coding experience.
feyd poited me the right solution, though i had to use also stripslahes

Code: Select all

<?php
$query=htmlentities($query);
$query=stripslashes($query);
?>
thank yo both for the fast help.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

pedrotuga wrote:well.. i dont want to start a fight here or anything...
but i think the thrad opening mesage is very clear...i even post the code.
well, no one tried to... as for opening message, there was a bit difference in requirement written and needed.
pedrotuga wrote:echoing a quote just the way it is would break the html
i posted the previous answer for this matter only...
pedrotuga wrote:escaping the quote would put extra backslashes before the special characters... so i didnt know what to do.
for that feyd pointed you at a right place.

well i would say that if you are posting the user input back for editing, the & &quote; etc. would not exactly get through well with the end user isn't it?
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post by pedrotuga »

I guess i could play arround with functions and their reverse versions to get either entities or actual characters as necessary.

this is for a search tool and it's working fine so far
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

Post by Li0rE »

if you need a textarea solution to this, I have a wysig (or whatever it's called) editor that automatically adds whatever quotes and takes them away to work in the editor.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

You can actually do it all in one step:

Code: Select all

value="<?php echo htmlentities(stripslashes($query)); ?>"
Post Reply