duanci19881003 2013-06-30 22:37
浏览 69
已采纳

PHP在echo中转义复杂的字符串

I am new at this php+HTML5+jquery mobile trying to build a web app with mysql connection.

Somewhere along the way... I use a php file to "generate" a HTML file containing 3 select boxes, where the user will have to choose a day of the month in order to continue. I designed the destination page using javascript + HTML all went well, then I echoed the entire page in a php file (that I call+process somethings in order to receive the page). The problem I have is with escaping a string I suppose. I mean, if I comment these lines, the page works (not as it should, but at least the page gets shown). If I do not comment them, the jquery shows an alert saying "Error loading page"

The html that works just fine is:

<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>

So, the above html file... works just fine, but when I try it from an echo in a PHP file, all goes to hell right at this line:

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

I mean:

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>';

So this code is generating the contents of a SELECT box, with day numbers 1-30, 1-31 or 1-29 depending on the month. Also it makes "selected" the current day. Again... all goes fine when doing it directly in HTML, but when I try to echo it... everything stops. because of that document write. I tried escaping single quotes or escaping double quotes. Same result. That's why I adopted the heredocs method. But as it seems it is still useless for my problem. I paid attention to the heredocs syntax: - no spaces after

<<<EOT
  • no spaces after final EOT;

  • The last one (EOT;) is on a new line Still, nothing works. Can anyone tell me what am I doing wrong? Any help will be appreciated. Thank you

  • 写回答

2条回答 默认 最新

  • drzfz9995 2013-07-01 14:27
    关注

    I do not see a solution for this "escaping escaped" text. Also, I separated the HTML from PHP (seems to be a widely known and recommended practice). So my code looks like:

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?