Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » php sorting mysql db using html form variable
Closed Thread
Old 04-28-2009, 09:57 AM   #1 (permalink)
ibexpiotr's Avatar
 
True Techie

Join Date: Nov 2003

Posts: 197

ibexpiotr is on a distinguished road

Send a message via AIM to ibexpiotr
Default php sorting mysql db using html form variable

i want to be able to sort a db according to what user selects from a list.
my question is how do i assign users selection from a form to php variable after clicking it?
everything on the same page, no buttons.
then im going to use that php variable to sort db by it.
ibexpiotr is offline  
Old 04-28-2009, 11:14 AM   #2 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: php sorting mysql db using html form variable

You will need Ajax for this.

Something like this:

Code:
<html>
<body>

<script type="text/javascript">
function ajaxFunc()
{
	var ajaxRequest;
	try 
	{
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return false;
			}
		}
	}

	return ajaxRequest;
}

function getData(user)
{	
	var request = ajaxFunc();	

	if (request == null)
	{
		return false;
	}

	request.onreadystatechange=function(){
		if (request.readyState == 4)
		{
			var display = document.getElementById('data');
			display.innerHTML = request.responseText;
		}		
	}
	
	request.open("GET","getuser.php?user="+user,true);
	request.send(null);
}

</script>

<form>

<select name="list" id="list" size="5">
<option value="User1" onclick="getData(this.text);">User1</option>
<option value="User2" onclick="getData(this.text);">User2</option>
<option value="User3" onclick="getData(this.text);">User3</option>
<option value="User4" onclick="getData(this.text);">User4</option>
<option value="User5" onclick="getData(this.text);">User5</option>
</select>

</form>
<div id="data">Select a user from the list</div>

</body>
</html>
PHP Code:
<?php

$db
['host']   = 'localhost';
$db['user']   = 'root';
$db['pass']   = 'root';
$db['dbname'] = 'ajaxtest';

$connect = @mysql_connect ($db['host'],$db['user'],$db['pass']);
$select  = @mysql_select_db ($db['dbname'],$connect);

$user $_GET['user'];

$user mysql_real_escape_string (trim (strip_tags (htmlentities ($user))));

if (!empty (
$user)) {
    
$sql mysql_query ("SELECT * FROM users WHERE username='".$user."'");

    if (
mysql_num_rows ($sql) > 0) {
        while (
$row mysql_fetch_array ($sql)) {
            echo 
$row['username'].'<br />';
        }
    } else {
        echo 
'No user found';
    }
}

?>

__________________

Need website help? PM me!
CrazeD is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Guide to Installing Apache, PHP, MySQL, and PHPMyAdmin on Windows CrazeD Programming Discussions 17 06-17-2009 04:27 PM
Wev Development: How does PHP work? Osiris Programming Discussions 1 01-08-2009 04:31 PM
batch to copy file help under_score Programming Discussions 6 11-14-2008 08:38 AM
Calling all PHP and MySql experts murdocsvan Programming Discussions 4 04-17-2008 01:51 PM
Posting Form data to HTML page kizzeith Programming Discussions 15 02-03-2008 06:54 AM