Embed php code in HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cliff
Forum Newbie
Posts: 2
Joined: Tue Apr 10, 2012 8:15 am

Embed php code in HTML

Post by cliff »

Hi!

I am new to PHP. I am trying to store array values in HTML table. For that I have written following code. Everything worked before I embedded php code. After I added php code I got an error HTTP 500 Internal Server error. Please help me.

Below is the code:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<body>
Please select your desired states:
<br><br>
<?php
$state_list = array("AL"=>"Alabama","AK"=>Alaska");
?> 
<form id=form1 >
	<table id=myTable border=1>
		<tr>
			<th>Column 1</th>
			<th>Column 2</th>
			<th>Column 3</th>
		</tr>
<?php foreach ($state_list as $row) { ?>
	<tr>
       <td><?php echo $row[0]; ?></td>
       <td ><?php echo $row[1]; ?></td>
       <td ><?php echo $row[2]; ?></td>
</tr>
     <?php } ?>

		
</table>
	<br>
	<input type='submit' value='Save' onclick=" " />
</form>
</body>
 </head>
</html>


thanks for the help in advance
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Embed php code in HTML

Post by social_experiment »

Code: Select all

<?php
// missing a " infront of Alaska
$state_list = array("AL"=>"Alabama","AK"=>Alaska");
?>
There is a missing " in the syntax above, not sure if that is what is causing the issue
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
cliff
Forum Newbie
Posts: 2
Joined: Tue Apr 10, 2012 8:15 am

Re: Embed php code in HTML

Post by cliff »

Hi, Thanks for the help. Exactly, That is what caused the error. Thanks again.

Now, I am trying to click/select the data of table cell. To achieve this, I am trying jQuery code. Is there any easier way to achieve this than using jQuery. Your direction would be helpful. Thanks.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Embed php code in HTML

Post by social_experiment »

You could use javascript but jQuery should be fine; have a look at this url for more help on the subject http://coderzone.org/library/Get-Table- ... k_1014.htm
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: Embed php code in HTML

Post by danjapro »

Simply converting your page to extension .phtml, will allow you to utilize php code structure without editing or adding any jQuery.
Post Reply