1

In my HTML-page I have a form with action="register.php" and a <p id="slogan">. I want the PHP-code in register.php to change the value of the <p> by echoing some Javascript.

However, the paragraph is not changing in my HTML-page and I do not understand why.

This is what my simplified HTML-page contains:

<body>
  <p id="slogan"> hello </p>    

  <form action="../scripts/register.php" method="post">
    ...
  </form>
</body>

This is what my simplified register.php contains:

<?php
...
if (mysqli_query($conn, $sql)) {
    header("Location: http://www.google.com");
    echo "<script>
        document.getElementById('slogan').innerHTML = 'Character successfully introduced.';
        </script>";
    sleep(3);
} 
?>

The echoed JavaScript is supposed to change "hello" to "Character successfully created.".

The reason behind sleep(3) is to wait three seconds so that you have time to notice the updated paragraph before getting redirected to Google.

RaminS
  • 2,208
  • 4
  • 22
  • 30
  • The JavaScript should be available to the page you are trying to redirect to. I would use external JavaScript from that page, instead of use PHP to create HTML. – StackSlave Nov 25 '15 at 00:29
  • I'm not trying to use the JavaScript on the page that I'm redirecting to. I'm using it on the page that had the form in it. This is the reason I have sleep(3) - so you can see the change for three seconds before getting redirected. – RaminS Nov 25 '15 at 00:30
  • `if(blahbla) {` is JavaScript, you should know that JavaScript on the Client cannot create PHP like that. – StackSlave Nov 25 '15 at 00:32
  • @PHPglue that's the issue, it's a little ambiguous - I thought it was PHP! – ScottMcGready Nov 25 '15 at 00:35
  • PHP has conditions too. "blabla" is actually "mysqli_query($conn, $sql)". Although this seems irrelevant, since the other two statements work. It's just the echo-statement that doesn't do anything. – RaminS Nov 25 '15 at 00:36
  • @Gendarme the echo statement might be working but you're redirecting the client before it gets a chance to go. Have a read at my answer and try that instead of this. You'll have far less problems if you do! – ScottMcGready Nov 25 '15 at 00:40
  • Are you saying that the sleep(3) halts the execution of the JavaScript? – RaminS Nov 25 '15 at 00:53
  • No, the `header()` executes first, redirects before `sleep()` has a chance to kick in. ***You can't redirect to another page using `header()` AND output stuff to the screen*** – ScottMcGready Nov 25 '15 at 00:55
  • 1
    @Gendarme, when you call a header(location) everything that runs on php after that is unknown to the browser that is loading the page, because after this header call php stops sending outputs to the page. So even if sleep gets executed the browser won't know that, because an redirection header was triggered. – Jacobson Nov 25 '15 at 01:17
  • So after the declaration of header() you can't use echo? If that is the case I will mark this as solved. – RaminS Nov 25 '15 at 01:22
  • @Gendarme you're kinda missing the point. Basically yes that's correct but there's a whole load of other issues with your code that mean your answer doesn't actually answer your own question. – ScottMcGready Nov 25 '15 at 01:47
  • I'll just use the header to redirect back to the previous page and store the success-message as a session variable instead and do the message output there instead. Now that I think of it, I think that's even close to what you suggested. What is it that I am missing? – RaminS Nov 25 '15 at 01:53

4 Answers4

2

This is a bad implementation in a couple of ways (hear me out).

Firstly I'd suggest not injecting javascript to the page. Why? Because in order to get that javascript to show, you're relying on two factors:

  1. Javascript to be enabled on the client's browser.
  2. PHP to sleep while that occurs.

It might seem like two tiny points but every time you send PHP to sleep that is effectively a blocker - while that happens, nothing else.

Also, slight flaw in your code if I've picked up your theory correctly - it seems you want to inject a success message in the "main script" page rather than the intermediary register.php page. If that's the case, it'll never get executed. If I've picked you up wrongly, it's worth adding more of your code to the question to clarify what exactly is going on.

Alternative approach

My suggestion would be to do something like the following:

register.php

if($your_var == 1) {
  header('Location: youroriginalscript.php');
  $_SESSION['yoursessionvar'] = 'Character successfully created.';
}

youroriginalscript.php

... (beside your slogan HTML entity) ...
<div id="slogan">
    <?php
    if(isset($_SESSION['yoursessionvar'])){
        echo $_SESSION;
    }
    ?>
</div>

This is by no means perfect but it should give you an idea to get started.

Your original script also assumes that the character creation is always successful. This might not be the case and should be double checked before giving clients misleading feedback. Check it, make sure it's correct, and never assume it stays the same between page hops!

Tip: If you ever try and get PHP to sleep or do some crazy stuff, it'll always, always, always create a bottleneck.

Edit

Okay, from your edited question it seems you're getting PHP/Javascript mixed up a little bit. Here's another answer I wrote a while back explaining the difference but there are literally millions of others out there:

Community
  • 1
  • 1
ScottMcGready
  • 1,612
  • 2
  • 24
  • 33
  • Answer to your first two points: 1) If they don't have JavaScript enabled, my whole page is useless anyways, since I have a JS-stylesheet instead of CSS. 2) I don't understand this. The only reason I wrote sleep(3) is so that they can see the success message for three seconds before being redirected to the next page. I don't know what you mean by 'main script'. If it is to any clarification, the header is really not that important. It just redirects to another html-file after everything is done. The only script I have is the register.php. – RaminS Nov 25 '15 at 00:46
  • Okay to clarify, the "main script" I speak of is the one with your `
    ` on it. You can't inject js into another page using PHP without using some funky AJAX but that would rely on your "main script" being a PHP page...
    – ScottMcGready Nov 25 '15 at 00:48
  • My
    resides in a simple HTML file that uses no PHP at all. The only script I use there is an external JS-file for styling.
    – RaminS Nov 25 '15 at 00:50
  • Okay... we're arguing over semantics here. I call it a script, you call it an HTML file... there's no difference. Have a good read at what I've written (and others too) and compare that with your code. If you update your question with more of your code, we can start to give more detailed analysis. – ScottMcGready Nov 25 '15 at 00:51
0

With

header("Location: someURL");

you are redirecting to another page (and very likely throwing an error because "someURL" might not exist) before the script is actually sent to the browser.

Jeff
  • 6,895
  • 1
  • 15
  • 33
  • The sleep(3); gets executed though. And the header works. It's just that the JavaScript doesn't get executed. I am not exactly sure what you are saying. – RaminS Nov 25 '15 at 00:28
  • 1
    How do you confirm that `sleep(3)` is being executed? – StackSlave Nov 25 '15 at 00:31
  • The page takes three seconds to redirect to the someURL, instead of doing it instantly. – RaminS Nov 25 '15 at 00:31
  • 1
    Probably because of the page it's redirecting to, not the `sleep()` function. – StackSlave Nov 25 '15 at 00:37
  • Without the sleep(3) it redirects instantly. – RaminS Nov 25 '15 at 00:55
  • Sleep is a blocker (as explained in my answer...) and the JS is never executing because there is no HTML document on the PHP page to append to. Besides, PHP runs *first*, then sent to the browser to parse, hence your JS will never execute. If it does, it's by a sheer fluke – ScottMcGready Nov 25 '15 at 01:48
0

The echo statement is being buffered. You have to configure your script to avoid buffering. Take a look at this: How to disable output buffering in PHP

Try this:

header("Location: someURL");

@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++)
ob_end_flush();
ob_implicit_flush(1);

echo "<script>
      document.getElementById('slogan').innerHTML = 'Character successfully created.';
    </script>";
sleep(3);
Community
  • 1
  • 1
Jacobson
  • 674
  • 5
  • 10
  • This still won't work as the slogan HTML element exists on the first page, where the `
    ` resides. Therefore without the HTML present on the page it won't work. Even though this'd work with a few sledgehammer style tweaks that Peter Gabriel himself would be proud of, I'd stay well clear of calling `sleep()` even if you think you really, really, really have to
    – ScottMcGready Nov 25 '15 at 00:41
  • 1
    I doubt even the sleep(3) is being executed. But if he's saying it executes, so the solution is not so crazy as this. :) – Jacobson Nov 25 '15 at 00:46
  • 1
    Yeah, I feel the same - I doubt sleep is executed at all in this case! **Edit:** they've clarified it's a JS based web app which could take 3 seconds to load easily – ScottMcGready Nov 25 '15 at 00:47
-1

The problem is that since PHP is back-end and JavaScript is front-end, the whole PHP-script has to finish before any JavaScript is executed. This means that the sleep(3) happens before the execution of the JavaScript and totally kills its purpose. All it does is to waste three seconds of your time.

If you want to output a message and also redirect, you need to come up with other ways of doing it. However, that is another topic.

RaminS
  • 2,208
  • 4
  • 22
  • 30