我有以下腳本。用戶將通過一個數值例如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