I was hoping to get some help with a jquery/php problem.
I want to be able to sort users with a series of drop down menus on a form.
To start with I’ll just focus on having one of the drop downs(positions) updating the query.
I want to get a value from a the #positions drop down and then run a mysql query and echo the data using php. I know there are prob a bunch of ways to do this, but I wanted to find a way that is pretty straight forward and simple.
When the page first runs I have php query the database and use a Sprintf function to echo each user’s div
<?php // display user divs
$userQuery = "SELECT * FROM users where user_status='registered';";
$users = mysql_query($userQuery);
// or die (mysql_errno($userQuery));
while ($user = mysql_fetch_array($users)) {
$user_div = sprintf(
"
<div class='user'>
<div class='user_box'>
<a href='profile.php?user_id=%s'><img src='%s'></a>
</div> <!-- close user_box -->
<a href='profile.php?user_id=%s'>
<h3 class='username' >%s</h3>
<h3 class='username' >%s</h3>
</a>
</div> <!-- close user div -->
", $user['user_id'], $user['user_pic_path'], $user['user_id'], $user['first_name'],$user['last_name']);
echo $user_div;
} // end while loop
?>
I was thinking I could use a JQuery script like this to grab the value of the drop down and then run a query with the value pulled.
<script type="text/javascript">
$(document).ready(function() {
$('#position').change(function() {
var queryString = $(this).val();
$.post('scripts/sort_users.php', queryString, processResponse);
});
function processResponse(data) {
$('#resultsGoHere').html(data);
}
});
</script>
Here is my form. Right now I just want to concentrate on getting the positions value, but eventually it will need to query all of the values selected.
<form id='sort_form'>
<ul>
<li>
<input type="checkbox" name="" value="" />
<label class='checkbox' for="">Only Display Users With Photos:</label>
</li>
<li>
<label for="gender">Gender:</label>
<select id=''position name='position' class='right' style="width: 118px;">
<option value="0">Select gender</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</li>
<li>
<label for="specialty">Specialty:</label>
<select name='position' id='position'class='right' style="width: 118px;">
<option value="0">Any</option>
<option value="1">Actor</option>
<option value="2">Actress</option>
<option value="3">Director</option>
<option value="4">Writer</option>
<option value="5">Producer</option>
</select>
</li>
<li>
<label for="Ages">Ages:</label>
<select name='high_ages' class='right'>
<option value="18">80</option>
<option value="19">81</option>
<option value="20">82</option>
</select>
<span> to </span>
<select name='low_ages'class='right' >
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
</select>
</li>
<li>
<label for="distance">Located:</label>
<select name='position' class='right' style="width: 118px; margin-top:0px;">
<option value="0">Any Distance</option>
<option value="10">10 Miles</option>
<option value="20">20 Miles</option>
<option value="30">30 Miles</option>
<option value="50">50 Miles</option>
<option value="100">100 Miles</option>
<option value="250">250 Miles</option>
<option value="500">500 Miles</option>
</select>
</li>
<li >
<label for="distance">From:</label>
<input class='right' type="text" name="zip" placeholder="Zip Code" maxlength="5" size="18"/>
</li>
<li>
<button class="submit" type="submit" value="Update">Update</button>
</li>
</ul>
</form>
here is the php script to process the new query. I’m not sure if I should generate the divs here and send back a string of divs or send back just the results of $users = mysql_query($userQuery);
<?php
require_once 'app_config.php';
require_once 'database_connection.php';
session_start();
$value = $_POST['queryString'];
$userQuery = "SELECT *
FROM haas12_pitchShark.users
where user_status='registered'
and user_pos_id=".$value.";";
$users = mysql_query($userQuery);
echo $users;
?>
I know this is kind of a mess and would appreciate any help.
Here is a link to the page: