Jump to content


Photo

embedding php in an echo statement


  • Please log in to reply
4 replies to this topic

#1 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 17 October 2006 - 08:59 AM

I have a dropdown box where the options are called directly from the database.

I need this line at the top of the form.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

I have come up with this...

function add_article () {
			echo "<form action=\"<?php echo \$_SERVER['PHP_SELF']; ?>\" method=\"post\">";
			echo "<select name=\"category\">";
					$query = "SELECT category_id, category_name FROM article_category ORDER BY category_id";
					$result = mysql_query ($query) or die("Problem with the query: $query on line:" . __LINE__ . "<br>" . mysql_error()); 
					while ($row = mysql_fetch_assoc ($result)) {

The form works, but doesn't loop back to the original page. Does anyone know a way around this?

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 17 October 2006 - 09:39 AM

That code looks good to me...

When the form loads, right click and view source and see what's being put in the 'action' parameter.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 17 October 2006 - 09:52 AM

I did just one quick edit -- but might possibly work...  when working with PHP there is aboslutely no reason to have <?php tags within it..  just a waste of space and gives you the chance of getting hung up....  so try this code in your function and see if it works....  though why you're not using a templating engine to seperate html from php i dunno (since i switched -- im loving my job as a developer)...

<?php
function add_article () {
			echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";
			echo "<select name=\"category\">";
					$query = "SELECT category_id, category_name FROM article_category ORDER BY category_id";
					$result = mysql_query ($query) or die("Problem with the query: $query on line:" . __LINE__ . "<br>" . mysql_error()); 
					while ($row = mysql_fetch_assoc ($result)) {
?>


#4 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 17 October 2006 - 09:56 AM

I'm new to php... whats a templating engine?

As for the problem itself I rewrote my form and changed my php function to get it working.

#5 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 17 October 2006 - 10:10 AM

templating engine is like smarty -- which is the one i use... makes life easy -- though if you're new to php hold off on it a while... and look into it when you start to build a large site... 




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users