RaminS
RaminS

Reputation: 2239

JavaScript in PHP-echo is not noticeable

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.

Upvotes: 1

Views: 133

Answers (4)

RaminS
RaminS

Reputation: 2239

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.

Upvotes: -1

ScottMcGready
ScottMcGready

Reputation: 1622

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:

Upvotes: 2

Jacobson
Jacobson

Reputation: 683

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);

Upvotes: 0

Jeff
Jeff

Reputation: 6953

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.

Upvotes: 0

Related Questions