为什么我的Javascript不从PHP文件工作


Why is my Javascript not working from the PHP file?

我有以下文件。由于某些原因,我的JavaScript (PHP 'echo'标签内)不工作:

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

"submit.php"没有任何其他PHP或HTML脚本/标签。我知道我正在使用过时的PHP/MySQL API(而不是PDO/MySQLi),但这不是重点。

应该应该打印一个警告。确定要打印脚本吗?我知道它可能会死在查询错误。

如果不是这样,你在connect-db.php中包含什么?可能是您在所需文件的某个地方搞砸了输出缓冲区。

我也很确定sql是不正确的:

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

这不是一个安全的查询,然而…那是离题了。

以下是可能发生这种情况的更多原因:

  1. include("connect-db.php');可能会终止脚本的执行。
    • 如果里面有问题。
  2. 查询可能会死亡。
  3. 输出被重定向到php配置中的其他地方。或在"connect-db.php"
  4. getRealIPAddress()没有记录,可能会导致脚本崩溃。
  5. 可能有一些假的$_POST['editor']可能打破原因

试试下面的代码

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