doupian9798 2011-02-19 11:47 采纳率: 100%
浏览 52
已采纳

选择下拉值后,使用javascript和php服务器设置文本框值

I am using a form which will retrieve values to set the textbox inputs after a dropdown value is selected. My problem comes from the fact that my php server fnction is designed to take the selected index of dropdown as parameter and return related values to populate the textboxes. I however can't figure out how to combine the php function and the javascript which will do the textbox value setting. Below is my sample code. Any help appreciated.

As can be seen below, displayGamsProjectInfo(fe) function calls the database function ddGamsProjectInfo($gamsId) but how can I can I allow the php function to 'pick' the selected dropdown index without the user POSTing the form values (after maybe button clicked)

javascript/php:

    <script language='javascript'>function displayGamsProjectInfo(fe){
   document.all.FACILITY_RC_ID.value= '';
   document.all.FACILITY_FACULTY_ID.value= '';
   document.all.FACILITY_SUPVSOR_NAME.value= '';
   document.all.FACILITY_SUPVSOR_TELNUM.value= '';
   document.all.FACILITY_SUPVSOR_EMAIL.value= '';
   if(fe.value != null && fe.value != ''){
";
   $getGamsInfo = FacilityDB::getInstance()->***ddGamsProjectInfo()***;
   if($getGamsInfo && $getGamsInfo != null){ 
      if($row = oci_fetch_array($getGamsInfo)){ 
        print "document.all.FACILITY_RC_ID.value= '$row[RES_CENTER]';
        document.all.FACILITY_FACULTY_ID.value= '$row[FACULTY_ID]';
        document.all.FACILITY_SUPVSOR_NAME.value= '$row[STAFF_NAME]';
        document.all.FACILITY_SUPVSOR_TELNUM.value= '$row[STAFF_TELNUM]';
        document.all.FACILITY_SUPVSOR_EMAIL.value= '$row[STAFF_EMAIL]';
";                      
        }
      }                                             
   print "}

}</script>"

php function:

public function ddGamsProjectInfo($gamsId){
if(!isset($gamsId) || $gamsId == '' || $gamsId == null)
   return null;
else{
   $sql = "....";       
   $stmt = oci_parse($this->con, $sql);
   oci_execute($stmt);
   return $stmt;
}

}

  • 写回答

1条回答 默认 最新

  • douqiang6036 2011-02-19 13:06
    关注

    Trying to elaborate more on my comment above...

    Basically, this is what you are trying to do: (Correct me if I'm wrong)

    1. User selects value from a drop-down.
    2. Based on that dropdown value, textboxes are populated with certain values.
      • However you don't want to have an explicit HTTP POST to accomplish this.

    This is possible with PHP and JS, but in more general terms it's possible with Ajax.

    You need to:

    • Write a JS event handler tied to the drop down that does an Ajax POST to a PHP handler, passing in the dropdown value.
    • Write the PHP handler to deal with the dropdown value and return the proper textbox values, preferably in a JS-friendly format like JSON.
    • Write the JS Ajax callback to deal with the return values. This is where you'll populate the textbox values.

    Current JavaScript libraries (jQuery, etc) provide a lot of this functionality. For example, here's jQuery's documentation on its Ajax functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题