Don't process html in value of input - php

I have an entry in my mysql table that contains html code:
<p>Hello!</p>
This shows up fine when I want to display it echo $entry
but when I place echo $entry in a textarea's value, it executes the code instead of showing it.
Is there any way to stop the code from executing and show the tags or convert to and from <>
Here is the code:
echo "<label for=\"details\">Details:</label><textarea id=\"details\" cols=\"60\" rows=\"10\" name=\"details\" value=\"" . $row["details"]."\"></textarea>";
Here is the entry:
<p><ol><li>HKCU/Software/Microsoft/Windows/NT/CurrentVersion/WindowsLegacy/DefaultPrinterMode<li> Set to 0 (on)</ol>

You have to escape special characters. You can do that with htmlspecialchars().
What's more, You should not set a value to the textarea but rather write it in its content:
<textarea>
<?php /* some code here */ ?>
</textarea>

i think this this will do it
<textarea><?php echo $row['details'];?></textarea>

try to save data lik
ascii_to_entities($this->input->post('ur_textara_name'));
and at the time of display
entities_to_ascii()

Related

xmlhttp.responseText to html code

I am trying to display an xmlhttp.responseText as HTML code and specifically to fill a dropdownbox, however it seems to be processed as a string and not HTML code.
I am using the code that I would like to display in HTML format as various menu <option>s in a <span> tag
javascript code within the xmlhttprequest function:
document.getElementById("test").innerHTML=xmlhttp.responseText;
Code in html that is found within the dropdown menu:
< span id="test">
< /span>
The php file that is called by the xmlhttprequest echo's the following:
$option="<option>";
(this is in a while loop)
{
echo $option.$row['productName'].$option="<option>";
}
if you want html outcome then use html, don't use special chars.
in your loop use
echo "<option>" . $row['productName'] . "</option>";
Use your code like,
$str='';
while(1) {
$str.='<option>'.$row['productName'].'</option>';
}
echo $str;
Also, option should be placed in drop down list like select not in span
So, change your HTML like,
<select id="test">
</select>

PHP variable with HTML formatting

Here is my variable that I am actually getting from my MySQL Database:
<h1>This is a H1</h1>
<p>NOT BOLD</p>
<p> </p>
<p><strong>BOLD</strong></p>
I am using TinyMCE to format the code.
Here is how I echo it
<?php
// WHILE LOOP GETTING $ROW FROM MYSQL
$conContent = $row['content'];
Then, when I go to the page, it displays the output like this...
http://i.snag.gy/BbMqx.jpg
I want the variable to make the echo formatted. So like then it will have all the html formatting.
You can insert your variable inside the <strong> tags using the following method:
<?php
/* getting row from your table */
$conContent = $row['content'];
?>
<strong> <?php echo $conContent; ?> </strong>
Another solution is:
$conContent = $row['content'];
echo "<strong>" . $conContent . "</strong";
//or echo "<strong> $conContent </strong";
If the styles are to be applied to all the rows, then you could use a foreach loop:
foreach($row as $v) {
echo "<strong>$v</strong";
}
Note: This assumes that you've the mysql array result stored in a variable called $row.
It's not just for <strong tags. You can use <h1>, <p>, <div> -- it doesn't matter. PHP will output the variable content in the location you specify.
Hope this helps!
Can you check the HTML source of the output? Is the HTML still around? It looks like strip_tags() or HTMLPurifier removes your HTML. Otherwise you would either see the formatting applied or the tags in the output.
If you have HTML code in your database you don't have to do anything with it in PHP, but can directly print it.

using echo statement after dynamic HTML

Here's the problem, I am trying to echo a statement or an array after dynamically generated HTML, and unfortunately the thing that i want to echo goes above the HTML, is there any way to echo it after that dynamic HTML or work around?
Code:
Link 1
Link 2
if(isset($_GET["id"]) && $_GET["id"] == "do_something") {
$html = "dynamic html generate";
echo $html;
//after this im using foreach
foreach($array as $item) { echo $item . "<br />"; }
}
As I click one of these two , dynamically generated HTML shows up. Now for example I have an array:
$array = array("error1", "error2");
All the generated PHP goes above the dynamic HTML :/.
How should i fix it so that i can echo all of this array below the dynamic HTML?
Thanks
Use buffering with ob_start
ob_start();
// dynamic html code generate
$dynamic_html = ob_get_clean();
echo $dynamic_html;
// your code
echo $dynamic_html;
Sounds like you missed some closing tags (most likely </table>) in the dynamic html. Thats why the later generated echo gets displayed at the top.
Example (Note the missing closing table):
<?php
echo "<table><tr><td>TableText</td></tr>";
echo "I should be bellow the table, but going to the top.";
?>
will produce:
I should be bellow the table, but going to the top.
TableText

add line break between 2 variables

I am echoing back these 2 variables in to a table, but wanted to know how to add a line break between these 2?
echo $row ['username'] . $row ['date_time'];
if you're printing an HTML output to your browser:
echo $row["username"]."<br />".$row["date_time"];
EDIT:
when printing to HTML - you better print the variables after passing them through htmlspecialchars function in order to avoid Cross-site-scripting (XSS), I'll do it this way:
echo htmlspecialchars($row["username"],ENT_QUOTES)."<br />".htmlspecialchars($row["date_time"],ENT_QUOTES);
if you want to print it to a file or something similar:
echo $row["username"]."\n".$row["date_time"];
you can always echo normal html from php.
echo "<div id=\"mydiv\"> Div content goes here </div>";
note the backslashes for double quotes wrapping mydiv to escape them.
so you can add a tag in between them to get a new line.
echo $row["username"] . "<br />" . $row["date_time"];

Using PHP to save from textarea to db

I have a post form with a text area in it. When I save the text from my textarea into mysql db, the text is saved with some white spaces before and after my actual test.
Why is happening this? How can I overcome this?
Thanks in advance
There's probably whitespace in your markup. For example:
<textarea>
<?php echo ($textareavalue); ?>
</textarea>
You could either remove the whitespace
<textarea><?php echo ($textareavalue); ?></textarea>
Or you could trim() the input before storing it to the database
$_POST ['textareavalue'] = trim ($_POST ['textareavalue']);
If you have code like this:
<textarea name="foobar">
<? echo $contents; ?>
</textarea>
Then you are adding whitespace to the value before/after the <? ... ?> tags (note, php does try to remove whitespace in some situations, so sometimes you can get away with it).
The fix is to do this:
<textarea name="foobar"><? echo $contents; ?></textarea>
you can use trim function before inserting into database for perticular that post data...
$text_area = trim($_POST['text_area']);
it will remove spaces from begining and end of the string...

Categories