Use <div> in php page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    Use <div> in php page

    I'm using php,mysql,ajax in my web application. my index.php page has search option. I want the results to display different page(stock.php) but not in index page. When I use <div> in index page it works. But when I put div in different page it doesn't work. Where I goes wrong? Please help me..
    This is my code.
    index.php
    Code:
    <tr>[LEFT]
                  <td  height="30">&nbsp;Year&nbsp;&nbsp;&nbsp;:</td>
                  <td><? getVehicleYear(); ?></td>
                </tr>
                <tr>
                  <td  height="30">&nbsp;Fuel&nbsp;&nbsp;&nbsp;&nbsp;:</td>
                  <td><? getVehicleFuel();?> </td>
                </tr>
    			<tr><td colspan="2"align="right">
    			<input type="image" src="http://bytes.com/images/searchBtn.jpg"  name="Search" id="Search" onclick=" searchVehicle(); return false;" />
    		[/LEFT]
    Ajax code

    Code:
    [LEFT]function searchVehicle(){
    		
    
    	var dropdownIndex = document.getElementById('Year').selectedIndex;
    	var year = document.getElementById('Year')[dropdownIndex].value;
    	
    	var dropdownIndex = document.getElementById('Fuel').selectedIndex;
    	var fuel = document.getElementById('Fuel')[dropdownIndex].value;
    	
    
    	
    				
    	var request = GetXmlHttpObject();
    	var url="searchResult.php";
    	url=url+"?year="+year+"&fuel="+fuel+"&S="+1;
    
    
    	url=url+"&sid="+Math.random();
    	request.onreadystatechange=stateChanged;
    	request.open("GET",url,true);
    	
    	request.send(null);
    
    	function stateChanged()
       {
    	 if (request.readyState==4)  
    		{
    			
    			document.getElementById("t").innerHTML=request.responseText;
    			
    		}
     	}
    }[/LEFT]
    searchResult.ph p

    Code:
    [LEFT]if($_GET['S']==1){
    
    	$year=$_GET['year'];
    	$fuel=$_GET['fuel'];
    
    	
    		$sql="SELECT * FROM vehicles WHERE year='$year' AND fuel='fuel'";
    		$result = mysql_query($sql)or die(mysql_error());
    
    		echo "<table width=100% align=center>";
    	echo "&nbsp;&nbsp;<tr height=20 bgcolor=#C0C0C0><td align=center>Year</td><td align=center>Fuel</td><td>SELECT</td></tr>";
    
    	while($row = mysql_fetch_array($result))
    	{
    		foreach( $row AS $key => $val ){
    		$$key = stripslashes( $val );
    		}
    			
    	echo "<tr class=normalText>";
     
    	echo "<td> $year</td>";
    	echo "<td> $fuel</td>";
    	
    
    	echo  "<td><a href='javascript:void(0);' onclick=\"LookupVehicle('$year','$fuel'); return false;\">Select</a></td>";
    			echo "</tr>";
    	}
    	echo "</table>";
    	
    	}[/LEFT]
    stock.php
    Code:
    <div id="t"></div>
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Javascript locate object only in the current page. It does not look for a object in different page. With some exception. your div object with ID T is in different object.

    I am not sure what are you trying to achieve, I didn't even tried to read. But If you want to change the entire page use
    Code:
    document.location='stock.php?data1=data&data2=data&data3=data'
    But if you dont want to use get method. try different approaches

    but you must remember your Javascript wont get object if it does not exists in your current page. and an object can be created or loaded using javascript from server

    Comment

    • ghjk
      Contributor
      • Jan 2008
      • 250

      #3
      Thank you johny10151981. Your code is working. Actually In my web application 3 or more pages having same search function. But I want to display search results in one page not in own page.When user search 1st page; search results will displays on result page. 2nd page also results should be on results page. Using your code it goes to the results page . But I couldn't get results. Please help me...
      ajax code
      Code:
          function searchVehicle(){
           
           
              var dropdownIndex = document.getElementById('Year').selectedIndex;
              var year = document.getElementById('Year')[dropdownIndex].value;
           
              var dropdownIndex = document.getElementById('Fuel').selectedIndex;
              var fuel = document.getElementById('Fuel')[dropdownIndex].value;
           
           
           
           
              var request = GetXmlHttpObject();
              var url="searchResult.php";
              url=url+"?year="+year+"&fuel="+fuel+"&S="+1;
           
           
              url=url+"&sid="+Math.random();
              request.onreadystatechange=stateChanged;
              request.open("GET",url,true);
           
              request.send(null);
           
              function stateChanged()
             {
               if (request.readyState==4)  
                  {
           
      document.location='stock.php?year=year&fuel=fuel';
           
                  }
               }
          }
      This code in my stock.php page
      Code:
      if($_GET['S']==1){
      
      	$year=$_GET['year'];
      	$fuel=$_GET['fuel'];
      echo $year;
      }
      Last edited by Niheel; Aug 20 '11, 08:21 PM. Reason: fixed unclosed code tag

      Comment

      Working...