Jump to content

Escaping single quote Code Syntax Errors


nkosinathi

Recommended Posts

Can anyone please help. I have the following PHP code and it keeps on making syntax errors. I believe because i have to escape the single quote (') but escaping it results in the Javascript NewCal function passing (/) .

 

 

<?php

$tblString = '<table>             

      <td height="19">Message date</td>

 

<!—ERRORS ARE IN THE CODE BELOW---->

 

      <td valign="top"><input type="text" name="schedule_SomeFunc" class="Textbox"><a href="javascript:NewCal('date_schedule ', 'ddmmmyyyy ',false,24)"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date">[/url]</td>

Can anyone please help. I have the following PHP code and it keeps on making syntax errors. I believe because i have to escape the single quote (') but escaping it results in the Javascript NewCal function passing (/) .

 

 

<?php

$tblString = '<table>             

      <td height="19">Message date</td>

 

<!—ERRORS ARE IN THE CODE BELOW---->

 

      <td valign="top"><input type="text" name="schedule_SomeFunc" class="Textbox"><a href="javascript:NewCal('date_schedule ', 'ddmmmyyyy ',false,24)"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date">[/url]</td>

 

Have you tried it like this

 

<?php
$tblString = '<table>              
      <td height="19">Message date</td>

<!—ERRORS ARE IN THE CODE BELOW---->

      <td valign="top"><input type="text" name="schedule_SomeFunc" class="Textbox"><a href="javascript:NewCal(\'date_schedule\ ', \'ddmmmyyyy\',false,24)"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date">[/url]</td>

 

Silly question, and really just a work around, but why not just do this:

 

<?php
$tblString = "
?>
<table>              
      <td height="19">Message date</td>
      <td valign="top"><input type="text" name="schedule_SomeFunc" class="Textbox"><a href="javascript:NewCal('date_schedule ', 'ddmmmyyyy',false,24)"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date">[/url]</td>
<?php
";

 

and simply not have to worry about it? Would that not work?

Yeah, "writing" JavaScript from within PHP code can be problematic. There are several solutions:

 

1. In your original code you are defining the string using single quote marks. So, if you need to include single quote marks in the string you need to escape them using a back-slash:

$tblString = '<table>              
      <td height="19">Message date</td>
      <td valign="top">
        <input type="text" name="schedule_SomeFunc" class="Textbox">
        <a href="javascript:NewCal(\'date_schedule\', \'ddmmmyyyy\',false,24)">
        <img src="cal.gif" width="16" height="16" border="0" alt="Pick a date">
      </td>';

 

Or you could "exit" out of PHP code as the last post suggests, but I think that makes the code confusing. Or you could define the string with double quotes, but you would then need to escape any double quotes that need to be in the string:

$tblString  = "<table>\n";
$tblString .= "  <tr>\n";
$tblString .= "    <td height=\"19\">Message date</td>\n";
$tblString .= "<td valign=\"top\">";
$tblString .= "<input type=\"text\" name=\"schedule_SomeFunc\" class=\"Textbox\">";
$tblString .= "<a href=\"javascript:NewCal('date_schedule', 'ddmmmyyyy',false,24)\">";
$tblString .= "<img src=\"cal.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Pick a date\">";
$tblString .= "</td>\n";

 

And, then there is always the hredoc method of defining a string

$tblString  = <<<TBL_HTML
<table>
  <tr>
    <td height="19">Message date</td>
<td valign="top">
<input type="text" name="schedule_SomeFunc" class="Textbox">
<a href="javascript:NewCal('date_schedule', 'ddmmmyyyy',false,24)">
<img src="cal.gif" width="16" height="16" border="0" alt="Pick a date">
</td>
TBL_HTML;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.