如何在不丢失表单数据且不刷新页面的情况下更新动态选择框

时间:2019-05-11 12:41:29

标签: javascript php jquery mysqli

我需要更新一个选择框 从外部页面。可能

这是下拉选择框

    <select class="form-control select" name="project_size" id="project_size" required="required">
<option value="">Select </option>
<option value="1|Muraba">1|Muraba</option>
<option value="2|Ars">2|Ars</option>
<option value="3|jeerubs">3|jeerubs</option>
<option value="4|canals">4|canals</option>
<option value="5|Marlas">5|Marlas</option>
</select>

options字段中的数据来自mysql数据库表。

<?php $masql = "SELECT * FROM project_size";
try {
$stmtm = $db->prepare($masql);
$resultarea = $stmtm->execute();
$marows = $stmtm->fetchAll();
}
catch(PDOException $e){
die("Failed to Run Query ".$e->getMessage());
}
?>
<label>Plot Type</label>
<select class="form-control select" name="project_size" id="project_size" required="required">
<option value="">Select </option>
<?php foreach($marows as $marow): 
$all =  $marow['code']."|".$marow['name'];
$some =  $marow['code']."|".$marow['name'];?>
<option value="<?php echo $all; ?>"><?php echo $all;?></option>
<?php endforeach; ?>
</select>

“选择”框位于一页上,现在该表单还包含其他一些字段,例如用户填写的字段。但是用户希望在选择框中添加一个新选项,而无需刷新页面和/或不丢失其他字段。

这是表单/页面,其中将选择框的日期添加到数据库表中。

                    <form role="form" method="post" action="?action=add">
                                <div class="form-group">
                                  <div class="col-lg-4 col-md-4">
                                    <label>Name</label>
                                    <input class="form-control" type="text" name="lname" value="" required="required">
                                    <p class="help-block"></p>
                                </div></div><!-- 4 coloms ends -->   
                                <div class="col-lg-2 col-md-2">
                                  <label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </label>
                                <input type="submit" name="save" class="btn btn-primary" value="Add New">
                                </div>

                            </form>

这是处理表单数据的php代码

if(isset($_POST['save']) && $_REQUEST['action']=="add"){
    operation ("o_add");
    $lname = htmlentities($_POST['lname']);
        $query = " 
            INSERT INTO $table (
                name
            ) VALUES (
                :lname

            ) 
        "; 
        $query_params = array( 
            ':lname' => $lname

           ); 
        try 
        { 
            // Execute the query to create the user 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        { 
            die("Failed to run query: " . $ex->getMessage()); 
        }         
        $type = "ok";
        $msg = "Record Added Successfully";
}

用户如何从添加选项页面添加新选项,并在不刷新页面或不丢失其他表单字段的情况下在选择框中刷新/添加新选项,将不胜感激任何编码帮助。

0 个答案:

没有答案