0

I'm messing around with some jQuery, I'm going to try to describe my issue as good as I can, as I'm a rookie. The script is just an example to show the issue I'm having.

$( "#activity" ).replaceWith("<?php ifSOMETHING {echo' <div id="hello">'}  ");

The issue is, the script uses " ", the PHP is using the ' ' for the echo, so now what can I use for the <div id="hello"> as the " " is already used by the .replaceWith("")

EDIT: The full code:

<script>
$( '#activity' ).replaceWith('
    <div id="chatwrap">
        <div id="content">
            <section class="box">
                <ul id="chat"></ul>
                    <form id="chatForm" action="">
                        <?php if($steam->loggedIn()) {
                        echo'
                            <input id="chatMessage" type="text" class="text" maxlength="100" placeholder="Message..." autocomplete="off">
                            <div id="sendmes">
                            <div id="onlinewrap"><img src="img/users.png" class="tooltip" title="Players Online" alt="players online"><div id="onl"></div></div>
                            <div id="wrap_options">
                            <a href="#" class="emotes show-pop btn btn-default top" data-title="Emotes" data-content=""><img src="img/emotes.png"></a></div>
                            <input id="chatSend" type="submit" class="grey" value="Send">';
                            }else{
                                echo'
                                    <center><a href="'.$steam->loginUrl().'" class="grey" style="width:150px;font-size:16px;line-height:30px;">Login to chat</a></center>';
                                    }?>
                    </form>
            </section>
                            </div>
        </div>
    </div>
');
</script>
3
  • 1
    I'm not 100% sure, but I believe you can use the " and ' as you want because that it's two different languages, meaning that the php part won't be visible for the jQuery, only the data you echo will be. Commented Jan 6, 2016 at 7:47
  • Correct, PHP part won't be parsed in JS side. Commented Jan 6, 2016 at 7:48
  • Oh, I'll give that a shot right now then. Commented Jan 6, 2016 at 8:03

6 Answers 6

2

No need to, php and js(jQuery) are two different languages and the php won't be parsed in the jQuery part, so you don't have to be afraid of JS interpreting the " or ' only the data php echo's will be visible for JS / jQuery

$( "#activity" ).replaceWith('<?php ifSOMETHING {echo '<div id="hello">';} ?>  ');

Is completely valid, you still have to be aware of any " ' that your PHP outputs, but not in the actual php code.

Sign up to request clarification or add additional context in comments.

6 Comments

I've tried it, it doesn't seem to be working right for me. I've pasted my full code, am I getting it right? (Sorry for the mess.)
Are you getting any errors @Codey93? Or is the output wrong?
It looks like it's not closed, whenever '); is on a new line, do i need to compress it all to 1 line for it to work?
Give me a second. @Codey93
@Codey93 Okay, it's something to do with the jQuery and perhaps the length / line breaks, but it isn't the ' / " causing it, I've tested it in my local environment and it works as it should with less data.
|
1

Try with JSON encode.

$(function(){

    $( '#activity' ).replaceWith('<div id="chatwrap"><div id="content"><section class="box"><?php echo json_encode('<ul id="chat"></ul>');?><form id="chatForm" action=""><?php if(1==1) {echo json_encode('<input id="chatMessage" type="text" class="text" maxlength="100" placeholder="Message..." autocomplete="off"><div id="sendmes"><div id="onlinewrap"><img src="img/users.png" class="tooltip" title="Players Online" alt="players online"><div id="onl"></div></div><div id="wrap_options"><a href="#" class="emotes show-pop btn btn-default top" data-title="Emotes" data-content=""><img src="img/emotes.png"></a></div><input id="chatSend" type="submit" class="grey" value="Send">');}else{echo json_encode('<center><a href="abc" class="grey" style="width:150px;font-size:16px;line-height:30px;">Login to chat</a></center>');}?>');
    });
    </script>
    <span id="activity"></span>

output: when if call

enter image description here

and when else call

enter image description here

1 Comment

Hello GuRu, thank you for taking the time to answer my question. I got help from @Epodax to fix this issue. But I learned to use the json_encode with your help. Thank you
0

Just use

$( "#activity" ).replaceWith("<?php ifSOMETHING {echo' <div id=\"hello\">'}  ");

Check out escape character for more details.

Comments

0

You can escape it, and replace JavaScript's quotes with single-quotes.

.replaceWith('<?php ifSOMETHING { echo "<div id=\"hello\">"; } ?>');

Comments

0

JSON is a subset of JavaScript literal syntax. PHP has the json_encode function to encode content as JSON. Use that to properly escape your data. It will handle ', ", new lines and anything else you might need to worry about in a string.

$( "#activity" ).replaceWith(<?php 
    if (SOMETHING) {
        echo json_encode(' <div id="hello">');
    }
?>);

Note that json_encode will output a JSON string including the quotes, so remove the " you added manually to the JavaScript.

Comments

-1

You may use backslashes

$( "#activity" ).replaceWith("<?php ifSOMETHING {echo "<div id=\"hello\">"; }  ");

I wanted to mention the usage of backslashes. But I admit that using single quotes to avoid this is a better and faster solution:

  $( "#activity" ).replaceWith("<?php ifSOMETHING {echo '<div id="hello">'; }  ");

5 Comments

"<?php ifSOMETHING {echo " /* here it will break it will asl you for + operator */ <div id=\"hello\">"; } "
No it will not, forget your editor, php will know where to start working, just copy and paste my code before putting down please.
then use 'hello' in place of \"hello\" . it will give error
A code block alone does not provide a good answer. Please add explanations (why it solve the issue, where was the mistake, etc...)
@LouisBarranqueiro I have reverted back the answer to the initial version. And added a comment, I still don't know Shubham's problem with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.