duanke3985 2016-06-27 12:09
浏览 164
已采纳

实时更新php变量并同时显示文本框中的值

This is my table row click function in the file, 'BAConsult.php'. On click, showconsultationdata function will run.

$(document).ready(function(){ //table row click
  }).on('click','.consultclick tr',function(e){   
      if(e.target.tagName === "TD"){
          $(".consultclick tr").removeClass("highlight");
    $(e.target).parent().addClass("highlight");    
      }      
    var dateconsulted = $(this).attr('value'); 
    alert(dateconsulted);
    showconsultationdata(dateconsulted);
});

This is my ajax script

function showconsultationdata(str) { 
if (str == "") {
    document.getElementById("txtHint2").innerHTML = "";
    return;
} else { 
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint2").innerHTML = xmlhttp.responseText; 

        }
    };
    xmlhttp.open("GET","BAConsultRecordsAJAX.php?q="+str,true);
    xmlhttp.send();
}
}

Here is another php file called 'BAConsultRecordsAJAX.php' where i placed the ajax of the showconsultationdata.

session_start();
require('Config/Setup.php');
$q = $_GET['q']; 
$consult="SELECT * FROM Counsel where nric='$_SESSION[nric]' and dateconsulted='$q'";

$consultresult = mysqli_query($dbconn,$consult);

while($row = mysqli_fetch_array($consultresult)) {
$skincareremarks=$row['skincareremarks'];
$skinconditionremarks=$row['skinconditionremarks'];
}

On table row click, $skincareremarks and $skinconditionremarks should be updated. And these values will show up in the textboxes in the 'BAConsult.php' page. How can i do this?

  • 写回答

1条回答 默认 最新

  • dongliehuan3925 2016-07-06 15:30
    关注

    So, i followed @Jeff's method by using JSON. However, I realised that the xmlhttp.responseText wasn't only showing my JSON encoded code, but also my javascript which was why the JSON.parse method was unable run properly. I then did the following:

    In my BAConsultRecordsAJAX.php file, i did this.

    echo "<div id='test1'>";
    echo json_encode(array('first'=>$skincareremarks,'second'=>$skinconditionremarks));
    echo "</div>";
    

    I gave this output a div called 'test1'.

    Then, in my main file's AJAX script, i did this.

    var a = JSON.parse($(xmlhttp.responseText).filter('#test1').html());    
    document.getElementById("test").value=a.first;
    

    So basically, it filters out the rest of the xhtmlhttp.responseText outputs, and selects only the contents in the div where id='test1'.

    Hope this helps those who have this problem too..

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

报告相同问题?

悬赏问题

  • ¥15 为什么eprime输出的数据会有缺失?
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题