Hi,歡迎訪問前端老白
<html> <head> <script src="layui/layui.js"></script> </head> <body> <form id="myForm"> <input type="text" name="myInput"> <button id="myBtn">提交</button> </form> <script> layui.use(['form', 'layer'], function(){ var form = layui.form, layer = layui.layer; form.on('submit', function(data){ $.ajax({ url: 'submit.php', type: 'POST', data: data.field, success: function(res){ if(res.code == 0){ layer.msg('提交成功'); }else{ layer.msg(res.msg); } } }); return false; }); }); </script> </body> </html>
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 創建連接 $conn = new mysqli($servername, $username, $password, $dbname); // 檢測連接 if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // 取得表單數據 $myInput = htmlspecialchars($_POST["myInput"]); // 插入數據 $sql = "INSERT INTO myTable (myField) VALUES ('$myInput')"; if ($conn->query($sql) === TRUE) { $result = array('code' => 0, 'msg' => '插入成功'); } else { $result = array('code' => 1, 'msg' => '插入失敗'); } echo json_encode($result); $conn->close(); ?>
老白網絡 (http://www.lofty888.cn/) 前端 后端 zblog主題.網站地圖xml