Tab spaces in textarea issue - php

Here's my code for my simple CRUD site:
<textarea id="bb" class="form-control"name="text" placeholder="Edit this question, <?= $_SESSION['name'] ?>?"> <?= $text ?> </textarea>
But the problem is that the textarea has 2 tab spaces at the beginning of the text and after the text there is a space with a tab space next to it.
I'm very sure that the text is trimmed in the MySQLi database. I'm using Bootstrap BTW.
Thanks in advance.

Remove space between <textarea></textarea>
<textarea id="bb" class="form-control"name="text" placeholder="Edit this question, <?= $_SESSION['name'] ?>?"><?= $text ?></textarea>

Related

Unwanted whitespace when echoing output in php

I am currently trying to write a program that outputs the stdout output of a c program to a text area in html (using php).
This is what I have thus far:
<?php
$output = shell_exec("./hello");
?>
<textarea name="view" cols="80" rows="24" readonly>
<?php echo $output; ?>
</textarea>
This prints the correct output, but it adds 2 tabs ahaed of the first character, so instead of
Hello World!
It prints
Hello World!
This is my first time using either php or html, so I'm sure it's something fairly easy to fix. I just can't seem to find anything addressing this specifically online, so any help would be greatly appreciated!
(Just in case it's not obvious the c program is just a simple hello world, that prints fine on the terminal).
Everything that is out of the php tags are echoed "as-is". Consequently, you echoed two tabs in front of your <?php echo $output; ?> within the textarea.
try this :
<textarea name="view" cols="80" rows="24" readonly><?php echo $output; ?></textarea>
This is because of your whitespaces inside the <textarea> tags:
<textarea name="view" cols="80" rows="24" readonly>
<?php echo $output; ?>
</textarea>
The text between your tags will be taken as the textareas value.
So your value will be 4 whitespaces and then the output of your echo statement.
tl;dr: Remove your indent (4 spaces):
<textarea name="view" cols="80" rows="24" readonly>
<?php echo $output; ?>
</textarea>

Code in original form

I have HTML code for a table in database. I want to present that code in its original form in a textarea so that I can make changes in it. When I echo it appears in compiled form (table), not in original HTML code. How can I display its original HTML code?
<textarea rows="40" cols="30" name="content" id="content">
<?php echo '<pre>'.$row['content'].'</pre>'; ?>
</textarea>
You can use the <code> tag to achieve this.
Try replacing < by <
<?php echo '<pre>'.str_replace('<', '<',$row['content']).'</pre>'; ?>

Cant enter PHP value for <textarea>

I have made a page in HTML/PHP and I would like a function to be able to edit some files though the web-page. I have a <textarea> tag to do this.
However, I cannot seem to enter any default value to the <textarea> through PHP variables.
Here is my code
<?php
$text = file_get_contents("file.txt");
?>
<textarea name="input">
<?php echo $text; >?
</textarea>
But the text that appears is "<?php echo $text; ?>"
Please help, I couldn't find help anywhere else.
You misplaced your ending PHP tag (>? should be ?>) when echoing the result into the textarea.
<?php
$text = file_get_contents("file.txt");
?>
<textarea name="input">
<?php echo $text; ?>
</textarea>
You should check your php knowledge:
line 5 should be ?> not >?. Then it will be valid.

line break in textarea without displaying HTML tags

I am using this HTML/PHP Code
<textarea name="ticket_update" id="ticket_update" cols="70" rows="2"><?php echo 'Ticket '.$ticket["ticketnumber"].'\n'.$result["notes"]; ?></textarea>
I have tried using \n \n\r and <br /> but it is displaying the HTML tags in the textarea.
how can i stop them displaying?
Use
as a line break.
Like this :
<textarea name="ticket_update" id="ticket_update" cols="70" rows="2"><?php echo 'Ticket '.$ticket["ticketnumber"].'
'.$result["notes"]; ?></textarea>
use "\n" as opposed to '\n' to display a line break;
To remove HTML tags from the contents, use strip_tags() function;
a complete example would look like this:
<textarea name="ticket_update" id="ticket_update" cols="70" rows="2">
<?php echo strip_tags('Ticket '.$ticket["ticketnumber"]."\n".$result["notes"]); ?>
</textarea>
Also, it looks like there might be some PHP notices or warnings occuring in your code; If your $ticket array does not have ticketnumber key, you will get error message including some HTML tags; You should either check for the presence of each key you use with array_key_exists, or silence errors with # like this:
echo #strip_tags('Ticket '.$ticket["ticketnumber"]."\n".$result["notes"]);
A textarea cannot have any child elements. If you don't want error recovery to try to render tags, then don't put tags in the element.
Try html_entity_decode()
<?php
$ticket = '50';
$nodes = 'sample'
?>
<textarea name="ticket_update" id="ticket_update" cols="70" rows="2"><?php echo 'Ticket '.$ticket.html_entity_decode("\n").$nodes; ?></textarea>
Demo Output
You can try this:
<textarea name="ticket_update" id="ticket_update" cols="70" rows="2"><?php echo 'Ticket: '.$ticket["ticketnumber"].'
'.$result["notes"]; ?> </textarea>
= line break
= Line Feed and 
 = Carriage Return
DEMO HERE

How to insert a newline by pressed enter key into a database in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to save user-entered line breaks from a TEXTAREA to a database?
When I press an ENTER KEY in a textarea it goes to newline. But when I receive this value from action page it converts to a white space. I want to receive ENTER KEY character as a newline from action page as not a white space.
Sample code : this is the HTML code
<form action="go.php" method="POST">
<textarea name="aa" cols="10" rows="10"></textarea>
<form>
Her is the go.php code :
<?php
$txt=$_POST['aa'];
echo $txt;
?>
If I input like
"this is me(PRESS ENTER KEY)this is he(PRESS ENTER KEY)".
I want to the output as like
"this is me(newline)this is he(newline)".
But I am getting from go.php like
"this is me this is he".
Here is no any newline but white space.
Please anyone help me. Why it is happen.
I believe you want nl2br. This will insert HTML line breaks before all newlines in a string. If you are planning to store this in a database, you'll need something similar to mysql_real_escape_string() to escape the line breaks.
<?php
echo nl2br("foo isn't\n bar");
?>
The HTML would look like:
foo isn't<br />
bar
Your code would then look like:
<?php
$txt=$_POST['aa'];
echo nl2br($txt);
?>
You need to use nl2br function to capture and replace newline. Working code below;
<form action="go.php" method="POST">
<textarea name="aa" cols="10" rows="10"></textarea>
<input type="submit" value="s">
</form>
Here is the go.php code :
<?php
$txt=$_POST['aa'];
echo nl2br($txt);
?>

Categories