我创建一个php页面我希望将所有html存储在单个php变量中然后使用ajax,我如何将这个html存储在php变量名称#msg中,这是我的代码:
$msg="<div id="new2" style="background:#fff; width:779px; min-height:310px;">
<table align="center" width="779px" cellpadding="0" cellspacing="0" id="tb1">
<tr style="height:28px; font-size:12px;">
<td style="width:246px">Name</td>
<td style="width:145px">CITY</td>
<td style="width:102px">ABCDEF</td>
<td style="width:71px">BUDGET</td>
<td style="width:102px">MEMBERSHIP</td>
<td style="width:84px" id="td1">UNTIL</td>
</tr>
</table>
<?php
$color='#DADDE2';
$business=getbussinesspagination(0,$start,$per_page);
if ($business){
while ($row = $business->fetch_assoc ()){
if($color=='#DADDE2')
$color='#F9F9F9';
else
$color='#DADDE2';
?>
<div id="m1" style="width:246px; height:28px; background:<?php echo $color; ?>; float:left"><p style="padding-left:45px; margin-top:8px;"><?php echo ($row['title']); ?></p></div>
<div id="m2" style="width:145px; height:28px; float:left; margin:0 0 0 1px;background:<?php echo $color; ?>"><p style="padding-left:30px; margin-top:8px;"><?php echo ($row['cityName']); ?></p></div>
<div id="m3" style="width:101px; height:28px; float:left; margin:0 0 0 1px;background:<?php echo $color; ?>"><p style="padding-left:40px; margin-top:8px;"><?php if($row['article']!=''){ echo 'A';} if($row['event']!=''){echo 'E'; } ?></p></div>
<div id="m4" style="width:70px; height:28px; float:left; margin:0 0 0 1px;background:<?php echo $color; ?>"><p style="padding-left:25px; margin-top:8px;"><?php echo ($row['credit']); ?></p></div>
<div id="m5" style="width:99px; height:28px; float:left; margin:0 0 0 1px;background:<?php echo $color; ?>"><p style="padding-left:25px; margin-top:8px;"><?php echo ($row['name']); ?></p></div>
<div id="m6" style="width:84px; height:28px; float:left; margin:0 0 0 1px;background:<?php echo $color; ?>"><p style="padding-left:10px; margin-top:8px;"><?php echo ($row['renewal_date']); ?></p></div>
<div id="m7" style="width:23px; height:28px; float:left; margin:0 0 0 1px;background:<?php echo $color; ?>"><img src="messg.png" style="margin:8px 0 0 2px;"></div>
<?php
}
}
?>
<div id="m1" style="width:246px; height:28px; background:#DADDE2; float:left"></div>
<div id="m2" style="width:145px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m3" style="width:101px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m4" style="width:70px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m5" style="width:99px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m6" style="width:84px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m7" style="width:23px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"><img src="messg.png" style="margin:8px 0 0 2px;"></div>
<div id="m1" style="width:246px; height:28px; background:#F9F9F9; float:left"></div>
<div id="m2" style="width:145px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m3" style="width:101px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m4" style="width:70px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m5" style="width:99px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m6" style="width:84px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m7" style="width:23px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"><img src="messg.png" style="margin:8px 0 0 2px;"></div>
<div id="m1" style="width:246px; height:28px; background:#DADDE2; float:left"></div>
<div id="m2" style="width:145px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m3" style="width:101px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m4" style="width:70px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m5" style="width:99px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m6" style="width:84px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m7" style="width:23px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"><img src="messg.png" style="margin:8px 0 0 2px;"></div>
<div id="m1" style="width:246px; height:28px; background:#F9F9F9; float:left"></div>
<div id="m2" style="width:145px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m3" style="width:101px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m4" style="width:70px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m5" style="width:99px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m6" style="width:84px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m7" style="width:23px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"><img src="messg.png" style="margin:8px 0 0 2px;"></div>
<div id="m1" style="width:246px; height:28px; background:#DADDE2; float:left"></div>
<div id="m2" style="width:145px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m3" style="width:101px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m4" style="width:70px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m5" style="width:99px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m6" style="width:84px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m7" style="width:23px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"><img src="messg.png" style="margin:8px 0 0 2px;"></div>
<div id="m1" style="width:246px; height:28px; background:#F9F9F9; float:left"></div>
<div id="m2" style="width:145px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m3" style="width:101px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m4" style="width:70px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m5" style="width:99px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m6" style="width:84px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m7" style="width:23px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"><img src="messg.png" style="margin:8px 0 0 2px;"></div>
<div id="m1" style="width:246px; height:28px; background:#DADDE2; float:left"></div>
<div id="m2" style="width:145px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m3" style="width:101px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m4" style="width:70px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m5" style="width:99px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m6" style="width:84px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m7" style="width:23px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"><img src="messg.png" style="margin:8px 0 0 2px;"></div>
<div id="m1" style="width:246px; height:28px; background:#F9F9F9; float:left"></div>
<div id="m2" style="width:145px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m3" style="width:101px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m4" style="width:70px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m5" style="width:99px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m6" style="width:84px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"></div>
<div id="m7" style="width:23px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;"><img src="messg.png" style="margin:8px 0 0 2px;"></div>
<div id="m1" style="width:246px; height:28px; background:#DADDE2; float:left"></div>
<div id="m2" style="width:145px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m3" style="width:101px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m4" style="width:70px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m5" style="width:99px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m6" style="width:84px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"></div>
<div id="m7" style="width:23px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;"><img src="messg.png" style="margin:8px 0 0 2px;"></div>
</div>
答案 0 :(得分:4)
您应该使用HEREDOC
语法。您可以复制整个HTML内容并将其传递到HEREDOC
内。您的变量也将被解析。
<?php
$str = <<<EOD
Example <b>of</b> string
spanning multiple lines
using heredoc <b>syntax</b>.
EOD;
echo $str;
<强> OUTPUT :
强>
答案 1 :(得分:1)
没有heredoc你不能在你的字符串中使用<?php
....
它应该像
$msg='<div id="new2" style="background:#fff; width:779px; min-height:310px;">
'.$another_variable_here_for_example.'<table align="center" width="779px" cellpadding="0" cellspacing="0" id="tb1">...';
如果您需要添加更多代码(就像您在第一个<?php
之后所做的那样)
结束$ msg ='...';变量,稍后通过$ msg。='...';
答案 2 :(得分:1)
如果我没有错过任何内容:
<?php
$msg = "<div id=\"new2\" style=\"background:#fff; width:779px; min-height:310px;\">
<table align=\"center\" width=\"779px\" cellpadding=\"0\" cellspacing=\"0\" id=\"tb1\">
<tr style=\"height:28px; font-size:12px;\">
<td style=\"width:246px\">Name</td>
<td style=\"width:145px\">CITY</td>
<td style=\"width:102px\">ABCDEF</td>
<td style=\"width:71px\">BUDGET</td>
<td style=\"width:102px\">MEMBERSHIP</td>
<td style=\"width:84px\" id=\"td1\">UNTIL</td>
</tr>
</table>";
$color='#DADDE2';
$business = getbussinesspagination(0,$start,$per_page);
if ($business)
{
while ($row = $business->fetch_assoc ())
{
if($color=='#DADDE2')
$color='#F9F9F9';
else
$color='#DADDE2';
$msg .= "<div id=\"m1\" style=\"width:246px; height:28px; background:{$color}; float:left\"><p style=\"padding-left:45px; margin-top:8px;\">{$row['title']}</p></div>
<div id=\"m2\" style=\"width:145px; height:28px; float:left; margin:0 0 0 1px;background:{$color}\"><p style=\"padding-left:30px; margin-top:8px;\">{$row['cityName']}</p></div>
<div id=\"m3\" style=\"width:101px; height:28px; float:left; margin:0 0 0 1px;background:{$color}\"><p style=\"padding-left:40px; margin-top:8px;\">".($row['article']!=''?'A':'').($row['event']!=''?'E':'')."</p></div>
<div id=\"m4\" style=\"width:70px; height:28px; float:left; margin:0 0 0 1px;background:{$color}\"><p style=\"padding-left:25px; margin-top:8px;\">{$row['credit']}</p></div>
<div id=\"m5\" style=\"width:99px; height:28px; float:left; margin:0 0 0 1px;background:{$color}\"><p style=\"padding-left:25px; margin-top:8px;\">{$row['name']}</p></div>
<div id=\"m6\" style=\"width:84px; height:28px; float:left; margin:0 0 0 1px;background:{$color}\"><p style=\"padding-left:10px; margin-top:8px;\">{$row['renewal_date']}</p></div>
<div id=\"m7\" style=\"width:23px; height:28px; float:left; margin:0 0 0 1px;background:{$color}\"><img src=\"messg.png\" style=\"margin:8px 0 0 2px;\"></div> ";
}
}
$msg .= "<div id=\"m1\" style=\"width:246px; height:28px; background:#DADDE2; float:left\"></div>
<div id=\"m2\" style=\"width:145px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m3\" style=\"width:101px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m4\" style=\"width:70px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m5\" style=\"width:99px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m6\" style=\"width:84px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m7\" style=\"width:23px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"><img src=\"messg.png\" style=\"margin:8px 0 0 2px;\"></div>
<div id=\"m1\" style=\"width:246px; height:28px; background:#F9F9F9; float:left\"></div>
<div id=\"m2\" style=\"width:145px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m3\" style=\"width:101px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m4\" style=\"width:70px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m5\" style=\"width:99px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m6\" style=\"width:84px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m7\" style=\"width:23px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"><img src=\"messg.png\" style=\"margin:8px 0 0 2px;\"></div>
<div id=\"m1\" style=\"width:246px; height:28px; background:#DADDE2; float:left\"></div>
<div id=\"m2\" style=\"width:145px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m3\" style=\"width:101px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m4\" style=\"width:70px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m5\" style=\"width:99px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m6\" style=\"width:84px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m7\" style=\"width:23px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"><img src=\"messg.png\" style=\"margin:8px 0 0 2px;\"></div>
<div id=\"m1\" style=\"width:246px; height:28px; background:#F9F9F9; float:left\"></div>
<div id=\"m2\" style=\"width:145px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m3\" style=\"width:101px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m4\" style=\"width:70px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m5\" style=\"width:99px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m6\" style=\"width:84px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m7\" style=\"width:23px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"><img src=\"messg.png\" style=\"margin:8px 0 0 2px;\"></div>
<div id=\"m1\" style=\"width:246px; height:28px; background:#DADDE2; float:left\"></div>
<div id=\"m2\" style=\"width:145px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m3\" style=\"width:101px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m4\" style=\"width:70px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m5\" style=\"width:99px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m6\" style=\"width:84px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m7\" style=\"width:23px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"><img src=\"messg.png\" style=\"margin:8px 0 0 2px;\"></div>
<div id=\"m1\" style=\"width:246px; height:28px; background:#F9F9F9; float:left\"></div>
<div id=\"m2\" style=\"width:145px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m3\" style=\"width:101px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m4\" style=\"width:70px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m5\" style=\"width:99px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m6\" style=\"width:84px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m7\" style=\"width:23px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"><img src=\"messg.png\" style=\"margin:8px 0 0 2px;\"></div>
<div id=\"m1\" style=\"width:246px; height:28px; background:#DADDE2; float:left\"></div>
<div id=\"m2\" style=\"width:145px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m3\" style=\"width:101px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m4\" style=\"width:70px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m5\" style=\"width:99px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m6\" style=\"width:84px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m7\" style=\"width:23px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"><img src=\"messg.png\" style=\"margin:8px 0 0 2px;\"></div>
<div id=\"m1\" style=\"width:246px; height:28px; background:#F9F9F9; float:left\"></div>
<div id=\"m2\" style=\"width:145px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m3\" style=\"width:101px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m4\" style=\"width:70px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m5\" style=\"width:99px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m6\" style=\"width:84px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"></div>
<div id=\"m7\" style=\"width:23px; height:28px; float:left; margin:0 0 0 1px;background:#F9F9F9;\"><img src=\"messg.png\" style=\"margin:8px 0 0 2px;\"></div>
<div id=\"m1\" style=\"width:246px; height:28px; background:#DADDE2; float:left\"></div>
<div id=\"m2\" style=\"width:145px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m3\" style=\"width:101px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m4\" style=\"width:70px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m5\" style=\"width:99px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m6\" style=\"width:84px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"></div>
<div id=\"m7\" style=\"width:23px; height:28px; float:left; margin:0 0 0 1px;background:#DADDE2;\"><img src=\"messg.png\" style=\"margin:8px 0 0 2px;\"></div>
</div>";
echo $msg;
?>
答案 3 :(得分:1)
对于PHP中的这种模板我使用ob_get_contents()
ob_end_clean();
ob_start();
include __DIR__ . '/your_template.php';
$contents = ob_get_contents();
ob_end_clean();
答案 4 :(得分:0)
或者只是使用$msg = '<p>Your HTML here</p>'
当你的html代码中有"
而不是使用'
来包装html。仅在您的代码中仅使用"
引号时才有效!
答案 5 :(得分:0)
干净的方法是使用heredoc语法,并修改你的代码,如下所示,只使用html中间的变量(无函数调用)
$color='#DADDE2';
$business=getbussinesspagination(0,$start,$per_page);
if ($business){
while ($row = $business->fetch_assoc ()){
if($color=='#DADDE2')
$color='#F9F9F9';
else
$color='#DADDE2';
}
else
{
}
$msg=<<<MYHTML
<div id="new2" style="background:#fff; width:779px; min-height:310px;">
<table align="center" width="779px" cellpadding="0" cellspacing="0" id="tb1">
<tr style="height:28px; font-size:12px;">
<td style="width:246px">Name</td>
<td style="width:145px">CITY</td>
<td style="width:102px">ABCDEF</td>
<td style="width:71px">BUDGET</td>
<td style="width:102px">MEMBERSHIP</td>
<td style="width:84px" id="td1">UNTIL</td>
</tr>
</table>
$business
MYHTML;
或者,您也可以使用ob_start();
和ob_get_content();
将输出重定向到稍后将使用的变量。
ob_start();
// do all your code, nothing will be sent to screeen
echo '<div id="...';
$html = ob_get_contents();
ob_end_clean();
var_dump($ out1,$ out2);