我的表单的一部分是通过ajax添加的,这使得表单无法提交到数据库。通过ajax添加的部分被删除后,将提交表单。
在添加依赖于ajax的select元素填充部分之前,我有一个适用于表单的代码,该如何解决此问题?
<form action="" id="treeview_form" class="wpcf7-form" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Register Agency</legend>
<div class="col-one-fourth">
<label>Select Parent Agency</label>
<select name="parent_category" id="parent_category">
</select>
</div>
<div class="col-one-fourth">
<label> Agency Name</label>
<input type="text" id="agencyName" name="agencyName" <?php if (isset($agencyName)) { echo 'value="' . htmlentities($agencyName, ENT_COMPAT, 'UTF-8') . '"'; } ?>>
</div>
<div class="clearfix"></div>
<div class="text-right">
<div class="divider-single"></div>
<input type="hidden" name="action" value="newAgency" />
<button class="btn btn-primary btn-big">Register</button>
</div>
</fieldset>
</form>
<script>
$(document).ready(function(){
fill_parent_category();
fill_treeview();
function fill_treeview()
{
$.ajax({
url:"fetch.php",
dataType:"json",
success:function(data){
$('#treeview').treeview({
data:data
});
}
})
}
function fill_parent_category()
{
$.ajax({
url:'fill_parent_category.php',
success:function(data){
$('#parent_category').html(data);
}
});
}
});
</script>
fill_parent_category.php
<?php
include 'database_connection.php';
$query = "SELECT * FROM agencies ORDER BY id ASC";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '<option value="0">Parent Category</option>';
foreach ($result as $row) {
$output .= '<option value="'.$row["id"].'">'.$row["agencyName"].'</option>';
}
echo $output;
fetch.php
<?php
include 'database_connection.php';
$parentId = 0;
$query = "SELECT id, parentId, agencyName FROM agencies";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach ($result as $row) {
$data = get_node_data($parentId, $connect);
}
echo json_encode(array_values($data));
function get_node_data($parentId, $connect) {
$query = "SELECT id, parentId, agencyName FROM agencies WHERE parentId = '".$parentId."'";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = array();
foreach ($result as $row) {
$sub_array = array();
$sub_array['text'] = $row['agencyName'];
$sub_array['nodes'] = array_values(get_node_data($row['id'], $connect));
$output[] = $sub_array;
}
return $output;
}
我希望表单能够发布并提交到数据库,但是单击“提交”按钮后,表单似乎可以处理,但是返回相同的填充界面,而无需提交表单值。