Jump to content

embedding php in an echo statement


Perad

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?
Link to comment
https://forums.phpfreaks.com/topic/24183-embedding-php-in-an-echo-statement/
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]

Archived

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

×
×
  • Create New...

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.