Page 1 of 1

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

Posted: Tue Jun 06, 2006 11:12 pm
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...

Posted: Tue Jun 06, 2006 11:31 pm
by feyd

Posted: Tue Jun 06, 2006 11:45 pm
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)

Posted: Tue Jun 06, 2006 11:49 pm
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()

Posted: Tue Jun 06, 2006 11:58 pm
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().

Posted: Wed Jun 07, 2006 12:18 am
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.

Posted: Wed Jun 07, 2006 12:31 am
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?

Posted: Wed Jun 07, 2006 12:52 am
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

Posted: Wed Jun 07, 2006 6:38 am
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.

Posted: Wed Jun 07, 2006 9:22 am
by Bill H
You can actually do it all in one step:

Code: Select all

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