Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
HTML
<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>
Posted
Comments
anku.arnab 12-Nov-14 3:13am    
@ManasBhardwaj , @SAKryukov , @DamithSL : Heard a lot about you. Can you help me with this issue..

1 solution

If i were you I would use jQuery..


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

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" />
<script>
$(document).ready(function(){
	$("button").click(function(){
		
		var yr = document.getElementById('entry').value;
		if (yr.length==0) { 
			document.getElementById("result1").innerHTML="";
			document.getElementById("result2").innerHTML="";
			return;
		}
		$.get("db.php?q="+yr,function(data,status){
			if(status=="success")
			{
				var content = data;
				document.getElementById("result").innerHTML = content;
			}
			else
			{
			//ERROR
			}
		});
  });
});
</script>
</head>
<body>
 
<form>
Enter year: <input type="text" id="entry" />
<input type="button" value="check here" onclick="fetchData()" />
</form>
<div id="result">RESULTS</div>
</body>
</html>


db.php FILE:
PHP
<?php
$entry=$_GET["entry"];
$result = "";

// DO SOME WORK
$result = '<div id="result1">RESULT FOR RESULT1</div>';
$result .= '<div id="result2">RESULT FOR RESULT1</div>';


echo $result;
?>



If u still have any doubt do ask>>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900