Jump to content

Archived

This topic is now archived and is closed to further replies.

Perad

embedding php in an echo statement

Recommended Posts

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

I need this line at the top of the form.

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

I have come up with this...

[code] 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)) {[/code]

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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)...

[code]
<?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)) {
?>
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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... 

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.