0

I am having a problem with how to echo javascript in php. I have a form which on submit will execute itself and echo some text and will redirect to a page in 5secs. I am currently echoing this:

header("Refresh: 5;url=index2.php?ID=".$objResult["ID"]."");

echo '<html>';
echo '<head>';
echo '<title>Klant toevoegen</title>';
echo '<link rel="stylesheet" href="style.css" type="text/css" media="screen" />';
echo '</head>';
echo '<body>';
echo '<fieldset>';
echo ''.$Naam.' is added to the database, u will be redirected in a couple of seconds.<br><br>';
echo '</fieldset>';
echo '</body>';
echo '</html>';

The javascript I have is a countdown which counts down from 5 to 1. The code is this:

<script>

var countdownFrom = 5;  // number of seconds
var countdownwin;

var stp;
function CountDownStart() {
 stp = setInterval("CountDownTimer('CountDownTime')",1000)
}

function CountDownTimer(id)
{
if (countdownFrom==0) {clearInterval(stp); window.close(); }
else     {
            var x
            var cntText = "Closing in "+countdownFrom+" seconds";

        if (document.getElementById)
    {
        x = document.getElementById(id);
        x.innerHTML = cntText;        }
    else if (document.all)
    {
        x = document.all[id];
        x.innerHTML = cntText;        }
    }
countdownFrom--
}

</script>
   <title>Untitled</title>
</head>

<body onload="CountDownStart()">

<Div id="CountDownTime"></div>

</body>

Now I would like to echo this countdown script to replace the <fieldset> in the html. I have tried several things like just add the whole code in 1 echo ''; and I tried to echo all the lines seperately but with both it crashes my whole script. If anyone knows how to do this it would be great!

4
  • ...and what is the problem you have? Commented Apr 16, 2013 at 8:04
  • @deceze The problem is that I can't get this to work. With the things I've tried it crashes my script. Commented Apr 16, 2013 at 8:06
  • "Crashes" how? Errors? What errors? Also, why are you 'echo'ing all this stuff? Just break out of PHP and write it plainly. Commented Apr 16, 2013 at 8:07
  • It wouldn't open the page in browser but you were right about breaking out of php and write it normally there. I did try that before but for some reason it didnt work back then. Thnx anyway. Commented Apr 16, 2013 at 8:15

3 Answers 3

3

I Wouldn't write all those echo's, instead, leave all the HTML and JS outside the PHP block

<?php
some php code
?>

HTML AND JS

<?php
More php if required
?>

And use

<?=$Naam?>

To inject your values where required

Alternatively you should look into template engines

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

Comments

2

Try to use

echo <<<EOT
   /*  some text here */
EOT;

Comments

0

You can put the script in a separate .js file and echo the script tag:

<? echo "<script type='text/javascript' src='path/to/script.js' ></script> ?>

Don't forget to remove any HTML tags from the JS file, like <body>, <head>, etc.

Comments

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.