开发者

How to echo a form in a php var without losing editor highlight syntax

I've been trying the heredoc method like this:

<?php  echo $form = <<<HTML ?>  

here follows the html...
<form method="post" >
<ta开发者_如何学JAVAble border="0" cellpadding="0"  cellspacing="0" id="reserv_table">
<tr>
<td><span id="message"><b>for you : 9€</b> &nbsp;</span></td>
<td><input type="text" name="_membre" id="_membre" style="width: 40px;" class="text disabled" disabled="disabled" /></td>
<td valign="top">
<table>
<tr> 


<?php HTML; ?> 

But I get an error.


If you're just echoing the form you don't even need PHP's heredoc syntax. Just break out of PHP using the closing delimiter, then return to PHP once you're done outputting the HTML form.

EDIT: OK, so you need the form output to be stored in a variable, but you also want it to be printed to the browser? No problem, use output buffering (in particular, ob_start() and ob_get_flush():

<?php

// Begin output buffering
ob_start();

// Break out of PHP...

?>

<form method="post" >
<table border="0" cellpadding="0"  cellspacing="0" id="reserv_table">
<tr>
<td><span id="message"><b>for you : 9€</b> &nbsp;</span></td>
<td><input type="text" name="_membre" id="_membre" style="width: 40px;" class="text disabled" disabled="disabled" /></td>
<td valign="top">
<table>
<tr> 

<?php

// Back to PHP, store the HTML in $form AND print it to the browser
$form = ob_get_flush();

?>


Your PHP tags <?php and ?> are messing with your Heredoc tags.

  • Remove the ?> at the end of the first line
  • Remove the <?php and the space at the start of the last line
  • Remove the space and the ?> at the end of the last line and place it one line below

This is all assuming that you need all the code to be placed into the $form variable for some reason. If you don't, pick BoltClock's answer.

If after fixing your code, your editor won't syntax highlight it properly, then your editor is at fault. You could either replace it with a different editor, or just avoid using Heredoc syntax. I tend to avoid using Heredoc but mainly because I think it makes things messy.


Why not to try heredoc according to it's syntax rules?

though to print out this form, you need no heredoc.
just print it out as is

here follows the html...
<form method="post" >
<table border="0" cellpadding="0"  cellspacing="0" id="reserv_table">
<tr>
<td><span id="message"><b>for you : 9€</b> &nbsp;</span></td>
<td><input type="text" name="_membre" id="_membre" style="width: 40px;" class="text disabled" disabled="disabled" /></td>
<td valign="top">
<table>
<tr> 


Why don't you just remove the php code like this

<form method="post" >
<table border="0" cellpadding="0"  cellspacing="0" id="reserv_table">
<tr>
<td><span id="message"><b>for you : 9€</b> &nbsp;</span></td>
<td><input type="text" name="_membre" id="_membre" style="width: 40px;" class="text disabled" disabled="disabled" /></td>
<td valign="top">
<table>
<tr>

Mean no echo is needed.


If you need to send static data you can do it putting it directly in the code outside the PHP tags

ex:

   <?php 
     echo "PHP Code";
   ?>
     HTML code

   <?php
     echo "PHP Code again";
   ?>

Also if you are triying to create a PHP String you need to keep all the content in your PHP Code

ex:

   <?php 
     echo "PHP Code";
     $heredoc_variable = <<<HTMLCODE
       <b> This is html code into a heredoc variable in php </b>
     HTMLCODE;
   ?>


If you really need it to be a variable and are looking for a really simple method, just include the entire form syntax in a variable with escaped quotes like so:

<?php

$output = "
<form method=\"post\" >
<table border=\"0\" cellpadding=\"0\"  cellspacing=\"0\" id=\"reserv_table\">
<tr>
<td><span id=\"message\"><b>for you : 9€</b> &nbsp;</span></td>
<td><input type=\"text\" name=\"_membre\" id=\"_membre\" style=\"width: 40px;\" class=\"text disabled\" disabled=\"disabled\" /></td>
<td valign=\"top\">
<table>
<tr>
";

echo $output; //or wherever you need to echo it...

?>

This method is a simple solution that is bound to use less resources than ob_start(); in my opinion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜