How to echo php code

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
wbryan6
Forum Newbie
Posts: 22
Joined: Sat Feb 04, 2006 12:13 pm

How to echo php code

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Code: Select all

<?php
echo htmlentities( '<?php echo \'Hello World\';?>' );
?>
alex-weej
Forum Newbie
Posts: 19
Joined: Sun May 14, 2006 11:20 am

Post by alex-weej »

Code: Select all

echo some_expression;
wbryan6
Forum Newbie
Posts: 22
Joined: Sat Feb 04, 2006 12:13 pm

Post 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]
alex-weej
Forum Newbie
Posts: 19
Joined: Sun May 14, 2006 11:20 am

Post 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();
.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

use single quotes.

Code: Select all

echo '<?php echo "this is a test"; ?>';
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
wbryan6
Forum Newbie
Posts: 22
Joined: Sat Feb 04, 2006 12:13 pm

Post by wbryan6 »

Thanks Everah. That works great. It's nice when someone offers you a little taste of common sense.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You got it. And common sense tastes good (once you develop a flavor for it :wink: ).
Post Reply