开发者

Quotation Marks Causing Error on Page

I really hope someone can help me. I've been banging my head against the wall with this one :(.

I have the following code that is driving me nuts

In my PHP file I have entered the following code to display a series of A-Z Links:

<div>
 <ul id="AZList">
     <li><a id="LetterLink1" href="javascript:showonlyone('LetterList1');">#</a></li>
  <?php 



   $lettercounter=1;
   for ($i=65; $i<=90; $i++){

      $lettercounter=$lettercounter+1;
      $LetterLinkLbL="ListLetter".$lettercounter;
?>

          <li><a id="LetterLink<?php echo $lettercounter ?>" href="javascript:showonlyone('<?php echo $LetterLinkLbL ?>');"><?php echo chr($i) ?><开发者_C百科;/a></li>         



  <?php } ?>
    </ul>

</div>

But when I view the source file it displays as

  <li><a id="LetterLink1"  href="javascript:showonlyone("LetterList1');">#</a></li>

      <li><a id="LetterLink2"  href="javascript:showonlyone("ListLetter2');">A</a></li>         




      <li><a id="LetterLink3"  href="javascript:showonlyone("ListLetter3');">B</a></li>

Which is causing an error on the page

The first one should read:

 <li><a id="LetterLink1"  href="javascript:showonlyone('LetterList1');">#</a></li>

Does anyone know how I can get the quotes to display correctly?


I've just uploaded the file from my laptop to the Remote server and it is working as it should without me making any changes to the file.

I did confirm that I was working on and viewing the same file, by writing an echo statement to the page.

Why would it be showing like this on my localhost? Does anyone have an idea what he issue could be?

Thank you for all your comments by the way


There is discrepancy between what you have in your code and the ouput should give you :

Your code :

href="javascript:showonlyone('<?php echo $LetterLinkLbL ?>');"

is bound to render :

href="javascript:showonlyone('ListLetter3');"

but you state it renders (with a double quotation mark at the start and a single one at the end) :

href="javascript:showonlyone("ListLetter3');"

The php code you give should not create an error at all as it is correct. Are you sure you did not give us the output from another trial ?

The rule to insert a quotation inside a PHP/javascript string using that same quotation to enclose it is simply to add a \ before it :

$Value = "Hello Johnny \"PHP\" Boy !!";

or

$Value = 'Hello Johnny \'PHP\' boy !';


Why don't you check out functions like htmlspecialentities(), which will escape and convert these ambiguous characters. also do check your text editor's charset encoding and escape quotes if necessary by applying preceding backslashes() to the single quote.

if you're escaping quotes, this will also work in PHP:

href="javascript:showonlyone(\"<?php echo $LetterLinkLbL ?>\");">

btw as an aside: <?php echo ... ?> can be written in shorthand and reduced to <?= ... ?> without even the ending semicolon(;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜