0

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?

Korki Korkig
  • 2,736
  • 9
  • 34
  • 51

4 Answers4

1

Try this:

echo stripslashes($_POST['txtNaam']);
bayuah
  • 251
  • 1
  • 5
  • 10
0

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
  • I've tried this with `ini_set` but didn't work. It is also deprecated since 5.3 and maybe this was the reason of fail. thanks for the info! – Korki Korkig Aug 13 '13 at 14:46
0

Have you tried

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

or

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

You can also use base64_encode() & base64_decode()

Jigar
  • 3,256
  • 1
  • 30
  • 51