#How do I nest properly with ', ", \' ...

111 messages · Page 1 of 1 (latest)

wet fractal
#

So this is a bit difficult to explain.. What is the correct order in which to put ' " " and so on?

Here is an example:
document.getElementById.("Test1").innerHTML = '<div onclick="alert(\'Test\'); document.getElementById(\'Test2\').innerHTML = ...></div>

Theres the ', then the " in onclick="...", then the \' in getElementById(\'Test2\').

Now how do I continue if I want to insert more code like this in ...? I hope I could explain this correctly... I already asked ChatGPT but it's solution didn't work.

Why do I need this? Because I want to echo with php a bit of HTML with javascript into my document at one time and because echoing <script> tags into the HTML doesn't work, I need to write the code directly into the onclick="" function of my <div>

mossy igloo
#

don't use innerHTML and onclick. will solve most of your problems

#

echoing HTML/JS from PHP is essentially the same problem, too

#

have the PHP-generated page load an external script. in that script, use createElement(), append(), event delegation, etc.

wet fractal
#

But I don't know how else to do it 😦 When I echoed a <script> tag into my HTML it appeared in inspect element but i didn't do anything

#

wait i'll show you the script if that's ok

#

`echo '<div class="link" onclick="

var command = prompt(\'Befehl eingeben\');
if (command == \'Logs\') {
    var key = prompt(\'Wie lautet der RSA 4096-Bit Private Key?\');
    bananaTree(key);
    document.getElementById(\'TreeOut\').innerHTML = \'<div id=\\\'TreeOutLayer\\\'></div>\';
}
else if (command == \'Clear Log\') {
    treeZurücksetzen(\'KlimkeTimpTg1f-b2zDh-3uQ4c-nI5oLa-6vYsGj-7hK8Fb-sH9tEw-0kVuNx-iZ1Py-lM2vJa\');
    alert(\'Du hast das Protokoll gelöscht.\');
}
else {
    alert(\'Ungültiger Befehl.\');

}

"><h3>Konsole</h3><div class="bottom">Befehle eingeben</div></div>';`
short lotusBOT
#

See #faq to learn how to properly share code on Discord: #faq message

mossy igloo
#

```js
code here
```

#

or I suppose
```php
code here
```

#

also, never use var

wet fractal
#
echo '<div class="link" onclick="
    
    var command = prompt(\'Befehl eingeben\');
    if (command == \'Logs\') {
        var key = prompt(\'Wie lautet der RSA 4096-Bit Private Key?\');
        bananaTree(key);
        document.getElementById(\'TreeOut\').innerHTML = \'<div id=\\\'TreeOutLayer\\\'></div>\';
    }
    else if (command == \'Clear Log\') {
        treeZurücksetzen(\'KlimkeTimpTg1f-b2zDh-3uQ4c-nI5oLa-6vYsGj-7hK8Fb-sH9tEw-0kVuNx-iZ1Py-lM2vJa\');
        alert(\'Du hast das Protokoll gelöscht.\');
    }
    else {
        alert(\'Ungültiger Befehl.\');

    }
    
    "><h3>Konsole</h3><div class="bottom">Befehle eingeben</div></div>';



    }
mossy igloo
#

oh, that doesn't help much since it's all a string 🤡

wet fractal
#

And it all has to be one singe string 😭 so thats why I need to know how to properly do the nesting stuff

mossy igloo
#

you don't. do what I suggested instead

wet fractal
#

Okay thank you

mossy igloo
#
<script defer src="myscript.js">
#

put all JS in myscript.js

wet fractal
#

Oh but I can't have it be in my HTML

mossy igloo
#

use addEventListener()

mossy igloo
wet fractal
#

It has to come from the server if I give it the correct password

#

The script that I just showed you should only appear for special people

mossy igloo
#

that's fine?

#

(ideally you write the script so it doesn't matter if they're logged in or not, but do whatever you want)

wet fractal
#

Well the script should only appear if you're logged in with the admin account so.. it has to be echoed inside with php right?

mossy igloo
#

the script tag, yes

#

however, like I just said, that shouldn't really be necessary

sour skiff
#

his problem is that he doesn't want the script to be accessible to unauthorized people I think

mossy igloo
#

security through obscurity is not security

#

why does the script contain sensitive data?

wet fractal
# mossy igloo the script tag, yes

oh and adding <script> with .innerHTML to the HTML somehow doesn't work, so if I hat the script document.getElementById("ID").innerHTML = "<script>function hey() { alert(Hey) };</script>" it wouldn't work

mossy igloo
#

never use innerHTML

#

that's not what I've suggested

wet fractal
mossy igloo
#

PHP echo is very different

mossy igloo
#

whether through fetch() or just embedded in the HTML

#

hidden input?

wet fractal
#
    $.ajax({
        type: "POST",
        url: "../php/test", // Hier den Pfad zu deinem PHP-Skript angeben
        data: { privateKey: privateKey },
        success: function(response) {
            // Hier kannst du den Erfolg der Anfrage behandeln
            document.getElementById("Test").appendChild(response);
        },
    });`
mossy igloo
#
<?
if (userLoggedIn) {
  echo '<input type="hidden" name="loginkey" value="KlimkeTimpTg1f-b2zDh-3uQ4c-nI5oLa-6vYsGj-7hK8Fb-sH9tEw-0kVuNx-iZ1Py-lM2vJa">'
  echo '<script defer src="myscript.js"></script>'
}
?>
#

omg don't use jQuery either

wet fractal
#

so now I don't have any idea on how to do it

mossy igloo
#

do it exactly as I've shown?

wet fractal
#

but what does it do?

#

or how do I add it

wet fractal
mossy igloo
#

not sure which part you're confused about. that snippet goes in your PHP then you move the JS to myscript.js and fix it so it doesn't contain sensitive data

#

no... stop using innerHTML damnit

short lotusBOT
#

See #faq to learn how to properly share code on Discord: #faq message

mossy igloo
#

put the whole PHP script somewhere for us to see

wet fractal
#

uhh wait a moment i'm not that fast in thinking..

wet fractal
mossy igloo
#

yes

wet fractal
#
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include('sql.php');
include('baum.php');

$user_login = $_POST["user_login"];

customLinks($user_login);

function customLinks($user_login) {

    if ($user_login == "KlimkeTimpTg1f-b2zDh-3uQ4c-nI5oLa-6vYsGj-7hK8Fb-sH9tEw-0kVuNx-iZ1Py-lM2vJa") {


    echo '<div class="link" onclick="
    
    var command = prompt(\'Befehl eingeben\');
    if (command == \'Logs\') {
        var key = prompt(\'Wie lautet der RSA 4096-Bit Private Key?\');
        bananaTree(key);
        document.getElementById(\'TreeOut\').innerHTML = \'<div id=\\\'TreeOutLayer\\\'>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</div>\';
    }
    else if (command == \'Clear Log\') {
        treeZurücksetzen(\'KlimkeTimpTg1f-b2zDh-3uQ4c-nI5oLa-6vYsGj-7hK8Fb-sH9tEw-0kVuNx-iZ1Py-lM2vJa\');
        alert(\'Du hast das Protokoll gelöscht.\');
    }
    else {
        alert(\'Ungültiger Befehl.\');

    }
    
    "><h3>Konsole</h3><div class="bottom">Befehle eingeben</div></div>';



    }

}
#

So there where the XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX is where I want to put this code:

<select id="filter" style="max-width: 300px; margin-left: 0;" onchange="


    var alles = document.querySelectorAll(\'.filterObjects\');
    alles.forEach(function(element) {
        element.style.display = \'none\';
    });

    var alles = document.querySelectorAll(document.getElementById(\'filter\').value);
    alles.forEach(function(element) {
        element.style.display = \'block\';
    });
    

">
    <option value="div.filterObjects">Alle Einblenden</option>
    <option value="div.LGIN">:bust_in_silhouette: Anmeldungen</option>
    <option value="div.ZTAT, div.FAVZ, div.DELZ, div.DLAZ">:envelope: Zitate</option>
    <option value="div.STRY, div.FAVS, div.DELS, div.DLAS">:envelope: Storys</option>
    <option value="div.UMFR, div.UAFV, div.UADL, div.UAAB">:bar_chart: Umfragen</option>
    <option value="div.RANK">:bar_chart: Rankings</option>
    <option value="div.ABST">:bar_chart: Abstimmungen</option>
</select>

And here is the JavaScript that innerHTML it into the HTML:

wait i can't find it right now it's such a folder mess... i'll look for it now

mossy igloo
#

please see the share link above and put the entire PHP file somewhere

wet fractal
#

oh ok

mossy igloo
#

wait i can't find it right now it's such a folder mess... i'll look for it now
this is part of your problem 😉

wet fractal
mossy igloo
#

that's fine

wet fractal
#

because there also php

mossy igloo
#

drop it all in the HTML section

wet fractal
#

ok ok

sour skiff
#

would it be fair to interpret that you simply want to know how todo session tokens with php?

wet fractal
mossy igloo
#

wow that's a lot of scripts

wet fractal
#

In line 80 is the ajax stuff

mossy igloo
#

what's with the random PHP block after all the rest of the HTML?

wet fractal
mossy igloo
#

but what is it? how does it relate to the HTML?

#

is that customLinks.php?

wet fractal
#

It generetes the stuff that should go into the HTML with insertHTML with ajax in line 80

wet fractal
mossy igloo
#

so, that's your easiest fix. stop having that return HTML. have it return JSON containing the data you use to build the HTML and use JS to add the HTML

wet fractal
#

okay.. never heard of JSON bevor but uhh maybe I'll be able to programm it with chat gpt

sour skiff
#

are you following a tutorial?

wet fractal
#

no xD

#

I'm just trying to somehow make a website from scratch

sour skiff
#

lookup a tutorial about how to do "sessions" between your server-side php and your client-side js

#

they'll give you the standard "easy" way todo it

mossy igloo
#

is that not what his user_login: getCookie("login") is doing with the fetch?

wet fractal
#

the way i do sessions is i have a cookie with my login data and then every time i open a html file i post it to php and then my php innerHTMLs the user specific stuff back into the html

mossy igloo
#

sounds highly unsecure

wet fractal
#

i dont know anything about security 😂

mossy igloo
#

anything can read that cookie

shy kernel
wet fractal
mossy igloo
#

he's using jQuery .ajax(). dunno if that includes credentials by default

wet fractal
#

accept for the problem now with the echo stuff

#

but the sign in stuff work great and this only supposed to be for our "abitur" (its a german word) stuff

mossy igloo
#

to do what you're asking, you just need to escape all the single quotes properly. that said, you really shouldn't be doing what you're asking. to do it right, you stop using innerHTML

wet fractal
#

oh okay okay

#

thanks

sour skiff
#

I mean it would probably work for just this one homework. But yeah what Jakob said, you are trying to "recreate" this whole cookie thing when its already handled for you on a lower level.

wet fractal
#

all of my 20 php and 20js files for this do it with ajax, innerhtml and cookies already 💀

#

i think the easiest fastest way is to somehow work arround it.. or figure it out with the ' " and ' \” but then for my next project i'll check our session stuff and json

sour skiff
#

here's what I would propose, look up a more modern tutorial about php sessions and fetch(), understand it as good as possible and then start from scratch.

wet fractal
#

aint no way i start from scratch xD

#

but for my next project in the future

sour skiff
#

suit yourself^^

wet fractal
#

😂

mossy igloo
#

use backticks and double quotes for everything JS and single quotes for PHP. should work around your issue

sour skiff
shy kernel
#

but why