我想在我的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'];
答案 0 :(得分:1)
在接收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'];
答案 1 :(得分:0)
您可以简单地检查您的脚本是否已POST,并确保POST不为空,以确保它包含如下数据:
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST) ) {}
答案 2 :(得分:0)
javascript框架,例如jQuery,会发送一个带有AJAX POST请求的特殊标头 - 所以你可以这样做:
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
// your code
}
某些编程框架(如CodeIgniter)具有自己的功能,可以使其更容易:
$this->input->is_ajax_request();
记住它可能是欺骗性的。
答案 3 :(得分:0)
如果你想保护你的ajax电话。生成令牌并在控制器中匹配该令牌。这些都可以在每个框架中使用。 如果您想在自定义PHP中执行此操作,则应在页面加载会话并在帖子请求中匹配此会话。