如何使用隐藏字段以编辑形式显示所选值

时间:2016-10-07 07:47:05

标签: php jquery mysql

这里我从数据库中获取所有数据并以表格格式显示。之后我单击编辑按钮意味着我想显示已经给出的值,我希望在文本框中显示的值(),正常如文本框表示我可以显示值,但选项值表示我无法在该字段中显示 选定的

<!--Edit part function here-->
<script>
	function show_edit_menu(id, t_name, state_id, city_id) {
    $("#show_edit_menu").show();

    $("#tid").val(id); //truck auto id
    $("#tname").val(t_name); //Truck Name
    $("#state").val(state_id); //state id
    $("#city").val(city_id); //city id

  }
</script>	
	
<script>
function edit_menu() {
  var name = $("#tname").val(); //truck name
  var tid = $("#tid").val(); //truck Id
  var state = $("#state").val(); //state
  var city = $("#city").val(); //city
  //var gender=$('input[name=egender]:checked', '#edit_form').val();//gender ethu check agirukunu pakarathu
  console.log(name); //here i can get the name
  console.log(state); // here i can't get the state name
  $.ajax({
    type: "POST",
    url: "ajax_edit_pgs.php",
    data: {
      name: name,
      tid: tid,
      state: state,
      city: city
    },
    success: function(data) {
      console.log(data)
      if (data == "success") {
        //window.location = 'dashboard.php';
      } else {
        $('#espan-error').html('Failed to Enter the Location');
      }
    }
  });

}

	</script>
<div class="row">
  <div class="col-lg-12">

    <!-- Start jQuery Datatable -->
    <div class="portlet" style="margin-bottom:0px;">
      <div class="portlet-heading dark">
        <div class="portlet-title">
          <h4></h4>
        </div>

        <div class="clearfix"></div>
      </div>
    </div>

    <div class="well white">
      <table id="SampleDT" class="datatable table table-hover table-striped table-bordered tc-table">
        <thead>
          <tr>
            <th data-class="expand">Truck Names</th>
            <th data-hide="phone,tablet">State</th>
            <th data-hide="phone,tablet">City</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
          <?php
            $sql = "SELECT * FROM trucks WHERE status !='1' ORDER BY reg_date DESC";
            $result = mysql_query($sql);
            $cat=array();
            while($row = mysql_fetch_array($result))
            {
              echo '<tr>
                  <td>'.$row["truck_name"].'</td>
                  <td>'.Getstate($row["state_id"]).'</td>
                  <td>'.Getcity($row["city_id"]).'</td>
                  
                  <td class="col-medium center">
                  <div class="btn-group btn-group-xs ">
                  <a class="btn btn-inverse" style="cursor:pointer;" onclick="show_edit_menu(\''.$row["id"].'\',\''.$row["truck_name"].'\',\''.$row["state_id"].'\',\''.$row["city_id"].'\')"><i class="fa fa-edit icon-only"></i>Edit</a>
                  <a class="btn btn-danger" onclick="prmpt_deletemenu('.$row["id"].')"><i class="fa fa-times icon-only"></i>Delete</a>
                  </div>
                  </td>
                </tr>';
            }
            
            ?>

        </tbody>
      </table>
      <p id="error" style="text-align: center;font-size: 14px;color: #f00;font-weight: bold;display:none;"></p>
    </div>
    <!-- End jQuery Datatable -->

    <!-- END YOUR CONTENT HERE -->

  </div>
</div>


<!--edit part here-->
<div class="portlet" id="show_edit_menu" style="display:none;">
  <div class="portlet-heading dark">
    <div class="portlet-title">
      <h4>Edit Menu</h4>
    </div>
    <div class="portlet-widgets">
      <a data-toggle="collapse" data-parent="#accordion" href="#f-3"><i class="fa fa-chevron-down"></i></a>
    </div>
    <div class="clearfix"></div>
  </div>
  <div id="f-3" class="panel-collapse collapse in">
    <div class="portlet-body">
      <form class="form-horizontal" role="form" id="edit_form" method="post" enctype="multipart/form-data">

        <div class="form-group">
          <label class="col-sm-2 control-label">Truck Name<span class="require">*</span></label>
          <div class="col-sm-8">
            <input type="text" class="form-control" value="" name="tname" id="tname">
            <input type="hidden" class="form-control" value="" name="tid" id="tid">
          </div>
        </div>

        <div class="form-group">
          <label class="col-sm-2 control-label">State<span class="require">*</span></label>
          <div class="col-sm-8">
            <select name="state" id="state" onchange="getCity(this.value);" class="form-control intro-form-fixer" required="" id="state" name="state" data-msg-required="Please enter your State" value="" aria-required="true">
            <!--<option value="">Select State</option>-->
            <?php
            $sql = mysql_query("SELECT * FROM state_list");
            while($row=mysql_fetch_assoc($sql)){
            ?>
            <option value="<?php echo $row['id'];?>"><?php echo $row['state'];?></option>
            <?php } ?>
          </select>
            <input type="hidden" id="state" name="state" value="" />
          </div>
        </div>

        <div class="form-group">
          <label class="col-sm-2 control-label">City<span class="require">*</span></label>
          <div class="col-sm-8">
            <select class="form-control intro-form-fixer city" autocomplete="off" name="city" id="city" style="width:100%;">
               <option value="">Select Area</option>
            </select>
            <input type="hidden" id="city" name="city" value="" />
          </div>
        </div>

        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <div class="tcb">
              <label>
                <span id="espan-error" style="color:#f00;"></span>
              </label>
            </div>
          </div>
        </div>
        <div class="form-actions">
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button type="button" class="btn btn-primary" onclick="edit_menu()">Submit</button>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>
function Getstate($id)
{
	$f="SELECT * FROM state_list WHERE id='$id'";
	$rr=mysql_query($f);
	while($row=mysql_fetch_array($rr))
		{
			$state = $row['state'];
		}
		return $state;
}

function Getcity($id)
{
	$f="SELECT * FROM city_list WHERE id='$id'";
	$rr=mysql_query($f);
	while($row=mysql_fetch_array($rr))
		{
			$city = $row['city_name'];
		}
		return $city;
}

0 个答案:

没有答案