简体   繁体   中英

Why is my Javascript not working from the PHP file?

I have the following files. For some reason my JavaScript (inside PHP 'echo' tags) does not work:

HTML (index.php):

<form action="submit.php" method="post">
<div id="edit-box">
<INPUT type="submit" name="save-text" id="save-text" value="Save">
<textarea id="editor" name="editor">
<?php echo $content; ?>
</textarea>
</div>
</form>

PHP (submit.php):

<?php

include('connect-db.php');

$submit_date = date("Ymd");
$content = mysql_real_escape_string(htmlspecialchars($_POST['editor']));
$ip_address = getRealIPAddress();

if ($content != '') { 
mysql_query("INSERT source SET submit_date='$submit_date', ip='$ip_address', content='$content'")
or die(mysql_error());

// The following line is not working! I need help here!
echo '<script type="text/javascript"> alert("Your file saved!");</script>';  

mysql_close($connection);
}
?>

The "submit.php" does not have any other PHP or HTML script/tags. I understand I am using obsolete PHP/MySQL API (instead of PDO / MySQLi), but that's beside the point.

It should should print an alert. Are sure you are printing the script? I can see it might die on an error in the query.

If that is not the case, what are you including in the connect-db.php, it might be that somewhere in the required file you are messing up the output buffer..

Also I'm pretty sure the sql is incorrect:

INSERT source SET submit_date='$submit_date', ip='$ip_address', content='$content'
INSERT INTO source (submit_date, ip, content) values('$submit_date','$ip_address','$content');

And that is not a secure query, however... that is beside the point.

Here is some more reasons why this could happen:

  1. The include("connect-db.php'); might kill the execution of the script.
    • If there is something wrong inside of it.
  2. The query might die.
  3. The output is redirected to somewhere else, in the php configuration, or inside of "connect-db.php"
  4. getRealIPAddress() is not documented, and might cause the script to break.
  5. There might be something bogus in $_POST['editor'] that might break cause the sql to die.

Try below code

<?php

include('connect-db.php');

$submit_date = date("Ymd");
$content = mysql_real_escape_string(htmlspecialchars($_POST['editor']));
$ip_address = getRealIPAddress();
$content = $_POST["editor"];
if ($content != '') { 
mysql_query("INSERT source SET submit_date='$submit_date', ip='$ip_address', content='$content'")
or die(mysql_error());

// The following line is not working! I need help here!
echo '<script type="text/javascript"> alert("Your file saved!");</script>';  

mysql_close($connection);
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM