这是我的代码示例:
在index.php中,我创建了一个下拉HTML并从MySQL数据库表中加载带有员工姓名的下拉列表。
Analytics.track(
user_id: '019mr8mf4r',
event: 'Clicked a Link',
properties: {
linkText : 'Next'
},
context: {
'Google Analytics' => {
clientId: '1033501218.1368477899'
}
}
)
使用jQuery Ajax下拉选择数据加载 现在在getData.js JavaScript文件中,我们将处理下拉选择更改事件以获取选定的值,并向服务器getEmployee.php发出Ajax请求,以从MySQL数据库表employee中获取选定的雇员详细信息。 Ajax请求从服务器获取JSON格式的响应员工数据。我们将使用jQuery显示该响应JSON数据。
public static void readContacts(Context context) {
if (context == null)
return;
ContentResolver contentResolver = context.getContentResolver();
if (contentResolver == null)
return;
String[] fieldListProjection = {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER,
ContactsContract.Contacts.HAS_PHONE_NUMBER
};
Cursor phones = contentResolver
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI
, fieldListProjection, null, null, null);
HashSet<String> normalizedNumbersAlreadyFound = new HashSet<>();
if (phones != null && phones.getCount() > 0) {
while (phones.moveToNext()) {
String normalizedNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
if (Integer.parseInt(phones.getString(phones.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (normalizedNumbersAlreadyFound.add(normalizedNumber)) {
String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d("test", " Print all values");
}
}
}
phones.close();
}
}
从MySQL数据库获取数据
现在终于在getEmployee.php中,我们将从MySQL数据库表中获取员工详细信息,并使用json_encode将数据作为JSON返回。
<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>
现在,我要添加“提交”按钮,如果设置了该按钮,则我想在单击下拉选项中的一名雇员姓名后,插入表中显示的数据。我该怎么做?
答案 0 :(得分:0)
您应该将Submit按钮.click事件链接到Ajax调用。
此外,您还必须具有insertEmp.php文件,才能进行插入。
首先,将ID设置为您的Submit按钮(id = SubmitButton)
代码近似如下:
$('#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) {
}
})
})