如何让按钮自动将数据库中的值输入表单?

时间:2015-07-06 11:05:42

标签: php html ajax database

这是页面的图像,

enter image description here

顶部的2个大蓝色按钮是表示数据库中值的按钮。此按钮的主要功能是帮助用户以方便的方式填写表格,只留下“描述”。

这是我的页面代码,

<div class="bodycontainer">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title">My Records</h3>
    </div>
    <div class="panel-body">
      <?php
		require 'dbfunction.php';

		$con = getDbConnect();
		$day = date("l");

		if (mysqli_connect_errno($con)) {
			"Failed to connect to MySQL: " . mysqli_connect_error();
		} else {
			$result = mysqli_query($con, "SELECT * FROM timetableschedule WHERE day='" . $day . "'");

			while ($schedule = mysqli_fetch_array($result)) {
				?>
      <div class="col-md-4">
        <div class="admininfobox">
        <a class="btn btn-primary">
          <?php
		echo "<br/>";
		echo $schedule['academicInstitution'] . "<br />";
		echo $schedule['startTime'] . "-" . $schedule['endTime'] .  "hrs<br />";

		echo "<br/>";
		?>
        </a>
        </div>
      
      </div>
      <?php
		}
		mysqli_close($con);
	}
	?>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading">Record Activity</div>
    <div class="panel-body">
      <form name="Create New Admin"  class="form-horizontal" method="post" action="handlerecord.php">
        <div class="form-group">
          <div class="col-sm-4">
            <label>Academic Institution</label>
            <input list="AcadInst" type="text" class="form-control" placeholder="Institution Name" name="academicInstitution">
            <datalist id="AcadInst">
              <option value="Singapore Polytechnic (SP)">
              <option value="Ngee Ann Polytechnic (NP)">
              <option value="Temasek Polytechnic (TP)">
              <option value="Republic Polytechnic (RP)">
              <option value="Nanyang Polytechnic (NYP)">
              <option value="Others (Please specify)"> 
            </datalist>
          </div>
          <div class="col-sm-4">
            <label>Level of Teaching</label>
            <input list="LvTeaching" type="text" class="form-control" placeholder="Teaching Stage" name="levelofteaching">
            <datalist id="LvTeaching">
              <option value="Undergraduate Teaching">
              <option value="Postgraduate Teaching">
              <option value="Continuing Education">
              <option value="Others (Please specify)"> 
            </datalist>
          </div>
          <div class="col-sm-4">
            <label>Type of Teaching</label>
            <input list="TyTeaching" type="text" class="form-control" placeholder="Teaching Type" name="typeofteaching">
            <datalist id="TyTeaching">
              <option value="Clinical Teaching">
              <option value="Academic Teaching">
              <option value="Talk">
              <option value="Others (Please specify)"> 
            </datalist>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-4">
            <label for="startdate">Start Time</label>
            <input type="text"  class="form-control" placeholder="Select Time" name="starttime">
          </div>
          <div class="col-sm-4">
            <label for="enddate">End Time</label>
            <input type="text"  class="form-control" placeholder="Select Time" name="endtime">
          </div>
          <div class="col-sm-4">
            <label for="enddate">Description</label>
            <input type="text"  class="form-control" placeholder="Optional" name="Description">
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-4">
            <input type="submit"  value="Add" class="btn btn-primary">
            </input>
          </div>
        </div>
      </form>
    </div>
  </div>

数据库概述, enter image description here

我不确定如何编码。喜欢一旦用户点击按钮,按钮就会通过数据库并相应地将数据输入到表单中,供用户提交。

2 个答案:

答案 0 :(得分:0)

我的建议是点击创建一个AJAX请求。

  1. 创建页面loadFormData.php
  2. 接受关键数据以在数据库中搜索。例如:id。然后使用此键在数据库中搜索并获取结果。最后将其编码为json(json_encode())并回显结果。

    1. 当用户点击按钮时,触发ajax(Javascript)函数并将键值发送到loadFormData.php
    2. 从返回的json结果中,提取机构名称,开始时间,结束时间等,并使用JavaScript填写此表单字段

      $("#blueButton").click(function() {
      
          $.ajax({
            type: "GET",
            url: "loadFormData.php",
            dataType: "json",
            success : function(data) {
                  // extract each item from the variable 'data'
            }
      });
      });
      

答案 1 :(得分:-1)

您应该创建一个表单并使用操作来说明输入所需值的方法。

例如

http://www.w3schools.com/php/php_forms.asp

您可以使用它轻松输入数据。