2013-03-08 45 views
-3

這是我正在嘗試做的...... 當用戶單擊窗體中的提交按鈕(我正在使用可正常工作的jQuery Form plugin)時,這應該將textarea的內容保存在數據庫中。 JavaScript警報應該顯示數據庫中最後更新的ID。但由於某些原因,雖然數據正在保存,但JavaScript不起作用,不知道爲什麼。JavaScript與PHP結合?

HTML格式(index.html的):

<form action="submit.php" method="post" id="myForm"> 
<input type="submit" name="submit" value="Submit"/> 
<div id="edit-box"> 
<textarea id="editor" name="editor"><?php echo $content; ?></textarea> 
</div> 
</form> 

提交表單(submit.php):

<? 

// connect to the database 
include('connect-db.php'); 


// get form data, making sure it is valid 
$submit_date = date("Ymd"); 
$content = mysql_real_escape_string(htmlspecialchars($_POST['editor'])); 
$ip_address = getRealIPAddress(); //$_SERVER['REMOTE_ADDR']; 

// check to make sure both fields are entered 
if ($content != '') { 

// save the data to the database 
mysql_query("INSERT source SET submit_date='$submit_date', ip='$ip_address', content='$content'"); 
or die(mysql_error()); 

$lastrow = mysql_query("SELECT MAX(id) FROM source"); 

echo '<script type="text/javascript"> alert("' . echo $lastrow . '");</script>'; 

} 
?> 

而且,我必須每次關閉連接?

+0

您應該始終關閉連接。總是。另外,你在連接時使用'echo';拿出來,這不是回聲的工作原理。 – Qix 2013-03-08 17:06:31

+0

您正在使用[an **過時的**數據庫API](http://stackoverflow.com/q/12859942/19068)並應使用[現代替換](http://php.net/manual/en/) mysqlinfo.api.choosing.php)。 – Quentin 2013-03-08 17:07:41

+0

「JavaScript不起作用」 - 我們看不到JavaScript。看看PHP的輸出。它是否生成你想要的JavaScript?如果沒有,那就是你應該問的問題。是的,然後向我們展示JavaScript而不是PHP。 – Quentin 2013-03-08 17:09:10

回答

0

爲什麼不使用'mysql_insert_id'來獲取最後插入的ID?

$lastrow = mysql_insert_id();