how to remove slashes in the php code

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
Rmias
Forum Newbie
Posts: 24
Joined: Wed Nov 26, 2003 11:02 am

how to remove slashes in the php code

Post by Rmias »

I have a form that displays user details from the database. If one user is selected from the list using radio buttons and click on edit button the selected user is dispalyed in the edit php and its detail can be edited. What the problem I have is when the user displayed in the edit.php it addes \ at the end of each field .e.g. User Id: 1\
First Name : John\

I used stripslashes() function to remove the slash but it does not work

In the display.php
<tr>
<td>User ID : <? echo “$user_id” ?> </td>
</tr>

<tr>
<td>First Name: <? echo “$first_name” ?> </td>
</tr>

the above displays
User Id: 1
First Name : John

The problem occurs when I click submit and details of the user displayed in the edit.php
<tr>
<td>User ID </td>
<td><input type=”text” name “user_id” value =<? echo “$user_id” ?> <td>
</tr>

<tr>
<td>First Name: </td>
<td><input type=”text” name “first_name” value =<? echo “$first_name” ?> <td>
</tr>

this displayed

User Id: 1\
First Name : John\

Please can some one help me on this thanks.
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

Quote your values and use a closing tag!

<input type=”text” name “user_id” value ="<? echo “$user_id” ?>">

not:

<input type=”text” name “user_id” value =<? echo “$user_id” ?>
Rmias
Forum Newbie
Posts: 24
Joined: Wed Nov 26, 2003 11:02 am

Post by Rmias »

thanks redhair, I tried as you suggested and it works now. However for some strange reason if i do the following it does not work

<input type=”text” name “user_id” value ="<? echo “$user_id” ?>">

but I changed the line to

<input type=”text” name “user_id” value ="

<?
echo “$user_id”
?>
">
and it removes the slash.

Thank you for your help
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

:lol: a ; is still missing

<input type=”text” name “user_id” value ="<? echo “$user_id”; ?>">
Rmias
Forum Newbie
Posts: 24
Joined: Wed Nov 26, 2003 11:02 am

Post by Rmias »

thanks redhair, I tried as you suggested and it works now. However for some strange reason if i do the following it does not work

<input type=”text” name “user_id” value ="<? echo “$user_id” ?>">

but I changed the line to

<input type=”text” name “user_id” value ="

<?
echo “$user_id”
?>
">
and it removes the slash.

Thank you for your help
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Or try

Code: Select all

<input type=”text” name “user_id” value ="<?=$user_id?>">
You don't need the quotes around the variable within the PHP <? ?> tags. <?= is short for echo.
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

Code: Select all

<input type=”text” name “user_id” value ="<?=$user_id?>">
:oops: There is a = missing to
User avatar
Meteo
Forum Newbie
Posts: 24
Joined: Sun Jan 18, 2004 10:19 am
Contact:

Post by Meteo »

Code: Select all

&lt;?php
echo &lt;&lt;&lt; input
&lt;input type="text" name="user_id" value ="$user_id"&gt;
input;
?&gt;
could do that
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

Meteo wrote:

Code: Select all

<?php
echo <<< input
<input type="text" name="user_id" value ="$user_id">
input;
?>
could do that
no...

Parse error: parse error, expecting `','' or `';'' in /var/www/html/test.php on line 2
User avatar
Meteo
Forum Newbie
Posts: 24
Joined: Sun Jan 18, 2004 10:19 am
Contact:

Post by Meteo »

eh, nice and polite of you too.
it always works for me, i'd usually go about it like this though...

<?php
$form = <<< form
<input type="text" name="user_id" value ="$user_id">
form;
echo $form;
?>
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

Meteo wrote:eh, nice and polite of you too.
it always works for me, i'd usually go about it like this though...

<?php
$form = <<< form
<input type="text" name="user_id" value ="$user_id">
form;
echo $form;
?>
That would work if you change <<< form into ", add \ to all " and change form; into ";
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

echo <<<EOT
<tr>
<td>User ID </td>
<td><input type="text" name="user_id" value=$user_id"></td></tr>

<tr>
<td>First Name: </td>
<td><input type="text" name &#8220;first_name" value="$first_name"></td>
</tr>
EOT;
The identifier you use (ie. EOT, or in your case, "input", must NOT be used in the rest of the code. That is why you got the parse error. So use something random (EOT, EOM, 4774, etc).

http://www.php.net/manual/en/language.t ... ax.heredoc


I also noticed some of your HTML was messed up (lack of a equals sign, not closing a tag, using the wrong tag to close it (two <td> tags instead of <td></td>), etc.

Check this out:
http://www.w3schools.com/html/default.asp
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

redhair wrote:
Meteo wrote:eh, nice and polite of you too.
it always works for me, i'd usually go about it like this though...

<?php
$form = <<< form
<input type="text" name="user_id" value ="$user_id">
form;
echo $form;
?>
That would work if you change <<< form into ", add \ to all " and change form; into ";
actually, his would work (this time), just not the original one he posted.

it's called heredoc, don't say it won't work when you don't know what it is ;)
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

LiLpunkSkateR wrote: it's called heredoc, don't say it won't work when you don't know what it is ;)
Well, I said it didnt work because I actually tested the code (just copy&pasted)...and got a parse error. So it don't work.

But ...I'll admit without shame, that I never seen heredoc before.
(If only the right code was supplied...)
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

ya, like i said, the original one didnt work because the word he used (input) could be found inbetween

ie.

$form = <<<form
<form action="whatever">
<input type="text">
<input type="submit">
</form>
form;

wouldnt work
Post Reply