Web Hosting Talk







View Full Version : PHP Inside of HTML


Goldfiles
01-05-2006, 02:10 PM
Here is my problem. I have a .php page where part of it is a PHP Switch Case. Then inside the php switch there is an HTML form. Well, I want to include more php inside of that html. How do I write that? I'm kinda new to php and I can't find out how to code that.

Here is what I have so far:

<?php

...Various PHP Coding...

echo ('
<fieldset>
<legend>
Update a Product
</legend>
<form method="POST" action="add.php">
ID <input type="text" name="idnum" size="4">
Name <input type="text" name="name" size="25">
Price <input type="text" name="price" size="6">
Type <select name="type">
<option value="3"><?php echo $idnum; ?>
<option value="2">Trendy
<option value="1">New
</select>
Already Sold? <select name="sold">
<option value="1">Yes
<option value="0">No
</select><br /><br />
<table><tr><td>Description</td><td><Textarea rows=5 cols=45 name="description"></Textarea></td></tr></table><br /><br />

<input type="submit" value="Submit"><input type="reset">
</form>
</fieldset>
');
?>

I used the echo (' ..... '); notation to put the HTML into my <?php ?>, but now I want to put more php into the html. How can I do this? (notice I made the problem section bold in the code)

Thanks for any help! :lovewht:

Goldfiles
01-05-2006, 04:18 PM
Seemed to have figured it out on my own, but I can't get a php if statment to work. bolded start of the problem part


<?php
echo ("
<fieldset>
<legend>
Update a Product
</legend>
<form method=POST action=add.php>
ID <input type=text name=idnum size=4 value=" . $idnum . ">
Name <input type=text name=name size=50 value=" . $name . ">
Price <input type=text name=price size=6 value=" . $price . ">

" .
if($type==2)
{
echo('
Type <select name=type value=" . $type . ">
<option value=3>Crystal
<option value=2 selected=selected>Trendy
<option value=1>Mommy
</select>
Already Sold? <select name=sold value=" . $sold . ">
<option value=1>Yes
<option value=0>No
</select><br /><br />
')
}
. "

<table><tr><td>Description</td><td><Textarea rows=5 cols=45 name=description>" . $description . "</Textarea></td></tr></table><br /><br />

<input type=submit value=Submit><input type=reset>
</form>
</fieldset>");
?>

Christopher Lee
01-05-2006, 04:50 PM
It doesn't work because you're trying to insert a statement inside an echo and you can't do that.

echo the first part, run your if statement block, then echo the second part.

Goldfiles
01-05-2006, 07:01 PM
cool, thanks!

slack
01-07-2006, 12:16 AM
You can mix your PHP and HTML in a PHP script, so you don't have to 'echo' all of your output.

For instance:

<?php
$something='Hey There';
?>
<html>
<body>
<h1>Just wanted to say <?php echo($something); ?>!</h1>
</body>
</html>



This is of great benefit because you can still use your HTML editor very effectively to design your pages. Trying to echo all of your HTML output is going to make design and maintenence a pain by comparison.

ikebowen
01-07-2006, 12:40 PM
... you're trying to insert a statement inside an echo and you can't do that.
Not like that, anyway. Inline conditionals also work (might look like what I have below), but it would be best to split it up into three parts instead.


<?php
echo ("
[ your beginning HTML ]
" .
( $type == 2 ? "[ your middle HTML ] " : "" )
. "[ the rest of your HTML ]");
?>