Unwanted whitespace when echoing output in php - 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>

Related

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.

php how to get the textarea tag from my sql in a textarea in php

I have a code which is all working right but when the code in the database has a it goes wrong
code like this
<tr><td><b>EN Page Content : </b></td><td><textarea name="pageContent" cols="150" rows="100"><?php echo getContentWithIndex("posts",$_GET['pageID'],"content");?></textarea></td></tr>
and content is like this
<textarea placeholder="Message*:" data-constraints='#Required #Length(min=20,max=999999)' name="SenderHRMessage"></textarea>
so it has the end of the </textarea> as an end for the first one and never display any other content in the first textarea.
Change your code to this: (I have reformatted it so that it fits on the SO post)
<tr>
<td><b>EN Page Content : </b></td>
<td>
<textarea name="pageContent" cols="150" rows="100"><?php
echo htmlentities(getContentWithIndex("posts",$_GET['pageID'],"content"));
?></textarea>
</td>
</tr>
The reason is that if you have HTML code that you want to display as HTML code you need to encode it to text.
See: http://php.net/manual/en/function.htmlentities.php

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);
?>

How to echo $row["Text'] to a TextField

Using PHP I am able to query MySQL database and see the results echo using:
echo $row['Text'];
I would like the information to load into TextField myAnswer instead. Can anyone help?
Thanks
Like this
<input type="text" name="myAnswer" value="<?php echo htmlspecialchars($row['Text']) ?>" />
or
<textarea name="myAnswer" rows="6" cols="40"><?php echo htmlspecialchars($row['Text']) ?></textarea>
None of this seems to work for me. Upon looking further, I found the following:
http://www.daniweb.com/forums/thread252486.html -- You can not manipulate text fields like java or C# or as3 but you can echo or print out html tags and text. if you echo your text you will need to space or manipulate using css and html.
So I guess I can't get the results back from the query in a textbox using PHP and MySQL.
Thanks anyways
Going on assumption from your question, you might try something like:
<input type="textbox" value="<?php echo $row['Text']; ?>" />
If you're thinking about a textarea control it'd be like this:
<textarea><?php echo $row['Text']; ?></textarea>
<input type="text" name="myAnswer" value="<?php echo $row['Text']; ?>" />
<?php if ($row['Text']) {?>
<textarea><?php echo htmlspecialchars($row['Text']); ?></textarea>
<?php } ?>
if you are displaying in the same page, otherwise you can just leave out the if .. check.

Categories