我有一個表單,我在mysql數據庫中保存了各種字段(名稱,電子郵件,註釋),數據被寫入,但我想顯示數據而沒有重新加載頁面,按提交按鈕並查看新評論。 。如何插入SQL值而無需重新加載?(jQuery + PHP)
HTML:
<form action="" method="post" id="form">
<fieldset>
<input type="hidden" name="noticia_id" value="<?php echo $id; ?>"><br>
<p><label>NOMBRE *</label>
<input type="text" id="nombre" name="usuario"></p>
<p><label for="email">EMAIL (No se publicará) *</label>
<input type="text" id="email" name="email"></p>
<p><label for="comment">COMENTARIO</label>
<textarea name="comentario" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
<p><input type="submit" name="submit" id="submit" tabindex="5" value="Enviar " /></p>
</fieldset>
</form>
PHP:
<?php
if ($_POST) {
//conectamos a la base
$connect=mysql_connect("localhost","root","");
//Seleccionamos la base
mysql_select_db("mostra",$connect);
$id=$_POST['noticia_id'];
$nick=$_POST['usuario'];
$email=$_POST['email'];
$comentario=$_POST['comentario'];
$query = "INSERT INTO comentarios (usuario,email,comentario,noticia_id, fecha) VALUES('$nick','$email','$comentario','$id', NOW())";
mysql_query($query) or die(mysql_error());
$query = "UPDATE noticias SET num_comentarios= num_comentarios+1 where id_noticia='".$id."'";
mysql_query($query) or die(mysql_error());
}
?>
如何創建一個jQuery函數或其他方法,而無需重新加載頁面中插入這些數據?
我看過教程,但我不能幫助我!
你需要使用AJAX。 – ewein
http://api.jquery.com/jQuery.ajax/ –
您還需要防範SQL注入。我建議參數化查詢。 – BAF