Hello, I would like to use the alert function from javascript from inside the php <? ?> tags, I find the same thing over and over again when using google: echo"<script>alert('my message');</script>"; PHP: But this doesn't work. In fact, nothing happens..
Sure,, This is the php-file that allows logged in users to use the guestbook in my system. <?php $allowed_html_tags = ""; $addtotop = "1"; // This determines the order to display it. Leave for newest comments on top or change to 0 for oldest to newest. // Checks if the user has already voted the past 5 minutes or not if (isset($_COOKIE["guestbook"])){ $spam = true; } else{ setcookie("guestbook", "guestbook", time()+300); $spam = false; } // Checks if the user wants to view the form or add a entry if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($spam == false)) { $name = $HTTP_COOKIE_VARS["loginname"]; $post = $HTTP_POST_VARS['post']; // Process the Information Entered and Remove Stuff $post = strip_tags($post, $allowed_html_tags); // Strip HTML $post = stripslashes($post); // Strip Slashes date_default_timezone_set("Europe/Brussels"); $time = date("d/m/y, G:i"); if ($addtotop == "0" ) { // Writes the user's entry to a file $fp = fopen("data.php", "a"); fputs($fp, "<p>Gepost door " . "<u><b>$name</b></u>" . " op " . "<b>$time</b>" . "</p><p>" . $post . "</p>"); fclose($fp); } if ($addtotop == "1" ) { // Get all the current entries and put it in a string $att1 = "data.php"; $att2 = fopen ($att1, "rb"); $currententries = fread ($att2, filesize ($att1)); fclose ($att2); // Writes the user's post to a file $fp = fopen("data.php", "w+"); fputs($fp, "<p>Gepost door " . "<u><b>$name</b></u>" . " op " . "<b>$time</b>" . "</p><p>" . $post . "</p>" . $currententries); fclose($fp); } header("Location: index.php?error=false"); } else { header("Location: index.php?error=true"); //echo"<script>alert('my message');</script>"; } ?> PHP:
Okay, here's your problem: header("Location: index.php?error=true"); PHP: That line is redirecting the script to "index.php?error=true" so anything after that line will be ignored (because the user will no longer actually be on that page). You'll either need to put the echo before that command or on the actual "index.php" page.
Mmmh, I'm getting this error when placing the echo in front of the header. I know it's saying my output is already been created in line 53 (the echo). And I'm trying to change the output again in line 54 with the command header. It doesn't allow me to change the output twice so it gives an error like this: PHP Warning: Cannot modify header information - headers already sent by (output started at /home/verplrke/public_html/comiteG/guestbook/insert.php:53) in /home/verplrke/public_html/comiteG/guestbook/insert.php on line 54 How can I put these in one echo then ?? Something like this I thought, but doesn't work,, The header works, the javascript is being ignored: echo("<script>alert('my message')</script>" . header("Location: index.php?error=true"); regards
hmm, you must avoid passing any content in the header when redirecting, otherwise it will result in this error, one solution is to use a js redirection instead ex: echo "<script>alert('my message');window.location.href='index.php?error=true'</script>";
Here's what's happening. When you echo any content for the first time the HTTP headers are created and the content begins buffering. This means once you echo something you cannot edit anything in the HTTP Headers because they've already been created. So you cannot echo something and then edit the headers to redirect them. Either you put the JS alert box on index.php or like the person above said create a JS/HTML redirect instead of a PHP one.