Korki Korkig
Korki Korkig

Reputation: 2814

Some characters of $_POST string variable are wrongly displayed

I have 2 php pages. After submitting the form on page1 it's posted data is being displayed on page2. This works fine but some of characters like ' and " automatically get a \ just before themselves and the spaces are also gone.

For example I give ' " on page1. This is displayed as \' \" on page2. As you see the characters got \ attached and the spaces are also gone.

My code:

Page1.php

<html>
<head>
<title>PAGE 1</title>
</head>
<body>

    <form enctype="multipart/form-data" action="page2.php" method="post">
       <input type="text" name="txtNaam" id="txtNaam" />
       <input type="submit" value="Submit">
    </form>

</body>
</html>

Page2.php

<?php
// TEST 1
echo $_POST['txtNaam'];               // <== \' \"

echo "<br/>";   

// TEST 2
echo rawurlencode($_POST['txtNaam']); // <== %5C%27%20%20%20%20%5C%22

echo "<br/>";   

// TEST 3
echo urlencode($_POST['txtNaam']);    // <== %5C%27++++%5C%22
?>

How can I get these special characters correctly displayed when they are posted?

Upvotes: 0

Views: 922

Answers (4)

bayuah
bayuah

Reputation: 261

Try this:

echo stripslashes($_POST['txtNaam']);

Upvotes: 1

Jigar
Jigar

Reputation: 3322

You can also use base64_encode() & base64_decode()

Upvotes: 0

user849001
user849001

Reputation:

Have you tried

echo htmlspecialchars($_POST['txtNaam'], ENT_QUOTES);

or

echo htmlentities(stripslashes($_POST['txtNaam']), ENT_QUOTES)

Upvotes: 0

anon
anon

Reputation:

If magic_quotes_gpc is turned on, all $_GET, $_POST and $_COOKIE variables (GPC) in PHP will already have special characters like ", ' and \ escaped.

To prevent this from happening, you can disable it.

Edit your php.ini like so:

magic_quotes_gpc = Off

Upvotes: 0

Related Questions