SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    table instead of dropdown

    In the below code I have a field to enter a search string, and onblur the dropdown fills with owner names that starts with the search string. Then after selecting the correct owner from dropdown the ownerid,petowner,ostreet fields are auto filled using ajax. All this works great right now. My question is:
    How would I replace the dropdown with a table to select the owner? Still having the search string field. I know I would need some sort of onclick on a table row. But how would I use a table there instead of the dropdown?

    Here are the three main files I am using:
    edit_lookup.php
    Code:
    <?php include 'include/dbconfig.inc.php'; ?> 
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    	body{
    		background-repeat:no-repeat;
    		font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;
    		height:100&#37;;
    		background-color: #FFF;
    		margin:0px;
    		padding:0px;
    		background-image:url('/images/heading3.gif');
    		background-repeat:no-repeat;
    		padding-top:85px;
    	}
    	fieldset{
    		width:500px;
    		margin-left:10px;
    	}
    	</style>
    
    
    
    
    <script language="javascript" type="text/javascript">
    // Roshan's Ajax dropdown code with php
    // This notice must stay intact for legal use
    // Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
    // If you have any problem contact me at http://roshanbh.com.np
    function getXMLHTTP() { //fuction to return the xml http object
    		var xmlhttp=false;	
    		try{
    			xmlhttp=new XMLHttpRequest();
    		}
    		catch(e)	{		
    			try{			
    				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch(e){
    				try{
    				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    				}
    				catch(e1){
    					xmlhttp=false;
    				}
    			}
    		}
    		 	
    		return xmlhttp;
        }
    function getowner() {		
    		var osch = document.getElementById('lookup').value
    		// alert(osch);
    		var strURL="findowner.php?owner="+osch;
    		var req = getXMLHTTP();
    		
    		if (req) {
    			
    			req.onreadystatechange = function() {
    				if (req.readyState == 4) {
    					// only if "OK"
    					if (req.status == 200) {						
    						document.getElementById('ownerIDdiv').innerHTML=req.responseText;						
    					} else {
    						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
    					}
    				}				
    			}			
    			req.open("GET", strURL, true);
    			req.send(null);
    		}		
    	}
    
    
    
    
    </script>
    
    	<script type="text/javascript" src="AJAX/ajax.js"></script>
    	<script type="text/javascript">
    		
    	var ajax = new sack();
    	var currentOID=false;
    	function getownerData()
    	{
    		var OID = document.getElementById('OID').value.replace(/[^0-9]/g,'');
    		
    		if(OID.length>0 && OID!=currentOID){
    		// if(clientId.length==4 && clientId!=currentClientID){
    			currentOID = OID;
    			// alert(currentOID);
    			ajax.requestFile = 'getowner.php?getownerID='+currentOID;	// Specifying which file to get
    			ajax.onCompletion = showownerData;	// Specify function that will be executed after file has been found
    			ajax.runAJAX();		// Execute AJAX function			
    		}
    	}
    	function showownerData()
    	{
    		var formObj = document.forms['ownerForm'];	
    		eval(ajax.response);
    	}
    	
    	</script>
    
    </head>
    <body>
    <?php 
    /* if (isset($_POST['saveUpdate']))
    {
    	if (!get_magic_quotes_gpc())
    		{
    			$usql = "UPDATE pets";
    			$usql = $usql . " SET address = '".$db->hscadd(strtoupper($_REQUEST['address']))."'";
    			// $usql = $usql . " ostreet = '".$db->hscadd(strtoupper($_REQUEST['ostreet']))."'";
    			$usql = $usql . " WHERE clientID = '" .($_REQUEST['clientID'] + 0) . "'";
    		}
    	else
    		{
    		 	$usql = "UPDATE pets";
    			$usql = $usql . " SET address = '".$db->hsc(strtoupper($_REQUEST['address']))."'";
    			// $usql = $usql . " ostreet = '".$db->hsc(strtoupper($_REQUEST['ostreet']))."'";
    			$usql = $usql . " WHERE clientID = '" .($_REQUEST['clientID'] + 0) . "'"; 
    		}
    		$db->query($usql);
    		echo $usql;
    } */
    
    
    
    if (isset($_POST['testUpdate']))
    {
      echo "you clicked test";
    }
    
    $pets_Res = $db->query("SELECT * FROM pets WHERE petid = '3'");
    			$pets_Row = $db->fetch($pets_Res);
    
    
    
    $db->closedb();
    ?>
    <form name="ownerForm" action="edit_lookup.php" method="post">	
    	<fieldset>
    		<legend>pet information</legend>
    		<table>
    			
    
    <tr>
    		<td>petname:</td>
    		<td>
    			<input type="text" name="petname" value="<? echo $pets_Row['petname'];?>">
    		</td>
    	</tr>
    	<tr>
    		<td>species:</td>
    		<td>
    			<input type="text" name="species" value="<?=$pets_Row['species'];?>">
    		</td>
    	</tr>
    	<tr>
    		<td>sex:</td>
    		<td>
    			<input type="text" name="sex" value="<?=$pets_Row['sex'];?>">
    		</td>
    	</tr>
      
      
      
      <tr>
    				<td><label for="lookup">search string:</label></td>
    				<td><input type="text" name="lookup" id="lookup" size="20" onblur="getowner();">search string</td>
    			</tr>
    			<tr style="">
    			    <td>owner</td>
    			    <td ><div id="ownerIDdiv"><select name="OID" id="OID"  >
    				<option>Select owner</option>
    			        </select></div></td>
    			
    			</tr>
      
      
      <tr>
        <td width="80">ownerid</td>
        <td width="431"><input type="text" value="<?php echo $pets_Row['ownerid'] ?>" name="ownerid">
    	</td>
       
      </tr>
      
      
       
      <tr>
    		<td>petowner:</td>
    		<td>
    			<input type="text" name="petowner" value="<?=$pets_Row['petowner'];?>">
    		</td>
    	</tr>
    	<tr>
    		<td>ostreet:</td>
    		<td>
    			<input type="text" name="ostreet" value="<?=$pets_Row['ostreet'];?>">
    		</td>
    	</tr>
    	<tr>
    		<td>dogpic:</td>
    		<td>
    			<input type="text" name="dogpic" value="<?=$pets_Row['dogpic'];?>">
    		</td>
    	</tr>
    
    		
    <!-- =============================================================== -->		
    
    		</table>	
    	<input type="submit" name="testUpdate" value="test">
    	<input type="submit" name="saveUpdate" value="Update">
    	
    	</fieldset>
    	</form>
    		
    </body>
    </html>
    findowner.php
    Code:
    <!--//---------------------------------+
    //  Developed by Roshan Bhattarai    |
    //	http://roshanbh.com.np           |
    //  Contact for custom scripts       |
    //  or implementation help.          |
    //  email-nepaliboy007@yahoo.com     |
    //---------------------------------+-->
    <?php
    #### Roshan's Ajax dropdown code with php
    #### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
    #### if you have any problem contact me at http://roshanbh.com.np
    #### fell free to visit my blog http://roshanbh.com.np
    ?>
    
    <?php
    $oid=$_GET['owner'];
    $t1 = $oid;
    $link = mysql_connect('localhost', 'root', 'sa'); //changet the configuration in required
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('petback');
    $query="SELECT ownerid, oname, ostreet FROM powners";
    $query = $query . " WHERE oname Like '".addslashes("$t1%")."'";		   
    $query = $query . " ORDER BY oname asc ";
     
    $result=mysql_query($query);
    
    ?>
    <select name="OID" id="OID" onchange="getownerData();">
    <option>Select owner</option>
    <?php
    while($row=mysql_fetch_array($result))
    {
    echo("<option value=\"" . $row[0] . "\">" . $row[1].'   '.$row[0] . "</option>\n");
    } 
    
    echo "</select>";
    
    ?>
    getowner.php
    Code:
    <?php
    /* Replace the data in these two lines with data for your db connection */
    $connection = mysql_connect("localhost","root","sa");  
    mysql_select_db("petback",$connection);
    
    if(isset($_GET['getownerID']))
    {  
      $res = mysql_query("select * from powners where ownerid='".$_GET['getownerID']."'") or die(mysql_error());
      if($inf = mysql_fetch_array($res))
      {
        echo "formObj.ownerid.value = '".$inf["ownerid"]."';\n";    
        echo "formObj.petowner.value = '".$inf["oname"]."';\n";    
        $vaddress = addslashes($inf["ostreet"]);
    	
    	// echo "formObj.address.value = '".stripslashes($inf["address"])."';\n";    
        echo "formObj.ostreet.value = '".$vaddress."';\n";   
    	
    	
        
      }else{
        echo "formObj.ownerid.value = '';\n";    
        echo "formObj.petowner.value = '';\n";    
        echo "formObj.ostreet.value = '';\n";    
           
      }    
    }
    ?>
    Last edited by jim9; May 17, 2010 at 15:15. Reason: spell


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •