weixin_33737774 2013-12-22 06:41 采纳率: 0%
浏览 33

Ajax PHP HTML表单

I am trying to retrieve multiple rows of data from my database. The number of rows that will be returned is unknown. It works for one row returned. But i also have scenarios where multiple rows are returned to the html page where the ajax function adds the corresponding value to the form.

What is the best way to handle unknown number of rows in an html form? Also is the form a good idea?

I was trying to figure out a way to have the php scipt build the html code and pass it back using ajax. But have been unable to find any sort of examples online.

In the function you can see that I take the data and equate it to the form. The form is predefined to have one entry. but i need to know how to modify the form to know how many records will be returned. I hope that makes sense. Thank you in advanced.

CODE:

function getFunction(){
    //browser support code
    var ajaxRequest; // The variable to create the ajax request
    try {
          // Opera 8.0+, Firefox, Safari Support
          ajaxRequest = new XMLHttpRequest();
    } catch (e){
          try{
                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try{
                                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e){
                                // Something went wrong with the browser support
                                alert("Your browser broke!");
                                return false;
                        }
                }
        }
        // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
          if(ajaxRequest.readyState == 4){
            // document.write(ajaxRequest.responseText);
            var response = JSON.parse(ajaxRequest.responseText);
            var pfullname=response.pfirstname + " ";
            var dfullname=response.dfirstname + " ";
            if (response.pmiddlename!=null) {pfullname = pfullname + response.pmiddlename + " ";}
            pfullname = pfullname + " " + response.plastname;
            document.info.name.innerHTML = pfullname;
            //document.info.address.value = ajaxRequest.responseText;
            document.info.dob.innerHTML = response.dob;
            document.info.address.innerHTML = response.paddress;
            document.info.phonenumber.innerHTML = response.phonenumber;
            document.info.sex.innerHTML = response.sex;
            document.info.occupation.innerHTML = response.occupation;
            if (response.dmiddlename!=null) {dfullname = dfullname + response.dmiddlename + " ";}
            dfullname = dfullname + response.dlastname;
            document.info.doctorp.innerHTML = dfullname;
            document.appointment.locationa.innerHTML = response.alocation;
            document.appointment.datea.innerHTML = response.adate;
            document.appointment.doctora.innerHTML = dfullname;
          }
        }
        //var testname = document.getElementById('testname').value;
        var healthid = document.getElementById('healthid').value;
        //var queryString = "?testname=" + testname + "&testpassword=" + testpassword;
        var queryString = "healthid=" + healthid;
        //document.write(queryString);
        ajaxRequest.open("POST", "getnum.php", true);
        ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        ajaxRequest.send(queryString);

}

PHP CODE:

query($sql)){ while($row = $result->fetch_object()){ echo json_encode($row); } } else{ //error occurred echo 'error:'.$con->error; } mysqli_close($con); ?>

HTML CODE:

    <h2>
      <span style="font-weight:bold;">Patient Information:</span>
      <form name='info'>
        Name:           <output type='text' name="name" id="name"> </output><br>
        DOB:            <output value='' type='text' name="dob" id="dob"> </output><br>
        Address:        <output type='text' name="address" id="address"> </output><br>
        Phone Number:   <output type='text' name="phonenumber" id="phonenumber"> </output><br>
        Sex:            <output type='text' name="sex" id="sex"> </output><br>
        Occupation:     <output type='text' name="occupation" id="occupation"> </output><br>
        Doctor:         <output type='text' name="doctorp" id="doctorp"> </output><br>
        Doctor Address: <output type='text' name="doctoradd" id="doctoradd"> </ouput><br>
      </form>
    </h2>
  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 GEO下载数据的处理报错 :函数‘Meta’标签‘"data.frame"’找不到继承方法,如何解决?
      • ¥15 DLNM模型是否可以用二分类变量作为y变量
      • ¥15 android object box 一个实体多个表怎么写
      • ¥15 temux 启用docker 服务失败
      • ¥15 Flask 使用celery发送邮件出现‘目标计算机积极拒绝‘
      • ¥60 老人用的sd卡在手机里面不知道操作了什么,导致图片和视频变成了文件,取下sd卡连接电脑就是图中的样子,后缀改为.jpg才可以,需要用系统的画图软件才能打开,文件属性还是文件,有没有批量操作的解决办法
      • ¥15 超时跳出方法代码的返回值问题
      • ¥15 汇编语言程序设计设计,ascii码求数,再求数的BCD码
      • ¥30 Mask rcnn训练自己的数据集出现问题!
      • ¥20 研究人工智能时的几个问题