繁体   English   中英

PHP在echo中转义复杂字符串

[英]PHP escaping complex string in echo

我是这个php + HTML5 + jquery移动设备的新手,它试图用mysql连接构建Web应用程序。

一路上的某个地方...我使用一个php文件来“生成”一个包含3个选择框的HTML文件,用户必须在其中选择一个月中的某一天才能继续。 我使用javascript + HTML设计了目标页面,一切都很顺利,然后在php文件中回显了整个页面(我调用+处理某些内容以接收该页面)。 我遇到的问题是转义我想的字符串。 我的意思是,如果我注释这些行,则该页面有效(不是应该的,但是至少显示了该页面)。 如果我不对它们发表评论,则jQuery将显示一条警告,提示“错误加载页面”

可以正常工作的html是:

<html>
<head>
<title>Select</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js" />
</head>
<body>
<span id="day-select">
<select id="day">
<script type="text/javascript">
  function LastDayOfMonth(Year, Month) {
    return new Date( (new Date(Year, Month+1,1))-1 );
  }            
var nextmonth = new Date();
nextmonth.setHours(0, 0, 0, 0);
nextmonth.setMonth( nextmonth.getMonth() + 1 );
var nextziua = nextmonth.getDate();
var ultimazidt = LastDayOfMonth(nextmonth.getFullYear(), nextmonth.getMonth());
var ultimazi = ultimazidt.getDate();
var ziuaselect = nextziua;
var count = ultimazi;
var ziua=1;
while(ziua <= count){
   if(ziua == nextziua){
     document.write('<option value="'+ziua+'" selected="selected">'+ziua+'</option>');
   }
   else{
     document.write('<option value="'+ziua+'">'+ziua+'</option>');
   }
ziua++;
}
</script>
</select>
</span>
<br><br>
</body>
</html>

因此,上面的html文件...可以正常工作,但是当我从PHP文件中的回显中尝试运行时,所有内容都在这一行下地狱:

document.write('<option value="'+ziua+'" selected="selected">'+ziua+'</option>');

我的意思是:

echo '  <html>';
echo '  <head>';
echo '  <title>Select</title>';
echo '  <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>';
echo '  </head>';
echo '  <body>';
echo '  <span id="day-select">';
echo '  <select id="day">';
echo '  <script type="text/javascript">';
echo '  function LastDayOfMonth(Year, Month) {';
echo '  return new Date( (new Date(Year, Month+1,1))-1 );';
echo '  }            ';
echo '  var nextmonth = new Date();';
echo '  nextmonth.setHours(0, 0, 0, 0);';
echo '  nextmonth.setMonth( nextmonth.getMonth() + 1 );';
echo '  var nextziua = nextmonth.getDate();';
echo '  var ultimazidt = LastDayOfMonth(nextmonth.getFullYear(), nextmonth.getMonth());';
echo '  var ultimazi = ultimazidt.getDate();';
echo '  var ziuaselect = nextziua;';
echo '  var count = ultimazi;';
echo '  var ziua=1;';
echo '  while(ziua <= count){';
echo '  if(ziua == nextziua){';
echo <<<EOT
document.write('<option value="'+ziua+'" selected="selected">'+ziua+'</option>');
EOT;
echo '  }';
echo '  else{';
echo <<<EOT
document.write('<option value="'+ziua+'">'+ziua+'</option>');
EOT;
echo '  }';
echo '  ziua++;';
echo '  }';
echo '  </script>';
echo '  </select>';
echo '  </span>';
echo '  <br><br>';
echo '  </body>';
echo '  </html>';

因此,此代码将生成SELECT框的内容,具体日期取决于月份,范围为1-30、1-31或1-29。 它还使当前日期为“选定”。 再次...直接用HTML进行操作时一切正常,但是当我尝试回显它时...一切都停止了。 因为那个文件写的。 我尝试转义单引号或转义双引号。 结果相同。 这就是为什么我采用了heredocs方法。 但是似乎它对我的问题仍然没有用。 我注意了Heredocs语法:-之后没有空格

<<<EOT
  • 最终EOT后无空格;

  • 最后一个(EOT;)在换行符上,仍然没有任何效果。 谁能告诉我我在做什么错? 任何帮助将不胜感激。 谢谢

我看不到此“转义转义”文本的解决方案。 另外,我将HTML和PHP分开了(似乎是一种广为人知和推荐的做法)。 所以我的代码看起来像:

<?php
 $idviz = $_POST['idvisit'];
 // other php code
?>
<html>
<head>
....
</head>
<body
 <!-- and now the html code for the page, and whenever I need parts/variables from php I just insert it in the middle of the HTML code using <?php $idviz; ?>  or other variables-->
</body>
</html>

添加document.close(); 在标签之前。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM