繁体   English   中英

使用带有数据库信息的下拉菜单填充文本框

[英]populating text boxes using a drop down menu with database information

大家好,我目前正在尝试弄清楚如何在用户从下拉菜单中选择“日期”后使用数据库中的信息填充文本框。

我的下拉菜单当前由游览的离开日期填充,我希望与该日期相关的所有其他信息都显示在页面上的文本框中。

这是针对我正在处理的项目,不幸的是,我们当前的服务器不支持 pdo。

这让我头晕目眩,我无法思考我应该如何实现这一目标。 在互联网上搜索并没有给我任何有用的信息。

这是我的选择框和文本框的 html 代码。

<div>

  <label><strong>Search Tours</strong></label>
<form>
  <p>
    <select name="lst_tour">
      <option value="">Select a Tour:</option>
      <?php 

foreach ( $results as $option ) : ?>
      <option value="<?php
     
          echo $option->departure_date; ?>"><?php echo $option->departure_date; ?>    </option>
      <?php endforeach; ?>
    </select>
  </p>
  <p><strong>List of Tour Details</strong></p>


        <input name="txt_tourname" type="text" id="txt_tourname" readonly="readonly"     value = <?php echo $ltour_name ?> />
      </label></td>

        <input name="txt_departuredate" type="text" id="txt_departuredate"  readonly="readonly" />
      </label>


        <input name="txt_tourdetails" type="text" id="txt_tourdetails"     readonly="readonly" />
      </label>

这是我的 php 连接代码

     <?php
session_start();

        $server = "server";
        $schema = "schema";
        $uid = "name";
        $pwd = "pass";

    $tour_name =$_POST["txt_tourname"];
    $departure_date =$_POST["txt_departuredate"];
    $tour_details =$_POST["txt_tourdetails"];
    $no_of_volunteers =$_POST["txt_noofvolunteers"];


mysql_connect($server , $uid , $pwd) or die ("server not found");
mysql_select_db($schema) or die ("Database not found");

     $sql = "SELECT * FROM tour";
     $query = mysql_query($sql);
     while ( $results[] = mysql_fetch_object ( $query ) );
     array_pop ( $results );
?>

任何帮助都会很棒。

好吧,这个问题被问过和回答过很多,大多数问的人都有一个概念问题。 我将分部分解释这一点,然后我将发布对您的代码的干净重写,包括一个 javascript 文件供您深入学习

  • 您尝试做的事情背后的想法涉及看起来最有可能是静态 html 页面的某种行为
  • Javascript将行为添加到您原本无效的 html 中,允许您触发事件并生成响应
  • Ajax 的诞生是出于集体需要,能够在用户未离开页面时与服务器对话。 它允许我们在幕后(异步)发出请求并带回信息
  • 行为异步请求相结合的能力是当今富互联网应用程序(简称 RIA?)的基础。

好吧,不要只看表面价值,对它做一些研究,你就会发现它的潜力。 与此同时,我会创建一个你想要的样子的模型,我会尽快回来 寻找其他答案,周围有很多知识渊博的人^^

编辑

html表单

<form method="somemethod" action="somescript.php">
  <select name="lst_tour" id="lst_tour">
    <option value="">Select a Tour:</option>
    <?php foreach ( $results as $option ) {
      ?><option value="<?php echo $option->departure_date; ?>"><?php echo $option->departure_date; ?></option><?php
    } ?></select>
  <!-- these will be "magically" populated, they conveniently have ids ^^ -->
  <input name="txt_tourname" type="text" id="txt_tourname" readonly="readonly" />
  <input name="txt_departuredate" type="text" id="txt_departuredate" readonly="readonly" />
  <input name="txt_tourdetails" type="text" id="txt_tourdetails" readonly="readonly" />
</form>

javascript

大量的编辑和重写。 这是一个包含大量alerts的嘈杂脚本,因此请耐心等待,并在您不再需要时按顺序开始删除警报。 注意: select 标签有一个 id ,我用它来查找并附加事件处理程序

(function(){
  var
    // the php script that's taking care of business
    url = 'http://path/to/handling/ajaxscript.php',
    select,
    // assume these are the ids of the fields to populate
    //  AND assume they are the keys of the array thats comming from the server
    inputs = ['txt_tourname','txt_departuredate','txt_tourdetails'],
    // the XMLHttpRequest, I'm not recycling it but, oh well
    xhr,
    // the onReadyStateChange handler function, it needs access to xhr
    xhrRSC,
    // event handler, called for <select>."onChange"
    onChooseDate,
    // response handler that will be executed once the xhrRSC deems it ready
    populateData,
    // convenient event handlers
    onLoad, onUnload;
  xhrRSC = function () {
    if (xhr && xhr.readyState !== 4) {return;}
    alert('the server response has completed. Now continue with the population');
    populateData(JSON.parse(xhr.responseText));
    xhr = null;
  };
  onChooseDate = function () {
    var date = select.options[select.selectedIndex].value;
    alert('I have been changed. Did I select the right date: '+date
      +'. Now we send some info to the server');
    // AJAX: make xhr
    xhr = new XMLHttpRequest();
    // AJAX: setup handler
    xhr.onreadystatechange = xhrRSC;
    // AJAX: open channel
    xhr.open('get',url+'?ajax=1&date='+date,true);
    // AJAX: send data (if method post)
    xhr.send(null);
    // if we had jQuery, we could condense all this into one line
    // $.post(url,{ajax:1,date:date},populateData,'json');
  };
  populateData = function (json) {
    // we have the content from the server. Now json decode it
    alert('json data => '+json.toSource());
    // foreach input id execute function
    inputs.forEach(function(v){
      // set the value of each input to the data sent by the server
      alert('setting input "'+v+'" to "'+json[v]+'"');
      document.getElementById(v).value = json[v];
    });
  };
  onLoad = function () {
    alert('page has loaded');
    // assume the <select> tag has an id of "lst_tour", just as it's name
    select = document.getElementById('lst_tour');
    // the "change" event is fired when the user changes the selected <option>
    select.addEventListener('change',onChooseDate,false);
  };
  onUnload = function () {
    select.removeEventListener('change',onChooseDate,false);
    select = null;
  };
  window.addEventListener('load',onLoad,false);
  window.addEventListener('unload',onUnload,false);
}());

ajax 脚本,php 处理程序

<?php
// this file has $_GET populated with 'ajax' => '1' and 'date' => 'what the user chose'
if (!isset($_GET['ajax'])) die('this is not how its supposed to work');
// we must protect the output
ob_start();
// your initializers
// your logic: it should make the selection, then populate the array to be JSON encoded

$response = array(
  'txt_tourname' => $txt_tourname,
  'txt_departuredate' => $txt_departuredate,
  'txt_tourdetails' => $txt_tourdetails
);
// you may want to log this for debugging
// server output has been protected
ob_end_clean();
header('Content-Type: text/json');
echo json_encode($response);
// the client has what he wanted
exit;

就是这样,它没有经过测试,只是一小部分,但是通过仔细审查,您会学到很多东西。 另请阅读Crockford并考虑使用 jQuery 的好处,使用 jQuery 时 javascript 本来可以简单得多,并且效率更高

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM