xmlhttp.responseText to html code - php

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>

Related

Php and Html code within echo

I'm writing an if statement in which a button needs to show if the cart is empty.
For this button I need to get the form key of the product for the data-url
So something like this:
Order
As mentioned above I need to wrap this button in an if statement, so something like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()){
echo 'Order';
}
else{
'<p>hello</p>';
}
?>
But obviously I can't have php echo within echo. Can anybody point me in the right direction of how to do this?
You don't put PHP inside HTML inside PHP. Since you're already in the context of PHP code, just concatenate the values you want to the output:
echo 'Order';
The resulting output is always just a string. You can simply build that string with whatever values you have.
You can just use string concatenation:
echo '<a href="#" data-url=".../' . Mage::getSingleton(...) . '"' ...
Simply don't open PHP up again. You can terminate the HTML interpretation inside an echo.
Your code should look like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()) {
echo 'Order';
}
else {
'<p>hello</p>';
}
?>

How to echo javascript code which contains php tags inside?

I usually echo script alert by simply
echo '<script type="text/javascript">alert("'.$my_message.'");</script>';
but how to echo the script when the script contains php tags? (see my example code)
Because of my php obfuscator script, I have to use only one <?php ?> tag. so in this case I need to echo the javascript without having <?php tags. What are the possible solutions in this case?
<?php
...some php code...
<script type="text/javascript">
jQuery(document).ready(function($){
$i = 0;
$('.wrapper_vertical_menu .megamenu_menu').append('<div class="more-wrap"><span class="more"><?php echo $this->__("More"); ?></span></div>');
$('.wrapper_vertical_menu .megamenu_menu > li.megamenu_lv1').each(function(){
$i ++;
if($i>13){
$(this).css('display', 'none');
}
});
... more js code ...
JavaScript doesn't "contain PHP tags". All your PHP code needs to do is build the resulting output string (which happens to be JavaScript code, but that doesn't matter to PHP) and echo it.
So where you have something like this:
echo "some javascript code <?php echo some_php_value; ?> more javascript code";
What you really want, quite simply, is this:
echo "some javascript code " . some_php_value . " more javascript code";
Don't try to nest <?php ?> tags within each other. Just concatenate the actual output you want and echo that output.

Don't process html in value of input

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()

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

Categories