This ps_search.php page has dropdown for categories like laptop, bags etc... and a dropdown for city. if a user wants to post an ad means he/she wants to sell something on website , first of all a category is selected like laptop, bag or anything else. and then city is selected from where the ad is posted. if, for instance, a user posts an ad of laptop from city1 and we have no more ads of laptops from other cities . then after selecting laptop , city 1 should appear in a dropdown menu. i have two files ps_search.php ang get_city.php
//ps_search.php
<form name="search" method="post" action="<?php echo $base_url ?>search_rides.php?go">
<table width="100%" border="0" cellpadding="4" cellspacing="0" id="tbsrch-engine">
<tbody>
<tr>
<td height="61">
<strong>Choose Category:</strong>
</td>
<td>
<select name="category_id" id="category_id" style="width: 155px;" onChange="get_city(this.value,'<?php echo $base_url ?>dropdown/get_city.php')">
<option value="0">Any</option>
<?php $query = "SELECT category_id as id, category_name as name FROM tbl_ps_category ";
$result = mysql_query($query);
?>
<?php while ( $row = mysql_fetch_array($result)) {?>
<option value="<?php echo $row['id'].'-'.$row['name']; ?>"><?php echo $row['name']?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td height="40">
<strong>City:</strong>
</td>
<td>
<div id="models">
<select name="city_id" id="city_id">
<option value="0">Any</option>
</select>
</div>
</td>
</tr>
<tr>
<td height="32"/>
<td>
<input type="submit" name="button2" id="button2" value="Search an Ad" class="fbutton"/>
</td>
</tr>
</tbody>
</table>
</form>
//get_city.php
<?php
include('../Connections/photohive.php');
$id = $_REQUEST['id'];
$explode = explode('-',$id);
$id = $explode[0];
$sql = "SELECT city_id FROM ".$ps_prefix."product WHERE category_id=".$id;
$query = mysql_query($sql);
//exit;
?>
<select name="city_id" id="city_id" style="width:155px;">
<option value="0">Any</option>
<?php
while ($row = mysql_fetch_array($query)){
$q1= sprintf("Select cityname from tbl_city where city_id='%s'" , mysql_real_escape_string($row['city_id']), " ORDER BY cityoreder ASC");
$r1= mysql_query($q1);
while($row2= mysql_fetch_assoc($r1))
{
?>
<option value="<?php echo $row['city_id']?>"><?php echo $row2['cityname']?></option>
<?php
}
}
?>
</select>
i have included js file as well
<script src="<?php echo $base_url;?>js/jquery.ajaxq-0.0.1.js" type="text/javascript"></script>
Please help me....
Your javascript should look like this:
(function ($) {
// When category select changes
$('#category_id').change(function() {
// make ajax query to get cities
$.get('get_city.php', {id: $(this).val())
.done(function(response) {
// get the cities dropdown from response and insert into page
$('#models').html(response);
});
});
})(jQuery);
I personally don't like returning HTML in my ajax calls. I usually prefer to return a JSON object and then parse out the HTML client side (usually using Handlebars). I think it's cleaner that way.
Related
I have three multiple select menus. I am trying to select the options returned from a database query. It is not setting any options. I console log the arrays returned by php and they are displayed as:["5233", "7148"]["5233", "5437", "5316"]["7029", "7852", "5525"].
I am not concerned about the form submission at this point. I only want to display the values returned from the db query as selected. I am sure there is something I am missing but can't seem to figure it out. I appreciate all the help I get. Thank you in advance!!
First are my 3 database arrays that are created:
Second is my html:
Third is my javascript/jQuery:
I have three multiple select menus. I am trying to select the options returned from a database query. It is not setting any options. I console log the arrays returned by php and they are displayed as:["5233", "7148"]["5233", "5437", "5316"]["7029", "7852", "5525"].
I am not concerned about the form submission at this point. I only want to display the values returned from the db query as selected. I am sure there is something I am missing but can't seem to figure it out. I appreciate all the help I get. Thank you in advance!!
First are my 3 database arrays that are created:
Second is my html:
Third is my javascript/jQuery:
<?
$huntNum = $hunts[$i][$fm_hunt_fields['__id']];
$cookRole = 'Cook';
$cookVols = get_volunteersByHunt($huntNum, $cookRole);
foreach ($cookVols as $cVols) {
//create new cook array that only contains ID of person
$exCooks[] = $cVols['_id_acct'];
}
$guideRole = 'Hunt Guide';
$hgVols = get_volunteersByHunt($huntNum, $guideRole);
foreach ($hgVols as $hVols) {
$exHg[] = $hVols['_id_acct'];
}
$supportRole = 'General Support';
$gsVols = get_volunteersByHunt($huntNum, $supportRole);
foreach ($gsVols as $gVols) {
$exGs[] = $gVols['_id_acct'];
}
?>
<script>
var existing_cooks = <?= json_encode($exCooks); ?>
var existing_hgs = <?= json_encode($exHg); ?>
var existing_gss = <?= json_encode($exGs); ?>
</script>
<form action="" name="vol-update" id="vol-update" class="vol- update" method="post">
<input type="hidden" id="id_hunt" name="id_hunt" value="<?= $huntNum; ?>" />
<input type="hidden" name="action2" id="action2" value="1" />
<table class="tyhp-account">
<tr>
<td>
<div class="floatL cont_lft_side">
<label for="cooks"><strong>Cooks</strong></label>
<select name="cook[]" class="form-control vCooks" multiple="multiple">
<?php
foreach ($cooksArray as $cook) {
?>
<option
<?
if (in_array($cook['ID'], $exCooks)) {
echo "selected='selected'";
}
?>
value="<?= $cook['ID']; ?>" >
<?= $cook['name_last'] . $cook['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
<td>
<div class="floatL cont_lft_side">
<label for="hunt_guides"><strong>Hunt Guides</strong></label>
<select name="huntGuide[]" class="form-control vHg" multiple="multiple">
<?php
foreach ($guidesArray as $guide) {
?>
<option
<?
if (in_array($guide['ID'], $exHg)) {
echo "selected='selected'";
}
?>
value="<?= $guide['ID']; ?>" >
<?= $guide['name_last'] . $guide['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
<?php
$allVols = getAllVolunteers();
?>
<td>
<div class="floatL cont_lft_side">
<label for="supp"><strong>General Support</strong></label>
<select name="gsupport[]" class="form-control vSupp" multiple="multiple">
<?php
foreach ($allVols as $allVol) {
?>
<option
<?
if (in_array($allVol['__id'], $exGs)) {
echo "selected='selected'";
}
?>
value="<?= $allVol['ID']; ?>" >
<?= $allVol['name_last'] . $allVol['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
</tr>
<tr>
<td> </td>
<td style="text-align:center">
<input type="submit" name="action" id="upVol" class="btn btn-primary" value="Update Volunteers">
</td>
<td> </td>
</tr>
</table>
</form>
//Volunteer information for hunts
<script>
var cooks = existing_cooks;
var hunt_guides = existing_hgs;
var gen_support = existing_gss;
console.log(cooks);
console.log(hunt_guides);
console.log(gen_support);
//Cooks multiSelect
$(".vCooks").val(cooks);
$('.vCooks').multiselect({
columns: 2,
placeholder: 'Select Cooks'
});
//Hunt Guide multi-select
$(".vHg").val(hunt_guides);
$('.vHg').multiselect({
columns: 2,
placeholder: 'Select Hunt Guides'
});
//General Support multi-select
$(".vSupp").val(gen_support);
$('.vSupp').multiselect({
columns: 2,
placeholder: 'Select General Support'
});
return false;
</script>
I made a cart system in which quantity is getting updated successfully but now i have added two new fields i.e color and size. I want to pass selected values in php variables.I don't want to use <form> tag.Is it possible to do so? I have tried following code. Any suggestions please.
code
<?php
$size = $_REQUEST['size'];//selected size
$color = $_REQUEST['color'];//selected color
if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
$_SESSION['cart'][$id]['size'];//session for size
$_SESSION['cart'][$id]['color'];//session for color
}else{
$sql_p="SELECT * FROM products WHERE productid={$id}";
$query_p=mysqli_query($con, $sql_p);
if(mysqli_num_rows($query_p)!=0){
$row_p=mysqli_fetch_array($query_p);
$_SESSION['cart'][$row_p['productid']]=array("quantity" => 1, "price" => $row_p['product_price'],"color"=>$color,"size"=>$size);
}else{
$message="Product ID is invalid";
}
}
}
?>
<table border="1">
<tr>
<th>Name</th>
<th>Picture</th>
<th>Description</th>
<th>Price</th>
<th>color</th>
<th>size</th>
</tr>
<?php
$query = mysqli_query($con,"SELECT * FROM products where cat_id=2 ORDER BY product_name ASC");
while($row=mysqli_fetch_assoc($query)){
?>
<tr>
<td><?php echo $row['product_name']; ?></td>
<td><img src="images/<?php echo $row['product_image']; ?>" width="120px" height="120px"></td>
<td><?php echo $row['product_desc']; ?></td>
<td><?php echo "$" . $row['product_price']; ?></td>
<td>Colors:
<select name="color">
<option selected value="choose">choose</option>
<option value="blue" id="blue">Blue</option>
<option value="yellow" id="yellow">Yellow</option>
<option value="green" id="green">Green</option>
</select></td>
<td> <select name="size"><option selected value="Choose size">Choose</option>
<option value="XL" id="XL">XL</option>
<option value="L" id="L">L</option>
<option value="S" id="S">S</option></select>
</td>
<td>Add to Cart</td>
</tr>
<?php
}
?>
</table>
I'd turn it around and make it an actual form. That way, you can select the product, the properties and use the submit button to add it to the cart.
If you want to keep this link, you will need some Javascript to add the properties to the link, but then you're basically recreating a poor man's form using a script and a link.
Using an actual form per product, you can easily let the user select products and add them to the cart, without any need for scripts.
The script I added is just to show the url, since Stack Snippets doesn't make that very obvious.
// Show the post url in the document, since forms are blocked in Stack Snippets.
$('form').on('submit', function() {
document.write(
'about to navigate to: ' +
$(this).attr('action') + '?' +
$(this).serialize());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="product" method="get" action="add.php">
<input type="hidden" name="product" value="12345">
<select name="color">
<option value="red">red</option>
<option value="green">red</option>
<option value="blue">red</option>
</select>
<button type="submit">Add to cart</button>
</form>
I don't think you can do what you want using PHP alone. I think you are going to have to use a Browser side language like JavaScript.
Do you know you could use tables inside a form?
I want to retrieve data to check-box list when user selected an option from a drop-down list. So, the main categories are loaded in a drop-down list and subcategories are suppose to be loaded in a list of check-boxes. Right now, I sorted out to populate data to another drop-down list. here is my code:
JQuery Code:
<script type="text/javascript">
$(document).ready(function() {
$("#parent_cat").change(function() {
$(this).after('<div id="loader" style="position:inline;"><img src="device manager/img/loading.gif" alt="loading subcategory" /></div>');
$.get('user manager/loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
$("#sub_cat").html(data);
$('#loader').slideUp(200, function() {
$(this).remove();
});
});
});
});
</script>
PHP Code:
<tr>
<?php
include('config.php');
$query_parent = mysql_query("SELECT * FROM tblclient") or die("Query failed: ".mysql_error());
?>
<form method="get">
<tr>
<td><label>Client</label></td>
<td>
<select name="parent_cat" id="parent_cat">
<option selected="selected" disabled="disabled">Select a Client</option>
<?php while($row = mysql_fetch_array($query_parent)): ?>
<option value="<?php echo $row['clientId']; ?>"><?php echo $row['clientName']; ?></option>
<?php endwhile; ?>
</select>
</td>
</tr>
<br/><br/>
<tr>
<td><label>Branch</label></td>
<td>
<select name="sub_cat" id="sub_cat" multiple>
<option selected="selected" disabled="disabled">Select a Branch</option>
</select>
</td>
</tr>
</form>
Loading subcategory list PHP Code:
<?php
include('config.php');
$parent_cat = $_GET['parent_cat'];
$query = mysql_query("SELECT B.*, C.* FROM tblbranch B INNER JOIN tblclientbranch CB on CB.branchId = B.branchId INNER JOIN tblclient C ON CB.clientId = C.clientId WHERE C.clientId = {$parent_cat}");
while($row = mysql_fetch_array($query)) {
echo "<option value='$row[branchId]'>$row[branchName]</option>";
}
?>
Does anyone know how to populate subcategories to a list of check-boxes?
Any solution would be great.
Thanks for reading my long code!
something like
<input type="checkbox" name="branch" value="$row[branchid]">$row[branchname]<br>
I have 2 forms, one where I enter records and one where I edit records.
On the form that I add records, I have a drop down which queries another table in my database for the values.
When I click on the "edit" record form to edit this data, I am unable to get the value that has already been entered to be pre-selected in the dropdown.
<select name="aankomstluchthaven">
<option name="aankomstluchthaven" value="">--Select--</option>
<?php
$list=mysql_query("select luchthavenID, luchthavencode from tbl_luchthaven
order by luchthavencode ASC");
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<?echo $row_list['luchthavenID']; ?>"> <?echo
$row_list['luchthavencode']; ?> </option>
<?
}
?>
</select>
the field that is being updated is 'aankomstluchthaven'. So the value from my table where the form data is stored (tbl_vluchtgegevens) should be populated.
Where tbl_luchthaven.luchthavenID = tbl_vluchtgegevens.aankomstluchthaven
I tried putting in the option value, but that is not what I"m looking for.
Edit to include example php page with dropdown (note, I have changed from aankomstluchthaven in the example above to vertrekluchthaven in the example below. same thing applies.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>bijwerk vluchtgegevens Form</title>
</head>
<body>
<? include "datalogin.php";//database connection
$order = "SELECT *
FROM tbl_vluchtgegevens
WHERE gegevenID='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<table border=1>
<tr>
<td width="547" align=center>bijwerk vluchtgegevens: <br>
gegevenID = <? echo "$row[gegevenID]"?></td>
<td width="547" align=center> </td>
</tr>
<tr>
<td>
<table>
<form method="post" action="bijwerkvlucht_post.php">
<input type="hidden" name="id" value="<? echo "$row[gegevenID]"?>">
<tr>
<td width="208">Vertrekluchthaven </td>
<td width="325">
<select name="vertrekluchthaven">
<option name="vertrekluchthaven" value="">--Select--</option>
<?php
$list=mysql_query("select luchthavenID, luchthavencode from tbl_luchthaven order by luchthavencode ASC");
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<?echo $row_list['luchthavenID']; ?>"> <?echo
$row_list['luchthavencode']; ?> </option>
<?
}
?>
</select>
</td>
</tr><tr>
<td align="right">
<input type="submit"
name="submit" id="submit" value="Submit"> </td>
</tr>
</form>
</table> </td>
<td> </td>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
</body>
</html>
Assuming you are getting the values by id or whatever when you edit the form, you would do something like this:
<option value="<?echo $row_list['luchthavenID']; ?>" <? echo ($row['luchthavenID'] == $row_list['luchthavenID'] ? ' selected="selected"' : ''); ?>> <?echo $row_list['luchthavencode']; ?> </option>
Each time through the loop you check the value against the values pulled in the query. If they match, you make this option selected.
UPDATED
I still don't know what your field is called, but assuming it is $row['luchthavenID'] I would write it like this:
echo '<option value="'.$row_list['luchthavenID'].'"'.($row['luchthavenID'] == $row_list['luchthavenID'] ? ' selected="selected"' : '').'>'.$row_list['luchthavencode'].'</option>';
I'm new with php and want to ask about this code.. I have one dropdown menu and one button..
I want to search in sql database what I choose in those drop down menu..
What is the sql syntax for search item in sql database by using drop down menu..
By default, I write --> SELECT * FROM helpline.
It should be --> SELECT * FROM helpline WHERE MISC = %**item in drop down menu**%.
This is my database = helpdesk
Table = helpline
NAME | DATE | MISC |
John | 02/01/2011 | Item 1 |
Mark | 03/01/2011 | Item 2 |
This is my code
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<label for="namelist"></label>
<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled">PLEASE CHOOSE ONE ITEM:-</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
<input type="submit" name="show" id="show" value="Submit" />
<?php
mysql_select_db("helpdesk",mysql_connect("localhost","root",""))or die (mysql_error());
$query = "SELECT * FROM helpline"; */ This line should select what I choose in drop down menu /*
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
?>
<table border="0" cellpadding="6" cellspacing="6" class="curve">
<thead>
<tr>
<th> <div align="right"><span class="font">NAME</span></div></th>
<th> <div align="right"><span class="font">DATE</span></div></th>
</tr>
</thead>
<tbody>
<tr>
<th><div align="left"><span class="font"><?php echo $row['name']; ?></span></div></th>
<td><div align="left"><span class="font"><?php echo $row['date']; ?></strong></span></div></td>
</tr>
</tbody>
<?php
}
?>
</table>
</form>
So, it should display all items in sql database according to what I choose in drop down menu when I click the button..
You can access your posted form data through the php $_POST[] associative array as described in PHP's Online Manual. From this you can select namelist and pass the value to your SQL.
$query = "SELECT * FROM helpline";
if(isset($_POST['namelist'])){
$dropdown_val = $_POST['namelist'];
$query .= " WHERE MISC = '$dropdown_val'";
}
But you should really be using ajax for this and jQuery would be a great javascript framework to help you accomplish this. Also, mysql_* functions are deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
Without Page Refresh You can do it like this
<script>
$(document).ready(function(){
$("#namelist").change(function(){
var data = $(this).val();
$.ajax({
type:'POST',
data:'search_value='+data,
url:'search_process.php',
success:function(data){
$("#result").html(data);
}
});
});
});
</script>
The HTML part
<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled">PLEASE CHOOSE ONE ITEM:-</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
<input type="button" name="show" id="show" value="Submit" />
<div id="result"></div>
The search_process.php Page
<?php
// DATABASE CONNECTIVITY
if(isset($_POST['search_value'])) {
$val = $_POST['search_value'];
$query = mysql_query("SELECT * FROM helpline WHERE MISC LIKE %$val%");
// NOW RUN YOUR QUERY
$result = '<table border="0" cellpadding="6" cellspacing="6" class="curve">';
$result .= '<thead>';
$result .= '<tr>';
$result .= '<th> <div align="right"><span class="font">NAME</span></div></th>';
$result .= '<th> <div align="right"><span class="font">DATE</span></div></th>';
$result .= '</tr>';
$result .= '</thead>';
while($row = mysql_fetch_array($query)){
$result .= '<tr>';
$result .= '<th><div align="left"><span class="font">'.$row['name'].'</span></div></th>';
$result .= '<td><div align="left"><span class="font">'.$row['date'].'</strong></span></div></td>';
$result .= '</tr>';
}
$result .= '</table>';
echo $result;
}
?>
Hope this gives an Idea
your select options not have value attribute
your dropdown
<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled">PLEASE CHOOSE ONE ITEM:-</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
change it :
<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled" value="">PLEASE CHOOSE ONE ITEM:-</option>
<option value="Item 1">Item 1</option>
<option value="Item 2">Item 2</option>
</select>
after submit get value from $_post["namelist"]