gpt4 book ai didi

javascript - 如何从 html 表单执行 php 查询?

转载 作者:行者123 更新时间:2023-11-29 17:36:09 24 4
gpt4 key购买 nike

我知道 php 是一种服务器端语言,而 html 是一种客户端语言,为此我会使用 javascript,但我不知道该怎么做。有人可以帮我编写名为 changeFunction() 的脚本吗?


这是我在index.php中的表单:

<form method="POST">
<div class="row"style="margin-top: 5%;">
<div class="col-3">

</div>
<div class="col-6">
<label for="inputUsername">Insert username</label>
<input type="text" class="form-control" name="inputUser" style="margin-bottom: 2%">
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Convert account type to: </label>
</div>
<select class="custom-select" id="inputGroupSelect01" name="inputSelected">
<option selected>Choose...</option>
<option value="admin">Admin</option>
<option value="user">User</option>
</select>
<button type="button" class="btn btn-outline-primary" onclick="changeFunction()">Submit</button>
</div>
</div>
<div class="col-3">
</div>
</div>
</form>

这是我在 query.php 中的查询:

 <?php     
$var1= $_POST["inputGroupSelect01"];
$var2= $_POST["inputUser"];
require 'connection.php';
$sql = "UPDATE user SET typeAcc =".$var1." WHERE username=".$var2;
?>

最佳答案

我希望这是您想要的解决方案。

首先在 html 表单中添加以下 div <div id='resultDiv' ></div>

所以你的html表单代码是这样的

<form method="POST" >
<div class="row"style="margin-top: 5%;">
<div class="col-3">
<h2>Testing Form</h2>
</div>
<div class="col-6">
<label for="inputUsername">Insert username</label>
<input type="text" class="form-control" name="inputUser" style="margin-bottom: 2%" id="inputUser">
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Convert account type to: </label>
</div>
<select class="custom-select" id="inputGroupSelect01" name="inputSelected">
<option selected>Choose...</option>
<option value="admin">Admin</option>
<option value="user">User</option>
</select>
<button type="button" class="btn btn-outline-primary" onclick="changeFunction()">Submit</button>
</div>
<!-- this need to be added -->
<div id='resultDiv' ></div>
</div>
<div class="col-3">
</div>
</div>
</form>

第二将 jQuery 和以下脚本添加到您的代码

两个jquery变量定义userNamerole随后被传递为 data如果服务器响应是 SUCCESS然后执行成功条件,或者您可以通过设置 ERROR 发送数据或特定错误消息与 YOUR ERROR TEXT

 <script src="https://code.jquery.com/jquery-3.3.1.min.js" 
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>

<script type='text/javascript'>

function changeFunction(){
var userName = $('#inputUser').val();
var role =$('#inputGroupSelect01').val();

$.ajax({
method: "POST",
url: "server.php",
data: JSON.stringify( { "userId": userName, "userRole": role } ),
dataType: "text",

success: function (response){
if(response=='SUCCESS'){
$('#resultDiv').append('<br/>Successful Updated'+response);

}else{
$('#resultDiv').append('<br/>Error'+ response);
}
}

});
}
</script>

**第三**你必须写新的server.php文件包含以下代码

<?php     
$var1= $_POST["userId"]; // change here userId as used in jquery block
$var2= $_POST["userRole"];// change here userRole as used in jquery Block
//require 'connection.php';
$sql = "UPDATE user SET typeAcc =".$var1." WHERE username=".$var2;

//suppose query is executed successfull then
//it will return no of rows updated


$query_result = mysql_query($query);
//SUCCESS_CONDITION is normally if number of row updated is 1 in return
///of query execution so kindly define by your own way

$num_rows = mysql_num_rows($query_result);
//if($num_rows==1 || define your own SUCCESS_CONDITION){
if($num_rows ==1){
$result ='SUCCESS';
}else{
$result ='ERROR';
}

echo $result;
?>

希望对你有帮助谢谢如果需要请寻求帮助

enter image description here

关于javascript - 如何从 html 表单执行 php 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50268181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com