为什么我在使用 OnChange 提交时无法访问表单数据?
Why am I unable to access form data when submitting using OnChange?
我有一个简单的 HTML select 选项,它应该自动发送 OnChange
。该表格似乎发送或 "Reload the page" 我应该说。我在另一端没有得到任何数据。我试过 POST
和 GET
我重命名了表单数据的每个部分并更改了整个 if(isset()
信息。为什么我没有收到任何数据有什么想法吗?
表格:
<?php
$system1 = mysqli_fetch_assoc(mysqli_query($conn,
"SELECT * FROM ap_system_status WHERE system_id = '1'"));
$system_id_1 = $system1['system_id'];
$system_name_1 = $system1['system_name'];
$system_status_1 = $system1['system_status'];
?>
<strong><? echo $system_name_1; ?></strong>
<div align="right" style="float:right">
<div class="form-group">
<form action="" method="GET" id="system_status" name="system_status">
<input type="hidden" id="system_id" value="<? echo $system_id_1; ?>" />
<select class="form-control" onchange="this.form.submit()" id="status">
<option disabled="disabled" selected="selected" hidden="hidden"><? echo $system_status_1; ?></option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Experiencing Errors'>Experiencing Errors</option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Being Updated'>Being Updated</option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Maintenance Mode'>Maintenance Mode</option>
<option value='<i class="fa fa-times-circle-o" style="color:#F00"></i> System Offline'>System Offline</option>
<option value='<i class="fa fa-check-circle-o" style="color:#090"></i> Good Service'>Good Service</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
数据收集(非常基本但仅用于测试!!):
<?php
if(isset($_POST['system_status'])){
$system_id = $_POST['system_id'];
$status = $_POST['status'];
echo $system_id;
echo $status;
}else{
}
?>
对您的表单进行一些更改,PHP 应该可以正常工作。为了使表单元素能够随表单一起提交,它们必须具有name
属性,并且表单method
必须匹配您所使用的超全局($_GET
或$_POST
)在 PHP 中访问。
<!--Change the form method to match what you are using in your PHP-->
<form action="" method="POST" id="system_status" name="system_status">
<!--Add a name to this input-->
<input type="hidden" id="system_id" value="<? echo $system_id_1; ?>" name="system_id" />
<!--Add a name to this select-->
<select class="form-control" onchange="this.form.submit()" id="status" name="status">
<option disabled="disabled" selected="selected" hidden="hidden"><? echo $system_status_1; ?></option>
<!--Not related to your question, but switch the value attribute with the
option text so you can see your icons and get the value I assume you expect-->
<option value="Experiencing Errors"><i class="fa fa-exclamation-triangle" style="color:#F60"></i> Experiencing Errors</option>
...
</select>
<noscript><input type="submit" value="Submit"></noscript>
我实际上不建议完全取消 isset
检查,而是修改它以便它实际验证您要使用的值是否存在。
<?php
if (!empty($_POST['system_id']) && !empty($_POST['status'])) {
$system_id = $_POST['system_id'];
$status = $_POST['status'];
echo $system_id;
echo $status;
} else {
// error handling for missing values
}
我有一个简单的 HTML select 选项,它应该自动发送 OnChange
。该表格似乎发送或 "Reload the page" 我应该说。我在另一端没有得到任何数据。我试过 POST
和 GET
我重命名了表单数据的每个部分并更改了整个 if(isset()
信息。为什么我没有收到任何数据有什么想法吗?
表格:
<?php
$system1 = mysqli_fetch_assoc(mysqli_query($conn,
"SELECT * FROM ap_system_status WHERE system_id = '1'"));
$system_id_1 = $system1['system_id'];
$system_name_1 = $system1['system_name'];
$system_status_1 = $system1['system_status'];
?>
<strong><? echo $system_name_1; ?></strong>
<div align="right" style="float:right">
<div class="form-group">
<form action="" method="GET" id="system_status" name="system_status">
<input type="hidden" id="system_id" value="<? echo $system_id_1; ?>" />
<select class="form-control" onchange="this.form.submit()" id="status">
<option disabled="disabled" selected="selected" hidden="hidden"><? echo $system_status_1; ?></option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Experiencing Errors'>Experiencing Errors</option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Being Updated'>Being Updated</option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Maintenance Mode'>Maintenance Mode</option>
<option value='<i class="fa fa-times-circle-o" style="color:#F00"></i> System Offline'>System Offline</option>
<option value='<i class="fa fa-check-circle-o" style="color:#090"></i> Good Service'>Good Service</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
数据收集(非常基本但仅用于测试!!):
<?php
if(isset($_POST['system_status'])){
$system_id = $_POST['system_id'];
$status = $_POST['status'];
echo $system_id;
echo $status;
}else{
}
?>
对您的表单进行一些更改,PHP 应该可以正常工作。为了使表单元素能够随表单一起提交,它们必须具有name
属性,并且表单method
必须匹配您所使用的超全局($_GET
或$_POST
)在 PHP 中访问。
<!--Change the form method to match what you are using in your PHP-->
<form action="" method="POST" id="system_status" name="system_status">
<!--Add a name to this input-->
<input type="hidden" id="system_id" value="<? echo $system_id_1; ?>" name="system_id" />
<!--Add a name to this select-->
<select class="form-control" onchange="this.form.submit()" id="status" name="status">
<option disabled="disabled" selected="selected" hidden="hidden"><? echo $system_status_1; ?></option>
<!--Not related to your question, but switch the value attribute with the
option text so you can see your icons and get the value I assume you expect-->
<option value="Experiencing Errors"><i class="fa fa-exclamation-triangle" style="color:#F60"></i> Experiencing Errors</option>
...
</select>
<noscript><input type="submit" value="Submit"></noscript>
我实际上不建议完全取消 isset
检查,而是修改它以便它实际验证您要使用的值是否存在。
<?php
if (!empty($_POST['system_id']) && !empty($_POST['status'])) {
$system_id = $_POST['system_id'];
$status = $_POST['status'];
echo $system_id;
echo $status;
} else {
// error handling for missing values
}