gpt4 book ai didi

php - 如何在 PHP 文件中检查以表单形式提交并通过 AJAX 调用发送的值

转载 作者:行者123 更新时间:2023-11-28 23:56:00 25 4
gpt4 key购买 nike

我想在我的 php 文件中为被放入组 #3 的用户执行一个 INSERT 查询。我知道使用 PHP 可以检查按钮是否被按下或是否设置了某些东西,但是如何检查值是否已发送并通过 AJAX 传递?

那么,假设我将组级别更改为 3(组级别通过表单中的值选项确定)。如何让我的查询检查是否选择了选项?

表单

<div class="change_group_button"> 
<a class="change_group" href="javascript:void(0)">Change User

Permission</a>
</div><br>
<div id="light" class="change_group_popup">
<a class="close" href="javascript:void(0)">Close</a>
<div class="group_success" style="color: red;"></div><br>
<form id="update_group" action="" method="POST" accept-charset="utf-8">
<div class="field">
<label for="group">Group</label>
<input type="hidden" value="<?php echo $approved_id; ?>" id="approved_id" name="id" />
<input type="hidden" value="<?php echo $approved_firstname; ?>" id="approved_firstname" name="firstname" />
<input type="hidden" value="<?php echo $approved_lastname; ?>" id="approved_lastname" name="lastname" />
<input type="hidden" value="<?php echo $approved_username; ?>" id="approved_username" name="username" />
<input type="hidden" value="<?php echo $approved_email; ?>" id="approved_email" name="email" />
<select id='group_id' name='group' required>
<option value=''><?php echo htmlentities($group); ?></option>
<option value="1">Bench</option>
<option value="2">Spectator</option>
<option value="3">Team Member</option>
<option value="4">Commissioner</option>
</select>
</div>
<input type="submit" value="submit" name="group">
</form>

AJAX 调用

$('#update_group').on('submit', function (event) {
event.preventDefault();
$.ajax({
url: 'user_group_update.php',
type: 'POST',
data: {
id: $("#approved_id").val(), //id
firstname: $("#approved_firstname").val(), //firstname
lastname: $("#approved_lastname").val(), //lastname
username: $("#approved_username").val(), //username
email: $("#approved_email").val(), //email
// update_group: $("#group_id").val() //group level
update_group: $(this).find( "#group_id option:selected" ).val()
},
success: function (data) {
//do something with the data that got returned
$(".group_success").fadeIn();
$(".group_success").show();
$('.group_success').html('User Permission Level Changed!');
$('.group_success').delay(5000).fadeOut(400);
alert(data);
},
error: function(jqXHR, textStatus,errorThrown )
{
// alert on an http error
alert( textStatus + errorThrown );
}
});
return false;
});

在我的 PHP 文件中,我从表单中获取这些字段..

$approved_id = $_POST['id'];
//test - delete if it doesn't work
$approved_firstname = $_POST['firstname'];
$approved_lastname = $_POST['lastname'];
$approved_username = $_POST['username'];
$approved_email = $_POST['email'];
$change_group = $_POST['update_group'];

最佳答案

在接收 POST 的 PHP 文件中,您可以进行值检查:

$approved_id = $_POST['id'];

if($approved_id == '3')
{
//do something
}
else
{
//do something else
}
//test - delete if it doesn't work
$approved_firstname = $_POST['firstname'];
$approved_lastname = $_POST['lastname'];
$approved_username = $_POST['username'];
$approved_email = $_POST['email'];
$change_group = $_POST['update_group'];

关于php - 如何在 PHP 文件中检查以表单形式提交并通过 AJAX 调用发送的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31660474/

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