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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 Maya的Fbx模型导入Ue4.24后为什么个别部位的UV发生了改变?
      • ¥15 网页设计与开发html
      • ¥15 bin格式的程序文件烧录GD32F105芯片。起始地址填多少?
      • ¥15 python对txt文件的处理
      • ¥25 需要一个能在H5中实现扫码的插件
      • ¥50 ArcGIS或MATLAB实现批量计算整个tif图像的EVI
      • ¥20 两个板子之间CAN通信ID号怎么设置
      • ¥15 两个板子CAN通信的话ID号怎么设置
      • ¥15 vue使用element-ui的el-upload上传图片至服务器,服务端使用的是node.js,图片上传成功,但界面显示不出来,是跨域问题?
      • ¥15 ANSYS APDL循环结果输出