0

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

0

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I was interested in a solution for using a string that has ' and " inside, and on top of that the string would be enclosed in ' or " to be usefull in a syntax like in an echo. If you know what I mean... Sadly I did not see any solution for that, and my poor attempt using heredocs <<< did not enlighten me.
0

Add document.close(); just before the tag.

1 Comment

I solved it by removing it from php. I mean, I gave up using echo for it. I just stoped php before the HTML begins with ?> and started again after it with <?php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.