从php文件中获取数据并使用javascript打印所选数据 [英] Fetching data from php file and using javascript to print selected data

查看:104
本文介绍了从php文件中获取数据并使用javascript打印所选数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的html页面中有2个div。使用JavaScript,我将查询参数发送到php文件,该文件返回2个div的组合结果。我想知道如何在JavaScript中分离结果并在各自的div中显示。我不想使用AJAX,即使我知道它可以使用。

I have 2 divs in my html page. Using JavaScript, I send my query parameters to a php file, which returns combined results for the 2 divs. I want to know how to separate the result in JavaScript and display in their respective divs. I do not wish to use AJAX, even though I know it can be used.

<script>
function fetchData() {
  var yr = document.getElementById('entry').value;
  if (yr.length==0) { 
    document.getElementById("result1").innerHTML="";
    document.getElementById("result2").innerHTML="";
	return;
  }

  var xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=function() 
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
	 {
		var content = xmlhttp.responseText;
	if (content == "%<searchword>%")
		document.getElementById("result1").innerHTML = content;
	else		
		document.getElementById("result2").innerHTML = content;	
     }
  }
  xmlhttp.open("GET","db.php?q="+ yr ,true);
  xmlhttp.send(); 
}
</script>



<body>

<form>
Enter year: <input type="text" id="entry" />
<input type="button" value="check here" onclick="fetchData()" />
</form>
<div id="result1">result 1 here</div>
<div id="result2"> result 2 here</div>
</body>

推荐答案

如果我是你,我会使用jQuery ..





HTML文件:

If i were you I would use jQuery..


HTML FILE :
<html>
<head>
<title></title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" />
<script>


document )。ready( function (){
(document).ready(function(){


button)。click( function (){

var yr = document .getElementById(' 条目')值。
if (yr.length == 0 ){
document .getElementById( result1)。innerHTML = < span class =code-string> ;
document .getElementById( result2).innerHTML= ;
return ;
}
("button").click(function(){ var yr = document.getElementById('entry').value; if (yr.length==0) { document.getElementById("result1").innerHTML=""; document.getElementById("result2").innerHTML=""; return; }


这篇关于从php文件中获取数据并使用javascript打印所选数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆