doujiongqin0687 2016-09-11 12:39
浏览 80
已采纳

如何使用Jquery ajax和php将数据插入mysql数据库?

I am trying to learn Ajax. I am inserting some data to mysql database from a Html Form by php. It works nicely. But my ajax part does not work. I get the success message but data dont go to my php file. My html and js code:

<!DOCTYPE html>
<html>
<head>
    <title>Insertion of data with Ajax</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">    </script>
</head>
<body>
    <form id="myForm" method="POST" action="ajax-save.php">

    Title: <input type="text" name="title" id="title"><br /><br />
    Description: <textarea name="description" id="description" rows="20" cols="40"></textarea><br /><br />
    Url: <input type="text" name="url" id="url"><br /><br />

    <input type="submit" id="submit" name="submit" value="submit">

    </form>

    <script>

     $(document).ready(function(){

       $("#submit").click(function(){
       $.ajax({
            url: 'ajax-save.php',
            async: true,
            cache: false,
            data: $('#myForm').serialize() ,
            type: 'POST',
            success: function(){
            alert("success");
            clearForm();
            }
        }); 
           return false;
       });

    });

   </script>

</body>
</html>

My php codes are working properly. I have tested it without ajax. Here is my php code.

    $conn = mysql_connect('localhost', 'root', '');
    $db = mysql_select_db('hospital');

    if (isset($_POST['title'])) { $title = $_POST['title'];}
    if (isset($_POST['description'])) { $description = $_POST['description'];}
    if (isset($_POST['url'])) { $url = $_POST['url'];}

    if(isset($_POST['submit'])){
        if(mysql_query("insert into `wp_upload_video` (`id`, `title`, `description`, `url`) values (NULL, '$title', '$description', '$url')"))
            echo "Successfully Inserted";
        else
            echo "Insertion Failed";
    }

Please let me know where is my fault.

  • 写回答

1条回答 默认 最新

  • dongpu5600 2016-09-11 19:35
    关注

    When you submit via ajax on a click of the submit button.... that condition is always true.

    Checking if $_POST['submit'] is set in the PHP will always result in true because if it is not true the ajax never gets processed.

    So... remove the if submit condition in the PHP and handle error notification in the ajax call.

    Also, as pointed out by @NiettheDarkAbsol in comments, it's a good idea to add e.preventDefault() to the jquery as well to stop the submit button submitting the form as it normally would and allow the jquery to handle the submit (via ajax).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Simulink仿真报错,请问如何解决
  • ¥20 宝塔面板无法添加Node项目,一直处于正在添加脚本页面
  • ¥50 Dkeil5 CT107D单片机的程序编写
  • ¥30 Ubuntu20.04中PVN3D复现过程交叉编译问题
  • ¥15 模拟电路求复阻抗和传递函数,请各位拍照写一下解答过程
  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?