Need some help to add PHP into HTML. I am trying to embed PHP code snippet in HTML however output is showing the snippet itself in the text box instead of evaluating it.
In HTML (register.php), added the below lines of code
<tr>
<td>
<?php utils::addControl('Name','label'); ?>
</td>
<td>
<?php utils::addControl('username','text'); ?>
</td>
</tr>
Here is the source of utils::addControl()
public function addControl($ctlName, $type) {
switch ($type) {
case 'text':
echo <<<EOT
<input type='text' name='$ctlName' value='<?php echo htmlspecialchars($ctlName); ?>'>
EOT;
break;
case 'label':
echo "<label for id='lbl$ctlName'>$ctlName</label>";
break;
}
Output is showing <?php echo htmlspecialchars(username); ?> in the text box.
What should be done in order to get the PHP snippet evaluated properly. TIA.
Servers are typically only configured for a single pass over a script while looking for PHP…
Source → PHP → Output
Not a double pass:
Source → PHP → PHP → Output
If it did perform a double pass then you would have to jump through hoops if you wanted to include the sequence <?php in the output (or even <? if short tags were turned on).
Write your PHP so it generates that output you want in the first place, don't try to generate PHP from PHP.
$htmlCtrlName = htmlspecialchars($ctlName);
echo "<input type='text' name='$htmlCtlName' value='$htmlCtrlName'>"
Related
I have created a custom wordpress post type everything works but my client asked me to insert a function that doenst show the button if the link field is empty that is also working but when I want to display the tekst or link the part where the php is inserted just doesnt shows up what am I doing wrong
I am able to get the data on other parts of this php file but not in this part of the page
<?php
$linktitle = $day_aray=get_field("under_shoe_button_title");
$linkexist = get_field("under_shoe_button_link");
echo($linktitle);
if (empty($linkexist)) {
echo '<html> <p></p></html>' ;
}
else {
echo '<html>
<a href="google.nl" class="button primary is-bevel box-shadow-3 box-shadow-4-hover expand" style="border-radius:5px;"
</html> <?php echo($linktitle); ?> <html><span></span>
<i class="icon-shopping-cart"></i></a>
</html>';
}
?>
If you would look carefully, you would notice, that you are echoing a string where, inside the string, you are trying to echo again. Even with little programming knowledge, you should understand, that it is not logical to do that.
The same goes for php opening <?php tag. You opened the tag at start of the page and later on, inside a string, you are trying to open it again. This does not work.
Instead, close the string (or escape it) and then add the echo option.
echo '<html>
<a href="google.nl" class="button primary is-bevel box-shadow-3 box-shadow-4-hover expand" style="border-radius:5px;"
</html>';
echo($linktitle);
echo '<html><span></span>
<i class="icon-shopping-cart"></i></a>
</html>';
And please, read the comments to you question and learn basic HTML
There are so many things wrong in your code
Firstly you are using echo inside echo you should use concatenation instead.
so you want to echo it like this
echo '<your html code>'.$linktitle.'<your other html code>';
Also your html code is wrong coz u are using many html tags.
i was wondering what the difference is when u put a variable in another variable with the notation. So i have a variable "Body" and in there is HTML tags, text and PHP variables, ive found out i can put them in between hooks but that they would react exactly the same. Example:
In this piece of code the variable is in between { } hooks.
$body = "
<table style='border: 1px;'>
<tr>
<td><b>Naam:</b></td><td>{$naam}</td><br>
</tr>
</table>"
And here it is not.
$body = "
<table style='border: 1px;'>
<tr>
<td><b>Naam:</b></td><td>$naam</td><br>
</tr>
</table>"
And this both reacts exactly the same. So can anyone tell me if this has an actual use, or that this is just like all PHP with the 10 ways to do the same thing.
Thanks in advance.
Addition
This is not a duplicate ofThis. It does not explain the part of why a variable inside of a variable can be put in between curly brackets.
Usage of curly braces inside " is particularly useful when you want to add complex instruction or access object/array properties.
This code will perfectly work
<?php echo "Test {$foo['bar']}"; ?>
<?php echo "Test {$foo->bar}"; ?>
While this one will fail
<?php echo "Test $foo['bar']"; ?>
<?php echo "Test $foo->bar"; ?>
So yes, it's not particularly useful if you are accessing a simple variable, but when you want to play with array and object, it's useful.
So I got the following: I'm creating dynamic pages (based on the page ID (obtained through $_GET)). My page consists of a $_get check (the html may only be shown if a $_GET variable is set) and the echo'ing of a page with html tags + php variables. Things like:
<table>
<tr>
<td>Name:</td>
<td>myFunction('foo');</td>
</tr>
</table>
etc etc etc.
I include this code above myincludedfile.php into my main file with functions include and isset($_get) .
echo include 'myincludedfile.php';
This, however, not seems to work. Altough the html gets shown, the variables and functions remain text and don't get executed.
Can anyone help me out?
Change it like this:
<table>
<tr>
<td>Name:</td>
<td><?=myFunction('foo');?></td>
</tr>
</table>
That way you are actually opening a small PHP block in the template. <?= is a shorthand notation for <?php echo, although it depends on your server configuration if the shorthand is enabled.
If the function doesn't return a value, but echos it instead, you can leave out the = as well and just execute the function like this: <? myFunction('foo'); ?> or the long notation: <?php myFunction('foo'); ?>.
In general, you don't need <?php at the start of your file. It's just that everything inside <?php .. ?> tags is executed as php and the rest is considered static output.
B.t.w. you don't need to use echo when you include a file.
Here is another alternative
$myfunction = myFunction('foo');
echo <<<CODE
<table>
<tr>
<td>Name:</td>
<td>$myFunction</td>
</tr>
</table>
CODE;
include 'myincludedfile.php';
This is called the heardoc syntax http://php.net/manual/en/language.types.string.php#example-75
You will have to include the file like this:
include 'myincludedfile.php';
What you are doing, is echo'ing the php file
Look:
<?php
$html="<h1>Hello</h1>";
function hello()
{
echo "<h1>Hello</h1>";
}
function hello2()
{
?>
<h1>Hello</h1>
<?php
}
?>
All these does the same thing.
to use them, you can do the following:
<?php
hello(); // Hello
hello2(); // Hello
echo $html; // Hello
?>
I don't think i get what you need, but try to be more specefic.
<?php
for($i=0;$i<=100;$i++)
{
?> // why this
<tr>
<td> <?php echo $i; ?></td>
<td><?php echo "tên sách $i"; ?></td>
<td><?php echo "noi dung sach $i";?></td>
</tr>
<?php
}
?>
So that is the scenario I'm looking to understand. Thanks
The <?php opens a php script sequence so it is saying that inside this is php code. ?> closes that sequence and says that I am not longer using php code.
In your case the php opens up and starts a for loop. Inside of the for loop a table is made but it is done using html not php. Then in each table piece, php is being used to echo (write something to the screen) some content into the table. Then finally at the end the php for loop must be finished with a closed bracket. I hope that makes sense.
The meaning of that syntax, is essentially telling the server to stop processing PHP. What follows in the page, is HTML code. That is <?php tells the server process this as PHP script and the ?> says stop processing PHP script.
Normally, you'll not see HTML outputted in this manner, but instead using PHP's echo to write the HTML
<?php
for($i=0;$i<=100;$i++)
{
echo "
<tr>
<td>{$i}</td>
<td>\"tên sách {$i}\"</td>
<td>\"noi dung sach {$i}\"</td>
</tr>
";
}
?>
?> is the end tag of PHP much like </td> is an end tag of a table cell.
The PHP parser lets you enable and disable parsing by including the PHP start and end tags <?php and ?> in your document. These two samples have the same output:
One
-------------------
echo '<td>' . $foo . '</td>';
Two
-------------------
?>
<td><?php echo $foo; ?></td>
<?php
As far as which is easier to read, well, that's a matter of preference I suppose.
See also: the shortcut tag <?= for echoing a value.
You can come in and out of php execution any time you like. If text is not between php tags, it will be output rather than executed.
<?php // start executing
if($test){ ?> <!-- start a php if statement -->
<p>This will only be output if the php test is true</p>
<?php } ?> <!-- don't forget to close the if statement -->
<p>This will always be output</p>
PHP allows you to min PHP code and HTML markup in the same file, so it needs a way to tell them apart.
If you don't enclose your PHP code between <?php and ?>, it won't be processed, and will be output as HTML.
You should read a minimum of documentation before start a project (and ask basic questions).
First part (l.1 to 4) is PHP code, here you start a for loop.
Second part (l.5 to 9) is HTML code, which is include in the loop.
Third and last part is here to close the loop.
You can do the same with :
<?php
for($i=0;$i<=100;$i++)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>tên sách".$i."</td>";
echo "<td>noi dung sach".$i."</td>";
echo "</tr>";
}
?>
This is a way to embed the HTML code without having to use the php print or echo function. using
?>
Another way to get at the same result would be this:
";
echo " $i ";
echo " tên sách $i ";
echo " noi dung sach $i ";
echo "";
}
?>
I want to grab data from a mysql database by using php. The data looks something like this:
apple 3
orange 2
banana 4
I want to take the data and put it in a html table and use css to make it look pretty, but I dont want to deal with it inside <?php ?>
After I grab the
$result = mysql_query("SELECT * FROM Table");
can I reference the result variable outside the <? php ?> tags?
No. PHP can only be done in <?php ... ?> or <?= ... ?>. Use a template engine such as Smarty if you want substitution in this manner.
in short, no you cant, it is a php variable (technically a resource in this case) so you have to parse it through the php engine, which requires the php tags
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr><td>'.$row['fruit'].'</td><td>'.$row['id'].'</td></tr>';
}
echo '</table>';
Short answer is no. HTML cannot deal with dynamic content.
If you want to cut down the amount of echo statements within your code you can store the html within a given variable and then make reference to it.
I find it better to do the following:
<table>
<?php foreach($result as $row): ?>
<tr>
<td><?php echo $row['fruit']?></td>
<td><?php echo $row['id']?></td>
</tr>
<?php endforeach; ?>
</table>
This provides clarity and minimizes concatenation.