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>

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.