[英]How do I insert data from selected ajax dropdown PHP
Here's the sample of my codes:这是我的代码示例:
In index.php, I created drop down HTML and load drop down list with employee names from MySQL database table.在 index.php 中,我创建了下拉 HTML 并加载了带有 MySQL 数据库表中员工姓名的下拉列表。
<div class="page-header">
<h3>
<select id="employee">
<option value="" selected="selected">Select Employee Name</option>
<?php
$sql = "SELECT id, employee_name, employee_salary, employee_age FROM
employee LIMIT 10";
$resultset = mysqli_query($conn, $sql) or die("database error:".
mysqli_error($conn));
while( $rows = mysqli_fetch_assoc($resultset) ) {
?>
<option value="<?php echo $rows["id"]; ?>"><?php echo
$rows["employee_name"]; ?></option>
<?php } ?>
</select>
</h3>
</div>
<div id="display">
<div class="row" id="heading" style="display:none;"><h3><div
class="col-sm-3"><strong>Employee Name</strong></div><div class="col-sm-4">
<strong>Age</strong></div><div class="col-sm-4"><strong>Salary</strong>
</div>
</h3></div><br>
<div class="row" id="records"><div class="col-sm-3" id="emp_name"></div>
<div class="col-sm-4" id="emp_age"></div><div class="col-sm-3"
id="emp_salary"></div></div>
<div class="row" id="no_records"><div class="col-sm-3">Plese select
employee name to view details</div></div>
</div>
<div style="margin:50px 0px 0px 0px;">
<input type="submit" class="btn btn-default read-more" name="submit" value="submit">
</div>
</div>
Drop Down Selection Data Load with jQuery Ajax Now in getData.js JavaScript file, we will handle drop down selection change event to get selected value and make Ajax request to server getEmployee.php to get selected employee details from MySQL database table employee.使用 jQuery Ajax 加载下拉选择数据现在在 getData.js JavaScript 文件中,我们将处理下拉选择更改事件以获取选定值并向服务器 getEmployee.php 发出 Ajax 请求以从 MySQL 数据库表员工中获取选定员工详细信息。 The Ajax request gets response employee data in JSON format from server.
Ajax 请求从服务器获取 JSON 格式的响应员工数据。 We will display that response JSON data with jQuery.
我们将使用 jQuery 显示该响应 JSON 数据。
$(document).ready(function(){
// code to get all records from table via select box
$("#employee").change(function() {
var id = $(this).find(":selected").val();
var dataString = 'empid='+ id;
$.ajax({
url: 'getEmployee.php',
dataType: "json",
data: dataString,
cache: false,
success: function(employeeData) {
if(employeeData) {
$("#heading").show();
$("#no_records").hide();
$("#emp_name").text(employeeData.employee_name);
$("#emp_age").text(employeeData.employee_age);
$("#emp_salary").text(employeeData.employee_salary);
$("#records").show();
} else {
$("#heading").hide();
$("#records").hide();
$("#no_records").show();
}
}
});
})
});
Get Data from MySQL Database从 MySQL 数据库中获取数据
Now finally in getEmployee.php, we will get employee details from MySQL database table and return data as JSON using json_encode.现在终于在 getEmployee.php 中,我们将从 MySQL 数据库表中获取员工详细信息,并使用 json_encode 以 JSON 形式返回数据。
<?php
include_once("db_connect.php");
if($_REQUEST['empid']) {
$sql = "SELECT id, employee_name, employee_salary, employee_age FROM
employee WHERE id='".$_REQUEST['empid']."'";
$resultset = mysqli_query($conn, $sql) or die("database error:".
mysqli_error($conn));
$data = array();
while( $rows = mysqli_fetch_assoc($resultset) ) {
$data = $rows;
}
echo json_encode($data);
} else {
echo 0;
}
?>
Now, I am going to add SUBMIT button and if that button is set, I want to INSERT the data that has been shown in the table after I clicked one of the employee names in the dropdown option.现在,我将添加提交按钮,如果设置了该按钮,我想在单击下拉选项中的员工姓名之一后插入已显示在表中的数据。 How do I do that?
我怎么做?
You should link the Submit button .click event to an Ajax call.您应该将提交按钮 .click 事件链接到 Ajax 调用。
Also you must have insertEmp.php file which will make the insert.此外,您必须有 insertEmp.php 文件,该文件将进行插入。
First, set an id to your Submit button (id = submitButton)首先,为您的提交按钮设置一个 id (id = submitButton)
An approximation of the code could be like this:代码的近似值可能是这样的:
$('#submitButton').click(function() {
$.ajax({
url:"/insertEmp",
type: "POST",
data: {emp_name:$('#emp_name').val() , emp_salary:$('#emp_salary').val() , .. all the others parameters... },
success: function (results) {
},
error: function (results) {
}
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.