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?
Try this:
echo stripslashes($_POST['txtNaam']);
Have you tried
echo htmlspecialchars($_POST['txtNaam'], ENT_QUOTES);
or
echo htmlentities(stripslashes($_POST['txtNaam']), ENT_QUOTES)
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
You can also use base64_encode() & base64_decode()
Related
I am making a form in html
and there is a <textarea> tag in that form.
first.php is like:
<form method="post" action="next.php">
<textarea name="desc" raws="4">Here, Its Text Area</textarea>
<button type="submit" name="submit">NEXT BUTTON</button>
</form>
And next.php is like:
<?php
if(isset($_POST["submit"]))
{
$desc = $_POST["desc"];
echo $desc; //<------ ECHO LINE 1
}
else
{
header("location: first.php");
}
And when I am typing this message:
Hi,
Helo there,
I am LakshyaK2011.
And echoing in it next.php, in echo line 1 (See In next.php's code)
It is displaying like this:
Hi, Helo there, I am LakshyaK2011.
is there any fixes to it?
Thanks,
I have tried:
(I didn't know what to do so I tried nothing)
I have expected:
I will enter:
Hi,
Helo there,
I am LakshyaK2011.
And it will print same as that.
Not like:
Hi, Helo there, I am LakshyaK2011.
To display new line in HTML format, you need something such as <br>
So Change
echo $desc;
to
echo str_replace(chr(13), '<br>',$desc);
Alternatively, use nl2br()
https://www.php.net/manual/zh/function.nl2br.php
I would like to replace \n with a new line.
This is the result from database:
<!DOCTYPE html>\n<html>\n<body>\n<div>\n<label class=\"s20 c03\">Label</label>\n</div>\n</body>\n</html>
I want the string to be aesthetically pleasing, for example:
<!DOCTYPE html>
<html>
<body>
<div>
<label class="s20 c03">Label</label>
</div>
</body>
</html>
It sounds like the \n are burnt into the string. To replace them with actual newline characters, use this:
$replaced = preg_replace('/\\\\n/', '\n', $yourstring);
You should definitely find the source of the double escaping first.
To undo your very exact data issue, there is stripcslashes() however.
Replaces a literal \n with a newline.
And the \" into a plain double quote.
It also undoes \r and octal \000 or hex \x00 escapes, or (it's most likely that your database also contains these) the double backslash \\ back into a singular one.
#ahmadalbayati is on the right track I think.
Pretty Markup
If you just want the database-returned string to look pretty in the returned page's markup (i.e. via View Page Source in a browser), simply echo it where you want it in the markup...
<?php
$dbResponse = "<!DOCTYPE html>\n<html>\n<body>\n<div>\n<label class=\"s20 c03\">Label</label>\n</div>\n</body>\n</html>";
echo $dbResponse;
?>
...which yields the following markup:
<!DOCTYPE html>
<html>
<body>
<div>
<label class="s20 c03">Label</label>
</div>
</body>
</html>
Pretty Rendered Page (and Markup)
If you want to show the database-returned string when the returned page is rendered, you can use the str_replace and htmlspecialchars functions in conjunction as follows...
<?php
$dbResponse = "<!DOCTYPE html>\n<html>\n<body>\n<div>\n<label class=\"s20 c03\">Label</label>\n</div>\n</body>\n</html>";
echo str_replace("\n", "<br />\n", htmlspecialchars($dbResponse));
?>
..., which yields the following displayed page...
<!DOCTYPE html>
<html>
<body>
<div>
<label class="s20 c03">Label</label>
</div>
</body>
</html>
...and the following markup behind it:
<!DOCTYPE html><br />
<html><br />
<body><br />
<div><br />
<label class="s20 c03">Label</label><br />
</div><br />
</body><br />
</html>
I have a textarea that users can edit. After the edit I save the text in a PHP variable $bio. When I want to display it I do this:
<?php
$bio = nl2br($bio);
echo $bio;
?>
But if a user for example types an HTML command like "strong" in their text my site will actually output the text as bold. Which is nothing I want.
How can I print/echo the $bio on the screen just as text and not as HTML code?
Thanks in advance!
Replace echo $bio; with echo htmlspecialchars($bio);
http://php.net/htmlspecialchars
When you output text to the html / the browser and you want to make sure that the output does not break the html, you should always use htmlspecialchars().
In your case you do want to show the <br> tags, so you should do that before you add them:
$bio = nl2br(htmlspecialchars($bio));
You can also use strip_tags() to get rid of the html tags altogether, but you would still need to use htmlspecialchars() so that for example a < character will not break your html.
You can also use htmlentites()
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
<form method="POST" action="">
<p><textarea rows="8" name="bio" cols="40"></textarea></p>
<p><input type="submit" value="Submit"></p>
</form>
<p>Result:</p>
<?php echo isset($_POST['bio']) ? htmlentities($_POST['bio']) : null; ?>
</body>
</html>
So like:
I am beginner to PHP and here is my question.
I have a text box and a 'Print' button. If 'Print' button is clicked, it should display the list of names entered in the text box. I'd written the following code, but the problem is, if I click the 'Print' button, it shows the names in a single line, instead of showing names in line by line format. Can someone help me in this. Thanks.
<!DOCTYPE html>
<html>
<head>
<title>Form Page</title>
</head>
<body>
<form action="Files.php" method="get">
<textarea rows="15" cols="30" value="textbox" name="textbox"></textarea></br>
<input type="submit" value="Print" name="Print">
</form>
</body>
</html>
<?php
if(isset($_GET['Print'])){
$file_print = $_GET["textbox"];
echo $file_print;
}
?>
You need to use nl2br() in PHP
Replace echo $file_print; with echo nl2br($file_print);
For edit..
echo nl2br($file_print);
file_put_contents('names.txt',$file_print,FILE_APPEND); //<-- Remove FILE_APPEND if you want to overwrite the file
Use nl2br()
nl2br — Inserts HTML line breaks before all newlines in a string.
So in your case, you need to do this echo nl2br($file_print);
I'm trying to replace some non standard characters like ë,Ë,ç,Ç with numeric entities like Ë , ' etc but i ran into a bit of a problem.
When i try to replace them directly like this it works fine:
$string = "Ë";
$vname = str_replace("Ë","AAAA",$string);
echo $vname."<br>";
an i get AAAA as a result.
But when i try to replace the characters from a string that i get from a form with POST then it doesn't change the characters. Here is an example:
<?php
if(isset($_POST['submit'])) {
$string = $_POST['title'];
if ($string == "Ë")
echo "Yes";
else
echo "No";
$vname = str_replace("Ë","AAAA",$string);
echo $vname."<br>";
echo $string;
}
?>
<form method="post" name="Form">
Title: <input name="title" type="text" value="" size="20"/>
<input name="submit" type="submit" value="submit"/>
</form>
Any help would be great!!
Most likely your characterset is wrong. I would suggest sending the following header when outputing html:
<?php header("content-type: text/html; charset=utf-8"); ?>
Where the charset match the charset you are storing your file in.
Edit: Just some more information. The file you store is in one charset for example latin1, while your browser interprets your html page as another charset (utf-8 for example). When the browser then sends the Ë character, it will send the utf-8 code 0xc38b, while the same character is 0xcb. As you can see, these does not match.
Edit - You can also update the CHARSET via HTML5 or xHTML:
HTML5
<meta charset="UTF-8"/>
xHTML
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />