php - 我想提交表单而不重新加载页面并且没有 OK 消息
问题描述
我需要您对以下问题的帮助:
我尝试在不重新加载页面的情况下提交表单,但没有成功。我读过其他相关问题,但我做不到。
我想要以下内容: 当用户单击按钮时,应将数据发送到服务器而不显示 OK 消息。 你能帮助我吗?这是我的代码:
索引.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<p align="center">Example</p>
<table align="center" width="730" border="0">
<tr>
<td align="center">
<div>
<table class="blueTable" style="float: left">
<thead>
<tr>
<th colspan="1"><u>Menu</u></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="button" value="New" id="new" onclick="new1();" class="button12"/></td></tr>
<tr>
<td><input type="button" value="Load" id="load" onclick="load2()" class="button"/></td></tr>
<tr>
<td><form name="SaveGame" id="SaveGame" method="post" action="http://127.0.0.1/PHP/mine2.php" enctype="multipart/form-data">
<input type="submit" value="Save" id="save" onclick="save3()" class="button"/>
</form>
</body>
</html>
mine2.php
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("127.0.0.1", "root", "", "mysql3");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt insert query execution
$sql = "INSERT INTO components (user_id, book_id, game_id, site_id) VALUES ('6', '6', '6', '6')";
if(mysqli_query($link, $sql)){
} else{
}
// Close connection
mysqli_close($link);
?>
解决方案
更改您的 html 文件,如下所示:
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<p align="center">Example</p>
<table align="center" width="730" border="0">
<tr>
<td align="center">
<div>
<table class="blueTable" style="float: left">
<thead>
<tr>
<th colspan="1"><u>Menu</u></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="button" value="New" id="new" onclick="new1();" class="button12"/></td></tr>
<tr>
<td><input type="button" value="Load" id="load" onclick="load2()" class="button"/></td></tr>
<button id="save" onclick="save3()" class="button">Save</button><!--This button will perform action-->
<script>
function save3(){
var value = 123;//dummy variable to show you its functionality
$.ajax({
type: "POST",
url: "http://127.0.0.1/PHP/mine2.php",
data: {value: value}, //that value comes from above which I have initialized
success: function(data)
{
console.log(data); //it will show response from php file in your console
}
});
}
</script>
</body>
</html>
在 php 文件中,mine2.php
这是您可以访问value
来自 ajax 请求的方式
<?php
if(isset($_POST['value']))
{
$value = $_POST['value'];
}
?>
推荐阅读
- ios - Snapchat Snap Kit iOS 登录重定向 URL 在 Safari 上不起作用
- python - while 循环不承认 if 语句
- spring - 如何使用 Spring LDAP 对 LDAP 进行身份验证
- flutter - 多个小部件使用相同的 GlobalKey。动画列表
- javascript - 如何将这个随机图像选择器功能转换为拼接所选图像的功能?
- django - 如何在 Django 中动态创建多级分层表单?
- google-api - Google People API 备注字段
- python - SpeechRecognition 在 Python (Raspberry Pi 4) 中不起作用。我在 try/except 中得到了 except
- mysql - SQL 错误 - 添加外键约束失败
- python - 将列表添加到 DF / 将字符串扩展到新列和数据