nkosinathi Posted March 30, 2010 Share Posted March 30, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/196973-escaping-single-quote-code-syntax-errors/ Share on other sites More sharing options...
the182guy Posted March 30, 2010 Share Posted March 30, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/196973-escaping-single-quote-code-syntax-errors/#findComment-1034070 Share on other sites More sharing options...
nkosinathi Posted March 30, 2010 Author Share Posted March 30, 2010 Yes i tried that and what happens is that the Javascript function elements are passed as 'date' instead as date_schedule instead of 'date_schedule' Quote Link to comment https://forums.phpfreaks.com/topic/196973-escaping-single-quote-code-syntax-errors/#findComment-1034332 Share on other sites More sharing options...
Jax2 Posted March 30, 2010 Share Posted March 30, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/196973-escaping-single-quote-code-syntax-errors/#findComment-1034338 Share on other sites More sharing options...
Psycho Posted March 30, 2010 Share Posted March 30, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/196973-escaping-single-quote-code-syntax-errors/#findComment-1034342 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.