MAO-EYE 2011-06-23 02:28 采纳率: 66.7%
浏览 26

是否可以使用javascript来获取用户的文本并将其插入到mysql数据库中?

我有一个带有提交按钮的简单表单(如下)。我想让用户在文本框中键入内容,然后当他/她单击提交时,页面就会刷新并回显他们在div中键入的内容。 用户输入的数据在回显之前已存储在数据库中,问题是,当我单击提交时,输入不会立即显示,我必须单击刷新才能显示它,并且当我这样做时,浏览器会弹出一个窗口(Safari),要求重新发送数据——这就会导致在数据库中插入重复的数据。 我大概知道需要使用javascript,还可以使用fadeIn使它更加优雅,但我不知道该怎么做。所以我想问问是否有一种方法可以使用javascript来获取用户的文本并将其插入到mysql数据库中?并在1或0(最好是刷新)全部单击提交后显示它?谢谢!

这是我的代码:
<form method='POST'  action='index.php'>
<input type='text' name='text' id='text'>
<input type ='submit' value='submit' name='submit'>
</form>

<?php
$input=$_POST['text'];
$put=mysql_query("INSERT INTO table VALUES ('$input')");

echo "<div id='area'>";
//i connect to the DB and echo out the data here

echo "</div>";

?>
  • 写回答

3条回答 默认 最新

  • weixin_33743880 2011-06-23 02:33
    关注

    Setting aside SQL injection attacks your code is vulnerable with, the standard practice is to respond to the POST with a redirect back to where the form was. In your case, it will be the page which runs SELECT from table.

    评论

报告相同问题?