默认下拉选项并显示其默认值

时间:2017-12-17 08:50:52

标签: php html

我有一个下拉选择框,其值从数据库中的字段priority填充。然后,有三个div标签,display_durationarrivaldispatch date根据显示的选项计算。

我能够执行所需的功能,代码运行顺畅但是还有一个条件Normal应该被选为默认选项值,默认情况下它的值应该显示在div标签中。

目前,我无法将普通设置为默认值,因此,我没有在div标签中获取其相关值。

这是我的代码:

数据库:

CREATE TABLE `tblpriority` (
    `priority_id` INT(11) NOT NULL AUTO_INCREMENT,  
    `priority` VARCHAR(15),
    `dispatch_interval_days` INT,
    `arrival_interval_days` INT,
    `display_duration` VARCHAR(50),
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `updated_at` DATETIME,
    PRIMARY KEY (`priority_id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;

查询插入值:

INSERT INTO `tblpriority` (`priority`,`dispatch_interval_days`,`arrival_interval_days`,`display_duration`) VALUES ('Normal',40,55,'4-6 Weeks');
INSERT INTO `tblpriority` (`priority`,`dispatch_interval_days`,`arrival_interval_days`,`display_duration`) VALUES ('High',30,45,'3-4 Weeks');
INSERT INTO `tblpriority` (`priority`,`dispatch_interval_days`,`arrival_interval_days`,`display_duration`) VALUES ('Low',45,60,'6-8 Weeks');
INSERT INTO `tblpriority` (`priority`,`dispatch_interval_days`,`arrival_interval_days`,`display_duration`) VALUES ('Very High',25,40,'3 Weeks');

index.php(主文件)

<?php

use Cosmo\DAL\Order;  

$priority_list_data = new Order();
$priority_list = $priority_list_data->getPriorityList();
$priority_option = '<option value=""></option>';
if ($priority_list != false) {
    for ($i = 0; $i < count($priority_list); $i++) {
        $priority_option .= '<option value= "' . $priority_list[$i]['priority_id'] . '" >' . $priority_list[$i]['priority'] . '</option>';
    }
}
?>
<div class="item text-center">
  <blockquote>
    <div class="row form-group">
      <div class="col-md-2 col-sm-1"></div>
      <div class="col-md-8 col-sm-10">
        <div class="panel panel-default">
          <div class="panel-body">
            <span class="pull-left">Priority:</span>
            <select class="form-control" id="priority_selected">
              <?php echo $priority_option; ?>
            </select>
            <div class="small" id="priority_duration"></div>
          </div>
        </div>
      </div>
      <div class="col-md-2 col-sm-1">
      </div>
    </div>
    <div class="row form-group">
      <div class="col-md-2 col-sm-1"></div>
      <div class="col-md-4 col-sm-5">
        <div class="panel panel-default">
          <div class="panel-heading">Estimated Dispatch Date:</div>
          <div class="panel-body" id="dispatch_date">
            <h3></h3>
          </div>
        </div>
      </div>
      <div class="col-md-4 col-sm-5">
        <div class="panel panel-default">
          <div class="panel-heading">Estimated Date of Arrival:</div>
          <div class="panel-body" id="arrival_date">
            <h3></h3>
          </div>
        </div>
      </div>
      <div class="col-md-2 col-sm-1"></div>
    </div>

  </blockquote>
</div>


<script>
    $(document).ready(function () {
        $("#priority_selected").change(function () {
            var id = $(this).find(":selected").val();
            var dataString = 'priority_id=' + id;
            $.ajax({
                url: '_getprioritydata.php',
                dataType: "json",
                data: dataString,
                cache: false,
                success: function (resultData) {
                    if (resultData['msg'] == 'Success') {
                        $("#priority_duration").html(resultData.display_duration);
                        $("#arrival_date").html(resultData.arrival_interval_dates);
                        $("#dispatch_date").html(resultData.dispatch_interval_dates);
                    } else if (resultData['msg'] == 'Failed') {
                        $("#priority_duration").html('');
                        $("#arrival_date").html('');
                        $("#dispatch_date").html('');
                    } else {
                        $("#priority_duration").html('');
                        $("#arrival_date").html('');
                        $("#dispatch_date").html('');
                    }
                }
            });
        })
    });
</script>

Order.php(类文件)

<?php

namespace Cosmo\DAL;

Class Order
{
    private $db;

    public function __construct(){
        $config = ConfigSettings::getInstance();
        $this->db = DB::getInstance($config->getDatabaseServer(), $config->getDatabaseUsername(), $config->getDatabasePassword(), $config->getDatabaseName());
    }



    public function getPriorityList(){
        $result = $this->db->query("SELECT priority,priority_id FROM tblpriority");
        $priority_data  = $result->fetchAllArray();
        if(count($priority_data)!= 0){
            return $priority_data;
        }
        else{
            return false;
        }
    }


    public function getDurationandDates($priority_id)
    {
        $result = $this->db->query("SELECT display_duration,dispatch_interval_days,arrival_interval_days FROM tblpriority WHERE priority_id='" . $priority_id . "'");
        $priorityData = $result->fetchAll();
        return $priorityData;
    }

}

?>

_getprioritydata.php(json file)

<?php

    use Cosmo\DAL\Order;

$response = [];

if (!empty($_REQUEST["priority_id"])) {

    $priority_id = $_REQUEST['priority_id'];
    $addPriority = new Order();
    $priorityData = $addPriority->getDurationandDates($priority_id);
    $response['display_duration']       = '';
    $response['dispatch_interval_dates'] = '';
    $response['arrival_interval_dates']  = '';
    if (!empty($priorityData)) {
        $curDate = date('d F Y');
        $response['msg']= 'Success';
        $response['display_duration']       = $priorityData[0]->display_duration;
        $response['dispatch_interval_dates'] =  date('d F Y', strtotime($curDate. " +{$priorityData[0]->dispatch_interval_days} days"));
        $response['arrival_interval_dates']  =  date('d F Y', strtotime($curDate. " +{$priorityData[0]->arrival_interval_days} days"));


    }
}
else{
    $response['msg']= 'Failed';
}
header('Content-Type: application/json');
echo json_encode($response);
exit;

1 个答案:

答案 0 :(得分:0)

为了使默认情况下选择其中一个选项,您应该包含该特定选项的$backupNumber = fopen("$v", "r+") or die("Unable to open file!"); $i = fread($backupNumber,filesize("invoices/invoice1/backupN.txt")); $i = intval($i); $i = $i + 1; echo $i; fwrite($backupNumber,$i); $a = "invoices/" . $invoiceN . "/backup" . $i; fclose($backupNumber); 属性:

selected

为了澄清,您只需要将<option selected="selected">Some option</option>属性放在默认选项上(将selected替换为您用来确定默认选项的内容):

some condition