Page 1 of 1

How to echo php code

Posted: Mon May 15, 2006 1:18 pm
by wbryan6
I am trying to setup a survey form, and I want the survey to appear when the user first arrives (!$_POST['variable']), and then when they have complete the survey, I want the same page to display a thank you screen and submit the information to my database. The problem I am having is on the initial form because it also has a error check system (if ($_POST['variable'] {echo "Please select an option";}). My question is what is the syntax for placing php code in an echo command? echo "<?php some_code ?>"; only throws errors. Thanks for your help.

Posted: Mon May 15, 2006 1:27 pm
by hawleyjr

Code: Select all

<?php
echo htmlentities( '<?php echo \'Hello World\';?>' );
?>

Posted: Mon May 15, 2006 2:01 pm
by alex-weej

Code: Select all

echo some_expression;

Posted: Mon May 15, 2006 2:05 pm
by wbryan6
hawleyjr | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm sorry I didn't word that correctly.  Let me write an example of what I am hoping to accomplish

Code: Select all

<?
if (!$_POST['var'])
{
   $msg = "$query = "SELECT row FROM tbl"
							or die("Invalid query.");
						$res = mssql_query($query)
							or die("No results returned.");
						while($line = mssql_fetch_row($res))
						{
							foreach ($line as $value)
							{
							 echo "<input type=\"radio\" name=\"tutor\" value=\"$value\" />";
							 echo " $value&nbsp;&nbsp;&nbsp;";
							}
						}";
}
else 
{
   $msg = "Thank you for completing the survey";
}

echo $msg;
?>
I am wanting to have php code generated and executed. If this is possible, please let me know what I need to do. Thanks.


hawleyjr | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon May 15, 2006 2:24 pm
by alex-weej
While I can't figure out what you're trying to do, and whatever you're doing I am fairly certain you're trying to do it the Wrong Way™, the function you need to look at is

Code: Select all

eval();
.

Posted: Mon May 15, 2006 4:36 pm
by John Cartwright
use single quotes.

Code: Select all

echo '<?php echo "this is a test"; ?>';

Posted: Mon May 15, 2006 4:56 pm
by RobertGonzalez

Code: Select all

<?php
$msg = '';
if (!$_POST['var'])
{
   $query = "SELECT row FROM tbl" or die("Invalid query");
   $res = mssql_query($query) or die("No results returned.");
   while($line = mssql_fetch_row($res))
   {
      $msg .= '<input type="radio" name="tutor" value="' . $line['row'] . '" />';
      $msg .= "\n";
      $msg .= $line['row'] . "&nbsp;&nbsp;&nbsp;\n";
   }
}
else
{
   $msg = "Thank you for completing the survey";
}

echo $msg;
?>
Is this what you are after?

Posted: Tue May 16, 2006 2:28 pm
by wbryan6
Thanks Everah. That works great. It's nice when someone offers you a little taste of common sense.

Posted: Tue May 16, 2006 2:33 pm
by RobertGonzalez
You got it. And common sense tastes good (once you develop a flavor for it :wink: ).