我有以下脚本。用户将通过一个数值例如123作为URL中的参数,应用程序将从MySQL加载/获取该值并将其显示在textarea中。如何在JavaScript弹出窗口中显示PHP变量值?
例如,在URL中输入“example.com/index.php?id=123”将从数据库中拉出第123条记录并将其显示在textarea中。一切似乎都在起作用,但我希望在提交表单时显示“源”表的最后一行/ id。所以,而不是显示“已保存!”在弹出窗口中,它应该显示“124”(或最后一次更新的行/编号)。
的index.php:
<?php
include('connect-db.php');
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
$id = $_GET['id'];
$result = mysql_query("SELECT content FROM source WHERE id=$id") or die(mysqli_error());
$row = mysql_fetch_array($result);
if($row)
{
$submit_date = date("Ymd");
$content = $row['content'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#myform").submit(function() {
$.post(
'submit.php',
$(this).serialize(),
//
// show the last id here!
//
function(data){ alert("Saved!"); }
);
return false;
});
});
</script>
</head>
<body>
<div id="header" >
<form action="submit.php" method="post" id="myform">
<textarea id="editor" name="editor" id="editor"><?php echo $content; ?></textarea>
<br/>
<input type="submit" name="submit" value="Submit" id="submit"/>
</form>
</div>
</body>
</html>
submit.php:
<?php
include('connect-db.php');
$submit_date = date("Ymd");
$content = mysql_real_escape_string(htmlspecialchars($_POST['editor']));
$ip_address = $_SERVER['REMOTE_ADDR'];
if ($content != '') {
//
// Show the variable (last_id) value in the JavaScript above!
//
$last_id = mysql_insert_id();
mysql_query("INSERT INTO source (submit_date, ip, content) values('$submit_date','$ip_address','$content')") or die(mysql_error());
mysql_close($connection);
}
?>
连接-DB:
<?php
$server = 'localhost';
$user = 'domdom';
$pass = '[email protected]';
$db = 'domdom';
$connection = mysql_connect($server, $user, $pass) or die ("Could not connect to server ... \n" . mysql_error());
mysql_select_db($db) or die ("Could not connect to database ... \n" . mysql_error());
?>
请注意,我不需要在PDO或MySQLi的任何建议,这在当时并不重要。但是,有关改进安全性/ SQL查询等的任何建议都是值得欢迎的。谢谢。
为什么不使用预处理语句?它们与您习惯的语法几乎完全相同。 – Blender 2013-03-10 02:59:40
你最后的声明让我感到困惑。 PDO/MySQLi ==查询安全性的改进。 – Daedalus 2013-03-10 02:59:46
你也容易受到HTML注入(XSS) – Eevee 2013-03-10 03:04:14