简体   繁体   中英

How to solve double quote inside double quote syntax error

I have a project where I have a PHP code, inside this I have to echo HTML which has JavaScript onclick function() which only accepts double-quotes. The problem is the echo uses double-quote as well so there's a syntax error in my code and I'm new to PHP so I don't know how to solve this.

Here is the code:

echo "
          <span class = 'username'>".$row['username']."</span>
          <form name = 'matchcreator' class='amount' action='arena.php' method ='post'>
          <input  name = 'm-maker' type = 'number' class='price'  min='5' max='100' value='5'/>
          <div class = 'review'>
          <p>REVIEW</p>
          </div>
          <button id ='send' type = 'button' onclick="Confirm.render('yes','no')">Send Challenge</button>
          </form>
           ";               
    echo "<br>";

I know this is a syntax error I just don't know how to solve it. Any help would be appreciated.

echo "
          <span class = 'username'>".$row['username']."</span>
          <form name = 'matchcreator' class='amount' action='arena.php' method ='post'>
          <input  name = 'm-maker' type = 'number' class='price'  min='5' max='100' value='5'/>
          <div class = 'review'>
          <p>REVIEW</p>
          </div>
          <button id ='send' type = 'button' onclick=\"Confirm.render('yes','no')\">Send Challenge</button>
          </form>
           ";               
    echo "<br>";
}

Simply by escaping with \\"

You simply have to escape it, using the \\ operator. This tells PHP that is is not to be treated as a quote in PHP, but as a character to be treated as part of the string instead. An example

echo "<input name=\"send\" />";

would output

<input name="send" />

So in your particular case, onclick="Confirm.render('yes','no')" would need its double-quotes escaped, like this: onclick=\\"Confirm.render('yes','no')\\"

You can also just exit PHP while parsing HTML and echo the particular PHP elements, it makes for easier and cleaner code

<?php /*begin PHP here, and exit it */?>
<span class="username"><?php echo $row['username']; ?></span>
<form name "matchcreator" ><!-- rest of form -->
<?php // continue PHP...
echo "
              <span class = 'username'>".$row['username']."</span>
              <form name = 'matchcreator' class='amount'              action='arena.php' method ='post'>
              <input  name = 'm-maker' type = 'number' class='price'  min='5' max='100' value='5'/>
              <div class = 'review'>
              <p>REVIEW</p>
              </div>
              <button id ='send' type = 'button' onclick=\"Confirm.render('yes','no')\">Send Challenge</button>
              </form>
               ";               
        echo "<br>";

Just escape double quote with backslash \\

如下所示放置onclick=\\"Confirm.render('yes','no')\\"

<button id ='send' type = 'button' onclick=\"Confirm.render('yes','no')\">Send Challenge</button>

Scape the double quotes with a back slash.

echo "
      <span class = 'username'>".$row['username']."</span>
      <form name = 'matchcreator' class='amount' action='arena.php' method ='post'>
      <input  name = 'm-maker' type = 'number' class='price'  min='5' max='100' value='5'/>
      <div class = 'review'>
      <p>REVIEW</p>
      </div>
      <button id ='send' type = 'button' onclick=\"Confirm.render('yes','no')\">Send Challenge</button>
      </form>
       ";               
echo "<br>";

This works for me.

Use it Like this:

echo '<span class = "username">'.$row['username'].'</span>
                  <form name = "matchcreator" class="amount"  action="arena.php" method ="post">
                  <input  name = "m-maker" type = "number" class="price"  min="5" max="100" value="5" />
                  <div class = "review">
                  <p>REVIEW</p>
                  </div>
                  <button id ="send" type = "button" onclick="Confirm.render(yes,no)">Send Challenge</button>
                  </form>
                   ';               
            echo '<br>';

Here is your corrected simpler clean code clean code . Now all you need is to change the confirm dialog buttons with (yes and no).

?>
<span class = 'username'><?php echo $row['username']; ?></span>
<form name = 'matchcreator' class='amount' action='arena.php' method='post'>
<input  name = 'm-maker' type = 'number' class='price'  min='5' max='100' value='5'/>
<div class = 'review'>
    <p>
        REVIEW
    </p>
</div>
<button id ='send' type = 'button' onclick="return confirm('are you sure?')">Send Challenge</button>
</form>
<br>

<?php

//rest of the code

?>

hope this helps.

You can also do like this : Just write your code in {}

echo     "<span class = 'username'>{$row['username']}</span>
          <form name = 'matchcreator' class='amount' action='arena.php' method ='post'>
          <input  name = 'm-maker' type = 'number' class='price'  min='5' max='100' value='5'/>
          <div class = 'review'>
          <p>REVIEW</p>
          </div>
          <button id ='send' type = 'button' onclick={Confirm.render('yes','no')}>Send Challenge</button>
          </form> <br>";    
?>

Try this:-

echo '<span class = "username">'.$row['username'].'</span>
          <form name = "matchcreator" class="amount" action="arena.php" method ="post">
          <input  name = "m-maker" type = "number" class="price"  min="5" max="100" value="5"/>
          <div class = "review">
          <p>REVIEW</p>
          </div>
          <button id ="send" type = "button" onclick="Confirm.render('."yes".','."no".')">Send Challenge</button>
          </form>';            
    echo "<br>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM