I have been using this code to display some testimonials in my website.
This is code so far :
// Select testimonials from database
$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type
FROM testimonials
INNER JOIN city ON city.city_id = testimonials.city_id
ORDER BY date_added DESC LIMIT $start, $display";
$r = mysqli_query($dbc, $q);
if ( $records >= 1 ) {
while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC)) {
$testimonial = $row['testimonial'];
//echo $testimonial;
$mytestimonial = nl2br($testimonial);
$city = $row['city_name'];
$name = $row['name'];
$url = $row['web_address'];
$imageName = $row['image_name'];
$type = $row['membership_type'];
echo '<div class="testimonial-row">
<div class="testimonial-image">
<img src="'.UPLOAD_DIR.$imageName.'" />
</div>
<div class="testimonial">
<h2>'.$name.'</h2>
<h3>';
if($type==1){
echo 'A teacher';
}elseif($type==2){
echo 'An Institute';
}elseif($type==3){
echo 'A Student';
}
echo " from <strong>$city</strong></h3>
<blockquote>$mytestimonial</blockquote>
<p class='user-url'><a href=''>$url</a></p>
</div>
</div>";
}
} else {
echo "There is no any testimonial to display at this time. Please try again later.";
}
After running above code I can display all my testimonials. I have 3 different kind of testimonials and at the moment display all kind of testimonials together in my page.
Now I am going a solution with a select box to filter and display them according to its type.
This is my select box :
<select class="select" name="type">
<option value="1">Tutor</option>
<option value="2">Institute</option>
<option value="3">Student</option>
</select>
When selecting an option from select box my filtering should happen with requering my original query with selected type. I got a solution with jquery show/hide function but its not match with desired result.
NOTE: I cant use a submit button with this select box. Thats why I am looking for a solution with Ajax or Jquery.
Can anyone tell me is it possible or not?
Thank you.
This may be Solution...
Pass your Value like this :
<select name="jumpMenu" class="sort-by-select" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)">
<option value=""> Select type </option>
<option value ="yourpage?type=1">Tutor</option>
<option value ="yourpage?type=2">Institute</option>
<option value ="yourpage?type=3">Student</option>
</select>
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore) //v3.0
{
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
</script>
After that get that value like this..
if(isset($_GET['type']) && $_GET['type'] != "")
{
$Q = $_GET['type'];
}
Put this value in Query
SELECT testimonial, city_name, name, web_address, image_name, membership_type
FROM testimonials
INNER JOIN city ON city.city_id = testimonials.city_id where membership_type='$Q'
ORDER BY date_added DESC LIMIT $start, $display
Here is code sample code that can give you some idea what I was suggesting to you ..
$(document).ready(function(){
$('#type').change(function(){
var data = $("#type").val();
$.ajax({
url : "yourdomain/yourfilterpage.php",
data: {'type':data},
type : 'POST' ,
success : function(data){ $('#divtoshowdata').html(data)}
});
})
})
<select class="select" name="type" id='type'>
<option value="1">Tutor</option>
<option value="2">Institute</option>
<option value="3">Student</option>
on yourfilterpage.php get your filtered result
Related
I have been working on a dependent select boxes form using PHP as the server-side language and JQuery with Ajax. I am having an issue with getting the response text, as it is not displaying as options in the second select box.
P.S. I am new to Ajax and there is no video that can help me with my problem.
HTML&PHP:
<center><form method="post" action="php/functions.php" id="catForm">
<select name="catSelect" class="catSelect" name="category">
<option value='null' default>اختر الفئة:</option>
<?php
$selectCategories = mysqli_query($connectionDB, "SELECT * FROM categories");
while($categoriesDisplay = mysqli_fetch_array($selectCategories)){
echo '<option value="'.$categoriesDisplay['id'].'">'.$categoriesDisplay['category'].'</option>';
}
?>
</select><br/><br/>
<select name="subCatSelect" class="subCatSelect">
<option value="null" default>اختر النوع:</option>
<?php
$catSelectVal = $_POST['catSelect'];
$selectSubCat = mysqli_query($connectionDB, "SELECT * FROM sub_categories WHERE id LIKE '$catSelectVal'");
while($subCatDisplay = mysqli_fetch_array($selectSubCat)){
echo '<option value="'.$subCatDisplay['id'].'">'.$subCatDisplay['subCategory'].'</option>';
}
?>
</select><br/>
<h1></h1>
<input type="submit" value="اختر" class="submitForm" /><br/>
</form></center>
Jquery code:
$(document).ready(function(){
$('.catSelect').change(function(){
var changeURL = $('#catForm').attr("action");
var data = $('.catSelect').val();
$.post(changeURL, {category : data}, function(subCategory){
$('.subCatSelect').append(subCategory);
});
});
});
The code that should work on getting the options for the second select box:
$catSelectVal = $_POST['catSelect'];
$selectSubCat = mysqli_query($connectionDB, "SELECT * FROM sub_categories WHERE id LIKE '$catSelectVal'");
while($subCatDisplay = mysqli_fetch_array($selectSubCat)){
echo '<option value="'.$subCatDisplay['id'].'">'.$subCatDisplay['subCategory'].'</option>';
}
I could be wrong, but I don't think you've sent 'catSelect' in the ajax request.. rather you've sent category with data being the value from 'catSelect'
So when you look for $catSelectVal = $_POST['catSelect']; there won't be anything to find.
Try instead: $catSelectVal = $_POST['category'];
As #RiggsFolly mentioned, if you print_r($_POST); you'll instantly see if this is the case or not.
I want view a price values in a field when select a record from dropdown list.
This is the dropdown list code with php code inside:
<select name="product_id[]" class="form-control">
<?php
include("connect.php");
$query = "
SELECT *
FROM tbl_product ORDER BY product_desc ASC
";
$result = mysql_query($query);
while ($record = mysql_fetch_array($result)) {
echo "
<option value=\"$record[product_id]\">$record[product_cod] $record[product_desc]</option>";
}
?>
</select> <input type="text" name="product_price[]" size="6">
In to the specific case: in dropdown list I have 3 records products:
1 milk 3euro
2 wather 1euro
3 caffee 4euro
For example, when select the second record, in the filed price how to put the 4euros values?
Thanks
Well you could try using something like this. My code uses ajax to get the price of the product_id we got from select.
<?php
include("connect.php");
$query = "SELECT * FROM tbl_product ORDER BY product_desc ASC";
$result = mysql_query($query);
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<select name="product_id[]" class="form-control">
<?php
while ($record = mysql_fetch_array($result))
{
?>
<option value="<?php echo $record['product_id']; ?>"><?php echo $record['product_cod'] . " " . $record['product_desc']; ?></option>
<?php
}
?>
</select> <input type="text" id="input_price" name="product_price[]" size="6" value="">
<script>
$( "select" )
.change(function () {
var val = $( "select option:selected").val();
//$('#input_price').val(val);
$.ajax({
url : 'ajax_getprice.php',
type : 'GET',
data : {
'product_id' : val
},
dataType:'json',
success : function(data) {
$('#input_price').val(data);
},
error : function(request,error)
{
alert("Request: "+JSON.stringify(request));
}
});
})
.change();
</script>
ajax_priceget.php
<?php
$productid = $_GET['product_id']; //you have the product id now
//now write a query to select the price of the product using the product id and echo the price
?>
#Frankie, if the price field is separetly shown somewhere then there are two ways by which you can perform this,
1- Use jquery to get the prices, in order to do it through jquery you need to place the prices of the records in their values and then to use $('.product_id[]').val(); to get the price of the selected record.
2- Second way is to use the ajax for that purpose if you have stored item's prices in the databse then you need to hit the database by sitting at the front end at the onChange's jquery method you need to get ajax response regarding the prices against the product id,s and then to display at any posisiton you want to.
Searched many options to find some easy solution where i can sort my results but din't find any.
I want to order the displayed results by selected drop down values. I don't want to use "form". Want the other way to sort it.
<div class="col-lg-2">
<label class="margin-bottom:25px;" style="margin-left:75px;"> Sort by: <label>
</div>
<div class="col-lg-2">
<select class="form-control" id="sortby" name="sortby">
<option selected value="ID">ID</option>
<option value="Name">Name</option>
<option value="Source">Source</option>
<option value="Location">Location</option>
</select>
</div>
The above is my drop down list.
The below is our $sql initial query :
$condition = implode(' AND ', $query);
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source,candidate_contact.cand_location FROM candidate ".$join.' where '.$condition;
Now we have tried to this so far ,but it seems something is wrong.
if($_POST['sortby']=="ID")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source,candidate_contact.cand_location FROM candidate ".$join.' where '.$condition." order by candidate.cand_number asc";
}
if($_POST['sortby']=="Name")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source,candidate_contact.cand_location FROM candidate ".$join.' where '.$condition." order by candidate.cand_fname asc";
}
if($_POST['sortby']=="Source")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source,candidate_contact.cand_source FROM candidate ".$join.' where '.$condition." order by candidate.cand_source asc";
}
if($_POST['sortby']=="Location")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source,candidate_contact.cand_source FROM candidate_contact ".$join.' where '.$condition." order by candidate.cand_location asc";
}
asc- Its for ordering in ascending order;
My jquery/ajax script-
<script>
$(document).ready(function(){
// Each time you change your sort list, send AJAX request
$("#sortby").change(function(){
$.ajax({
method: "POST",
url: "viewcandidate.php",
data: { sortby:$("#sortby").val() }
})
// Copy the AJAX response in the table
.done(function( msg ) {
$("#list").html(msg);
});
});
});
</script>
//even tried with $(window).load(function){.....but no result.
What is wrong as this is not working for me ? I dont want to use form. Suggest me some easy solution.
Firstly Here something wrong...
<select class="form-control" id="sortby" name="sortby">
<option selected value="ID">ID</option>
<option value="Relevance">Name</option><!-- Value ???-->
<option value="Name">Source</option><!-- Value ???-->
</select>
would be like
<select class="form-control" id="sortby" name="sortby">
<option selected value="ID">ID</option>
<option value="Name">Name</option>
<option value="Source">Source</option>
</select>
As you compare like $_POST['sortby']=="Name" and $_POST['sortby']=="Source"
Try to add a blank space before the order by.
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source
FROM candidate ".$join.' where '.$condition." order by candidate.cand_number" ;
Your drop-down list is this :
<select class="form-control" id="sortby" name="sortby">
<option selected value="ID">ID</option>
<option value="Relevance">Name</option>
<option value="Name">Source</option>
</select>
When you post a drop-down list, the value will be the value="..." attribute of the selected option, not the text displayed.
So your 'sortby' conditions are not good, it must be :
if($_POST['sortby']=="ID")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source FROM candidate ".$join.' where '.$condition." order by candidate.cand_number" ;
}
if($_POST['sortby']=="Relevance")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source FROM candidate ".$join.' where '.$condition." order by candidate.cand_fname" ;
}
if($_POST['sortby']=="Name")
{
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source FROM candidate ".$join.' where '.$condition." order by candidate.cand_Source" ;
}
#CodeSniper..try to check some spaces. I am sure you will get the solution with this.
I made a small example to show you how to use jQuery to dynamically reload your data. Of course, you will have to adapt this to your case.
For this example I am using my localhost, with database = 'test' and a table named 'candidate' with three columns : ID, Name and Source.
There are two files : index.php and request.php (in the same folder).
index.php file :
<?php
// Database connection
$db = mysqli_connect('localhost', 'root', '', 'test');
// First request, on load
$sql = "SELECT ID, Name, Source FROM candidate";
$exe = $db->query($sql);
?>
<html>
<head>
<!-- Including jQuery -->
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>
<body>
<div>
<!-- Generate table on load -->
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Source</th>
</tr>
</thead>
<tbody id="list">
<?php
while($row = mysqli_fetch_assoc($exe)) {
echo "
<tr>
<td>".$row['ID']."</td>
<td>".$row['Name']."</td>
<td>".$row['Source']."</td>
</tr>";
}
?>
</tbody>
</table>
Sort by :
<select class="form-control" id="sortby" name="sortby">
<option selected value="ID">ID</option>
<option value="Name">Name</option>
<option value="Source">Source</option>
</select>
</div>
<script>
$(document).ready(function(){
// Each time you change your sort list, send AJAX request
$("#sortby").change(function(){
$.ajax({
method: "POST",
url: "request.php",
data: { sortby:$("#sortby").val() }
})
// Copy the AJAX response in the table
.done(function( msg ) {
$("#list").html(msg);
});
});
});
</script>
</body>
</html>
request.php file :
<?php
$db = mysqli_connect('localhost', 'root', '', 'test');
if(isset($_POST['sortby'])) {
if($_POST['sortby'] == "ID") {
$sql = "SELECT ID, Name, Source FROM candidate ORDER BY ID";
} else if($_POST['sortby'] == "Name") {
$sql = "SELECT ID, Name, Source FROM candidate ORDER BY Name";
} else if($_POST['sortby'] == "Source") {
$sql = "SELECT ID, Name, Source FROM candidate ORDER BY Source";
}
} else {
$sql = "SELECT ID, Name, Source FROM candidate";
}
$exe = $db->query($sql);
while($row = mysqli_fetch_assoc($exe)) {
echo "
<tr>
<td>".$row['ID']."</td>
<td>".$row['Name']."</td>
<td>".$row['Source']."</td>
</tr>";
}
?>
Demo : http://maxime-mettey.com/projects/dropdownajax/
Dowload files : http://maxime-mettey.com/projects/dropdownajax/dropdownajax.zip
I'm a bit stuck on a bit of code im doing.
Here's the code i already have
HTML code:
<form method="post" action="index.php" >
<select name="sortby" class="sortby" >
<option value=" ORDER BY id DESC">Date Added (Newest First)</option>
<option value=" ORDER BY id ASC">Date Added (Oldest First)</option>
<option value="ORDER BY clicks DESC">Website Clicks </option>
<input type="submit" value="Submit" />
</select>
</form>
<form method="post" action="index.php">
<select name="country" class="sortby">
<option value="">Country...</option>
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="AS">American Samoa</option>
<option value="AD">Andorra</option>
.... and so on
</select>
<input type="submit" value="Submit" />
PHP code:
$sortby = $_POST['sortby'];
$sortby = $mysqli->real_escape_string($sortby);
$country = $_POST ['country'];
$country = $mysqli->real_escape_string($country);
$results = $mysqli->query("SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 $sortby");
while($row = $results->fetch_assoc()) {
print '<div id="linkmainwrapper"><div id="linkwrapper"><div class="linkimageborder"><div class="linkimage">'.$row["image"].'</div></div>';
print '<div class="linktitle">'.$row["title"].'</div>';
print '<div class="link">('.$row["link"].')</div>';
print '<div class="countryflag"><img src="http://www.hatblocksdirect.co.uk/lib/flags/'.$row["country"].'.gif"></div>';
print '<div class="linkdescription">'.$row["description"].'</div>';
print '<div class="clickcount">Website Clicks ('.$row["clicks"].')</div></div></div>';
what i would like to do is sort colums. the first sort box works, i can sort by website clicks, id etc using $sortby.
Ive passed the $country using a select drop down but i dont know how to implement this into my select query. I need it to display all entries if a country hasn't been selected. once a country is selected only display that country.
Any help very much appreciated
Put the additional condition of the WHERE clause in a variable depending on whether the parameter is supplied.
if (!empty($_POST['country'])) {
$country = "AND country = '" . $mysqli->real_escape_string($country) . "'";
} else {
$country = "";
}
$results = $mysqli->query("SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 $country $sortby");
Use an if condition like
if(isset($_POST ['country']))
{ // if country is set
$country = $mysqli->real_escape_string($_POST ['country']);
$sql="SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 and country='$country' $sortby";
}
else
{ // if country is not se
$sql="SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 $sortby";
}
$results = $mysqli->query($sql); // execute the query
In my website I have a page to display testimonials. I wrote this code to display my all testimonials from database.
This is my code so far :
while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC)) {
$testimonial = $row['testimonial'];
//echo $testimonial;
$mytestimonial = nl2br($testimonial);
$city = $row['city_name'];
$name = $row['name'];
$url = $row['web_address'];
$imageName = $row['image_name'];
$type = $row['membership_type'];
}
With this code I can get all my testimonials to the page. Its pretty working for me. My problem is now I need to filter my testimonials according to its type. I have 3 different kind of testimonials in my database. (tutor, institute, student)
I am going to use a select box to filter the data. When selecting an option from select box I need to display testimonials according to that selected type.
<div class="filter-box">
<div id="select_box">
<form method="post" action="">
<div class="variation2">
<label>Filter By</label>
<select class="select">
<option>Tutor</option>
<option>Institute</option>
<option>Student</option>
</select>
</div>
</form>
</div>
</div>
Can anyone get me going in a direction here?
Thank You
Why not reload the select box dynamically (using AJAX) once the user has selected one of the 3 options and display your required testimonial.I guess that will solve your problem.
So you want to then select the testimonials based on their type
$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type
FROM testimonials
INNER JOIN city ON city.city_id = testimonials.city_id
WHERE type = '$type'
ORDER BY date_added DESC LIMIT $start, $display";
Now you also want to get the type from the user
<select name="type" class="select">
With the POST
$type = $_POST['type'];
Auto submit the form on change example
Javascript:
<script type="text/javascript">
function submitform(){
document.frmType.submit();
}
</script>
Form:
<form name="frmType" method="post" action="">
<select name="type" class="select" onchange="submitform()">
Further examples can be found here
You have a couple options. The first is with ajax.
Second is submitting the form onchange or onsubmit:
<select class="select" name="type" onchange="document.forms[0].submit();">
<option value="Tutor">Tutor</option>
<option value="Institute">Institute</option>
<option value="Student">Student</option>
</select>
And your query:
$type = '';
if(!empty($_POST) && isset($_POST[type])){
$type = " WHERE testimonials.type = '".$_POST[type]."'";
}
$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type
FROM testimonials
INNER JOIN city ON city.city_id = testimonials.city_id
".$type."
ORDER BY date_added DESC LIMIT $start, $display";
Of course make sure to check if the form was submitted.
add a class by the name to each of the testimonial divs
<div class="student">testimonial goes here</div>
now use jquery to hide/show the categories at one shot.
$('.student').show();
i hope it helps