I'm new to PHP, I'm trying to write to a file a text written from user. If i put a normal text like "TEST123" it is ok but when i put "TEST 123" it doesnt pass anything. A little help will be appreciated. Thnx
<?php
$filename = 'textfile.txt';
$content = $_POST['thedata'] ;
$handle = fopen("textfile.txt", "w");
fwrite($handle,'My text is:');
fwrite($handle,$content);
fclose($handle);
?>
HTML is
<form method="post" action="changetxt.php">
<font size"3"><font color="white"><b>Name </b> </font>
<div id="dat">
<textarea name="thedata" id="thedata" cols="20" rows="1"></textarea>
</div>
<div id="sub" style="position:absolute; left: 25">
<input type="submit" value=" Change Name ">
</div>
</form>
I think your question should have been: How do I write quotes to a file?
And the answer would be to put them in the string using escaping (\") in a double quotes string or directly in a single quoted string.
Try:
$content = '"' . $_POST['thedata'] . '"';
or
$content = "\"" . $_POST['thedata'] . "\"";
(I would have put this in the comments but I need more reputation...)
Related
i have a text box where i can paste the Base64 encoded strings Like that
ZWxpZWVsYW1AeWFob28uY29t
ZWxpZXJvbWVyQGhvdG1haWwuY29t
ZWxsaXNjdXN0b21AZ21haWwuY29t
ZWxsYWxvMTAuZ3ZAZ21haWwuY29t
ZWxsZW5ib2x0OTFAb3V0bG9vay5jb20
and when i click the button Decode it should echo like the same way as i paste them in text box
Like that
elieelam#yahoo.come
lieromer#hotmail.com
elliscustom#gmail.com
elliscustom#gmail.com
elliscustom#gmail.com
But i can't get that
i'm getting the result like that
elieelam#yahoo.comelieromer#hotmail.comelliscustom#gmail.comellalo10.gv#gmail.comellenbolt91#outlook.com
Here is my code
<form action="" method="get">
<textarea name="text" rows="5" cols="5" style="width: 400px;height: 200px;"></textarea>
<input type="submit" name="btn">
</form>
<?php
if(isset($_GET['btn'])){
$value=$_GET['text'];
$content= base64_decode($value);
echo $content;
}
?>
if(isset($_GET['btn'])){
$value=$_GET['text'];
$ids = explode(PHP_EOL, $value);
$content = '';
for ($i=0;$i<count($ids);$i++){
$content .= base64_decode($ids[$i]).'<br>';
}
echo $content;
}
You can avoid writing dangling <br> tags by using implode(). Here is a functional one-liner solution:
Code: (Demo)
$string = 'ZWxpZWVsYW1AeWFob28uY29t
ZWxpZXJvbWVyQGhvdG1haWwuY29t
ZWxsaXNjdXN0b21AZ21haWwuY29t
ZWxsYWxvMTAuZ3ZAZ21haWwuY29t
ZWxsZW5ib2x0OTFAb3V0bG9vay5jb20';
echo implode("<br>", array_map('base64_decode', explode(PHP_EOL, $string)));
Output:
elieelam#yahoo.com<br>
elieromer#hotmail.com<br>
elliscustom#gmail.com<br>
ellalo10.gv#gmail.com<br>
ellenbolt91#outlook.com
I am wanting to be able to create links that show up on the links.html page based on user submissions.
The links would follow this format TITLE, so quite simplistic.
Users will submit data via this form:
<form action="links.php" method="post">
<input type="text" placeholder= "URL:" name="url" required><br>
<input type="text" placeholder= "Title:" name="title" required><br>
<input type="submit">
And the PHP I'm using is
<?php
$url = $_POST["url"];
$title = $_POST["title"];
$text = "".$title." <br> \n"
$file = fopen("./data/links.html","a+ \n");
fwrite($file, $text);
fclose($file);
?>
I know that the issue lies with building the ".$url." part as there are also speech marks. How would you get around this given that the URL requires the "URL" format.
Thanks in advance.
You would need to add the proper escape slashes for the quoting issue
$text = "".$title." <br> \n";
becomes
$text = "".$title." <br> \n";
Or you could mix single and double quotes and not use escapes -
$text = "<a href='".$url."'>".$title."</a> <br> \n";
As long as the $text string is in double quotes, you can do this:
$text = "<a href='{$url}'>{$title}</a> <br> \n";
How can I put a custom error message with php? The x-moz-errormessage works great but not so the oninvalid="this.setCustomValidity.
// Formular erstellen
$Formular = $javascript . '
<br><p class="button_center"><a class="btn btn-primary" href="feedback_en.php">to the feedbacks</a></p>
<form name="Form" action="feedback_en.php?eintragen" method="post" accept-charset="UTF-8">
<p><br>
<label class="form-control-label">Your name
<input required="" oninvalid="this.setCustomValidity('Please fill this field.');" onchange="try{setCustomValidity('')}catch(e){};" x-moz-errormessage="Please fill the field!" type="text" class="form-control" name="name" value="' . $name . '" size="35" maxlength="35">
</label>
</p>
'
Escape the surrounding apostrophs in setCustomValidity('Please fill this field.'), so that they can be treated by PHP as literal characters, not as closing/opening characters of a PHP string.
Read Escape sequences.
So, correct is:
$Formular = $javascript . '... oninvalid="this.setCustomValidity(\'Please fill this field.\');" ...';
If, for example, you are writing this simple PHP code in an test.php page:
<?php
$myName = 'My name is 'Julie'.';
echo $myName;
then you'll receive the following error:
Parse error: syntax error, unexpected 'Julie' (T_STRING) in
[path-to]/test.php on line 3
Whereas the following code with escaped apostrophes will work:
<?php
$myName = 'My name is \'Julie\'.';
echo $myName;
I am creating a web page where the user select a number via a form.
HTML:
<form method="POST" style="font-size: 30px">
<input type="number" id="idname" name = "na" value=168 />
<label for="idname" >Number< /label>
<p><input type = "submit" name = "gesendet" value="Submit" />
<input type = "reset" /> </p>
</form>
using php I want to insert a txt file according to the number, such as 12_.txt or 166_.txt
<?php
$txtfile='"txt/' . $_POST["na"] . '_.txt"';
readfile($txtfile);
?>
The $txtfile string is O.K when "echoing" and the readfile function works with me when I read a file without the $_POST variable, but this simple code is just not working for me !!
Change:
$txtfile='"txt/' . $_POST["na"] . '_.txt"';
To:
$txtfile = 'txt/' . $_POST["na"] . '_.txt';
(Notice the extra double quotes)
I'm using the following code to post the content of a text box to an HTML file. When the info is submitted, I want there to be <br/> inserted where line-breaks are. I thought using this code would fix the problem:
$content = nl2br($_POST['content']);
But it seems to produce the following if there is already a <br /> in place:
Lorum ipsum.<br /><br />
How do I remedy this?
The post code:
<?php
$fn = "people.html";
if (isset($_POST['content']))
{
$content = stripslashes('content');
$content = nl2br($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
}
?>
How it's being submitted:
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
<textarea rows="25" cols="40" name="content"><?php readfile($fn); ?></textarea>
<input type="submit" value="Sauver">
</form>
From what I understand, if the html your inserting already contains a <br /> tag then it adds two because of the \n after the <br />, to remedy this you would need to strip the extra break tag from the input using str_replace or regex.
Then when echoing result back to the textarea you should use: <?=htmlentities(file_get_contents($fn))?> as SpoonNZ suggested.
note: using user submitted html without passing through htmlentities or $_SERVER["PHP_SELF"] within a form will open you upto XSS
You should not be storing your line breaks as <br> in your persistent storage (your text file). In your text file, line breaks should be stored as line breaks ("\n"). You should only transform them into HTML <br> tags when you output them into HTML. You also need to escape any user supplied content you put into HTML (for why, read The Great Escapism).
if (!empty($_POST['content'])) {
file_put_contents('people.txt', $_POST['content']);
}
Then:
<textarea rows="25" cols="40" name="content"><?php
echo nl2br(htmlspecialchars(file_get_contents('people.txt')));
?></textarea>
This way line breaks are line breaks at all times, they just need to be converted to <br>s because that's the only way to conserve them in an HTML context.
As for why line breaks are doubling, there simply seems to be an additional line break added at some point, possibly by the browser when the textarea content is submitted. You may want to trim your input/output to avoid any leading or trailing whitespace.
//$content = stripslashes('content');
$content = preg_replace('~\s*<br ?/?>\s*~',"<br />",$_POST['content']);
$content = nl2br($content);
Something like that should strip excess whitespace around line breaks for you.
Also, why are you using stripslashes? That should only be necessary if you have magic quotes on, and you shouldn't have magic quotes on. You also have got that line's syntax wrong anyway.
edit: Still trying to interpret what you're saying. I'm thinking this might help:
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
<textarea rows="25" cols="40" name="content"><?htmlentities(preg_replace('~\s*<br ?/?>\s*~i',"\n",(file_get_content($fn))); ?></textarea>
<input type="submit" value="Sauver">
</form>'
Will start with the correct data. Perhaps.