Separate ID numbers in a column separated by a , - php

I need a bit of help separating id's that are separated by a comma and then displaying them in a dropdown list.
Here's the code below that i am working with, if the clothing_type_id has only 1 number in it, it works fine, if i add more numbers so it's split by a comma i.e 1,2,3,4 it will no longer work, i'm sure i need to use explode but i can not get it to work. Any help will be appreciated.
<? $clothing=intval($_GET['clothing_var']);
include 'classes.php';
mysql_select_db('database');
$query="SELECT ID,Qty_Values FROM Quantity WHERE Clothing_Type_ID='$clothing'";
$result=mysql_query($query);
$test=explode(',', '$result');
?>
<select name="state" onchange="getCity(<?=$clothing?>,this.value)">
<option>Select State</option>
<? while($row=mysql_fetch_array($test)) { ?>
<option value=<?=$row['ID']?>><?=$row['Qty_Values']?></option>
<? } ?>
</select>
UPDATE:
Here's what i'm trying todo.
Hi, pasted your code in and although it works, it only brings up the first selection, if i click on the 2nd id nothing displays. My database table goes like this, the fist selection called Clothing_Type has 3 columns, ID Garment and Price, the second table has 4 columns, ID, Clothing_Type_ID, Qty_Values and price. Basically I am trying to do is make a pricing guide so if i select a t-shirt in the first selection box it will bring up 4 prices at the end of the form, 1 for each item, 2 for a total amount (If selecting a higher quantity) 3 for VAT and 4 Total Amount. Every time selection is selected then it will automatically update the price. After i've selected 3 or 4 selection boxes, i then want a few checkboxes again to update the price if they're ticked.
Update 2: Here's the js and html
JS
<script language="javascript" type="text/javascript">
function getXMLHTTP() {
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 getQty(countryId) {
var strURL="findQty.php?clothing_var="+countryId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('qtydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getCity(countryId,stateId) {
var strURL="findCity.php?clothing_var="+countryId+"&qty_var="+stateId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
HTML
<body>
<form method="post" action="" name="form1">
<?
include 'classes.php';
$query="SELECT ID,Garment FROM Clothing_Type";
$result=mysql_query($query);
?>
<select name="clothing_type" onChange="getQty(this.value)">
<option>Select Clothing Type</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['ID']?>><?=$row['Garment']?></option>
<? } ?>
</select>
<p id="qtydiv">
<select style="width: 150px;" name="qty" disabled='disabled'>
<option>Select Country First</option>
</select>
</p>
<p id="citydiv">
<select style="width: 150px;" name="city" disabled='disabled'>
<option>Select State First</option>
</select>
</p>
<input type="checkbox" name="vehicle" value="Bike" /> I want to live here<br />
<form>
<input type='checkbox' name='1' value='1'id='checkbox_1' />1<br>
<input type='checkbox' name='2' value='2'id='checkbox_2' />2<br>
<input type='checkbox' name='3' value='3'id='checkbox_3' />3<br>
</form>
</body>

Try this,
$clothing = "1,2,3,4";
$query="SELECT ID,Qty_Values FROM Quantity WHERE Clothing_Type_ID IN ($clothing)";

I think you should use the IN() operator instead of just comparing to a comma-separated string:
$query = "SELECT ID, Qty_Values FROM Quantity WHERE Clothing_Type_ID IN($clothing)";

Update your SQL to this..
$query="SELECT ID,Qty_Values FROM Quantity WHERE Clothing_Type_ID in ($clothing)";
Get rid of the explode. This will return a result set of the clothes that have a clothing type Id that is listed in your $clothing list.
Part of me thinks you are doing this backwards. I'm assuming each item in the DB only has 1 clothing type ID and the list of ids is in your $clothing string.
If this isn't the case give me an example look at your DB table.

Related

Why am I not receiving any response from my PHP page and no other errors?

I have 2 pages, practice.php and addtocart.php. In practice.php, I gather info from a form and send the info using AJAX to addtocart.php. This page then connects to the database and inserts my values. However I am not receiving any response from addtocart.php and no data is being inserted in the database.
I have been trying to figure out the problem but cannot seem to find it. Please help.
practice.php
//form from which addtocart is being called
<form id="mehendicones">
Color: <input list="mehendi_color" class="mehendi_color"
id="mehendi_color1">
<datalist id="mehendi_color">
<option value="Black">
<option value="White">
<option value="Brown">
<option value="Dark Red">
</datalist>
<br>
<p id="price">17</p>
Quantity<input type="number" min="5" max="15"
id="quantity_mehendi_color" step="1">
<input type="submit" value="ADD TO CART" style="text-align:center;"
onclick="addtocart()" >
</form>
function addtocart() {
var request = new XMLHttpRequest();
var url = "addtocart.php";
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-
urlencoded");
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
alert(request.response);
}
};
var pdetails = "mehendi cones";
var pquantity = document.forms["mehendicones"]
["quantity_mehendi_color"].value;
var pcolor = document.forms["mehendicones"]
["mehendi_color1"].value;
var price = document.getElementById("price").innerHTML;
var data = ({
"pdetails": pdetails,
"pquantity": pquantity,
"pcolor": pcolor,
"price": price
});
request.send(data);
}
addtocart.php
<?php
include 'includes/database.php';
// Handling data in JSON format on the server-side using PHP
header("Content-Type: application/json");
// build a PHP variable from JSON sent using POST
//method
if($_POST){
$v= json_decode(stripslashes(file_get_contents("php://input")));
echo json_encode($v);
$v->pdetails;
$v->pcolor;
$v->pquantity;
$v->price;
$fname=$_SESSION['firstname'];
$id=$_SESSION['id'];
$nam=$fname.$id;
$sql="INSERT INTO ".$nam."
(userid,pdetails,price,pquantity,pcolor) VALUES
('$id','$pdetails','$price','$pquantity','$pcolor');";
$result=mysqli_query($con,$sql);
if(mysqli_num_rows($result) > 0){
echo "added to cart";
}else{
echo "couldnot add to cart..try again";
}
}
mysqli_close($con);
?>

Third drop down menu does not populate from database

I have got the following Index.php:
<script language="javascript" type="text/javascript">
function getXMLHTTP() { //function 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 getColor(CategoryId) {
var strURL="getColor.php?Category="+CategoryId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('qcolor').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getBrand(CategoryId,ColorId) {
var strURL="getBrand.php?Category="+CategoryId+"&Color="+ColorId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('qbrand').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
</head>
<body>
<div id="Quick_find_2">
<div id="Quick_find_container">
<form action="search2.php" method="get">
<div id="qcategory_1">Product</div>
<div id="qcategory">
<select name="Category" class="dropmenu" id="Category" onChange="getColor(this.value)">
<option value="">Any</option>
<option value="Keyboard">Keyboard</option>
<option value="Piano">Piano</option>
</select>
</div>
<div id="qcolor_1">Colour</div>
<div id="qcolor"><select name="Color" id="Color" class="dropmenu">
<option value="">Select Color</option>
</select>
</div>
<div id="qbrand_1">Brand</div>
<div id="qbrand"><select name="Manufacturer" class="dropmenu">
<option value="">Any</option> </select>
</div>
Please note the form is not ready yet it will need a submit button in the end, however for now I just want to populate the drop down menus
The first two menus work fine I'm having trouble populating the last third one.
The getColor.php contains the following code:
$Category= $_GET['Category'];
mysql_select_db($database_dconn, $dconn);
$query="SELECT DISTINCT Color FROM products WHERE products.Category LIKE '%$Category%' AND Category!= 'Stage Pianos' AND Category!= 'Recent Pianos' AND Category!= 'Recent Keyboards' AND hidden ='no' ORDER BY Color";
$result=mysql_query($query);
?>
<select name="Color" onchange="getBrand(<?=$Category?>,this.value)">
<option value="">Select Color</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['Color']?>><?=$row['Color']?></option>
<? } ?>
</select>
The getBrand.php contains this code:
$Category=$_GET['Category'];
$Color=$_GET['Color'];
mysql_select_db($database_dconn, $dconn);
$query="SELECT DISTINCT Manufacturer FROM products WHERE products.Category LIKE '%$Category%' AND Color = '$Color' AND Category!= 'Stage Pianos' AND Category!= 'Recent Pianos' AND Category!= 'Recent Keyboards' AND hidden ='no' ORDER BY Manufacturer";
$result=mysql_query($query);
?>
<select name="Manufacturer">
<option value="">Select Brand</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['Manufacturer']?>"><?=$row['Manufacturer']?></option>
<? } ?>
</select>
Please don't worry about SQL injection as I will sort that.
Somehow the values for the $Category and Color are not being passed trough can anybody see where I'm going wrong? Any help welcome

listbox not being populated in IE works in FF and Chrome

Hello I am working in php and javascript and have code where a admin can select a name in a dropdown box and it pulls up the locations that name is associated with these are sales guys. Well it works fine in FF and Chrome but in IE the list box just goes blank. hopefully someone can help me out bosses here use IE
Brent
DROPDOWN and LIST code
<label for="firstname"><?php echo ADD_EDIT_SALESREP;?><span class="required">*</span></label>
<select name="sales_rep" id="sales_rep" onChange="findLocation2(this.value)">
<option value="">Select</option>
<?php
$sqlQry1 = "SELECT * FROM ".TABLE_PREFIX."_employee WHERE status='t' AND is_Deleted='N' ORDER BY employee_Id";
$sql_Show1 = $DBObject->db_query($sqlQry1);
while($catArr = $DBObject->db_fetch_array($sql_Show1)){
?>
<div align="center"><br />
<option value="<?php echo $catArr['employee_Id'] ?>"><?php echo SafeOutput($catArr['first_Name']) ?> <?php echo SafeOutput($catArr['last_Name']) ?></option>
</div>
<?php
}
?>
</div>
</div>
</div>
<br />
<p>
</select>
<div id="salesrloc">
<select name="salesr_loc" id="salesr_loc" title="Sales Rep Loc" size="5" multiple="multiple">
<option value="">Select</option>
</select>
<br />
Javascript
<script language="javascript">
dv = document.createElement('div'); // create dynamically div tag
dv.setAttribute('id',"details");
function findLocation2(category)
{
dv.innerHTML='<div id="salesrloc" style="width:auto; height:200px;"><img src="image/loading.gif"></div> <br>';
var url11 = "salesrep2.php";
var qry11="?sales_rep=" + category ;
var result1='salesr_loc';
var ajaxRequest; // The variable that makes Ajax possible!
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){ //alert(ajaxRequest.responseText);
var a=ajaxRequest.responseText;
try{
document.getElementById('salesr_loc').innerHTML=ajaxRequest.responseText;
}catch(e){
dv.innerHTML=ajaxRequest.responseText;
}
}
}
ajaxRequest.open("POST", url11+qry11, true);
ajaxRequest.send(null);
} </script>
and Finally populate listbox php
<?php
include("mastersecure.php");
$emp_id = $_GET['sales_rep'];
$qry= "select * FROM ".TABLE_PREFIX."_location ls, ".TABLE_PREFIX."_customer cs WHERE loc_Salesrep = ".$emp_id." AND ls.customer_Id = cs.customer_Id";
$res = $DBObject->db_query($qry);
?>
<body onLoad="document.getElementById('salesr_loc').focus();">
<select name="salesr_loc[]" id="salesr_loc" title="Sales Rep Loc" size="5" multiple="multiple">
<?php
while($row=$DBObject->db_fetch_array())
{
?>
<option value="<?=$row["location_Id"]?>"><?=$row["customer_Name"]?>, <?=$row["location_Name"]?></option>
<?php
}
?>
</select>
</body>
the IE does not support .innerHTML for < select> elements!!
<select name="salesr_loc" id="salesr_loc"
...
document.getElementById('salesr_loc').innerHTML=ajaxRequest.responseText;
instead, you must do it in a loop:
var d = document.getElementById('salesr_loc');
for(...) {
d.options[x] = new Option(..);
}

AJAX Multiple Drop Downs

I'm looking off of this site to make a multiple drop down: Roshan's Blog
And most of it is working, I'm just having a problem with the 3rd drop down box.
(Dropdown1: Clients, Dropdown2:Location, Dropdown3:Zone)
On my page so far, if I look at the source, after selecting the first dropdown(Client1), the second dropdown statement says:
<select style="width: 150px;" id="add-event-dialog-location" name="add-event-dialog-location" onchange="getZone(Client1,this.value)">
Which is what I need, but now, when I click on one of the options in the second drop down, it is not placing through the getZone() script. The 'zonediv' is not changing, and I'm not sure if the rest is going through or not. If I load the getZone.php by itself and place in my own GET statements into the URL, I get results, but I can't get them within the page I'm calling the dropdowns from.
I'm probably just missing something small, but I've been looking at it so long, that I just can't figure it out.
The HTML:
<select style="width: 150px;" name="add-event-dialog-client_name" id="add-event-dialog-client_name" onchange="getLocation(this.value)">
<?php
echo "<option selected='selected' disabled='disabled'>-Client Name-</option>";
$result = mysql_query("SELECT DISTINCT client_name FROM spc_clients");
while($row = mysql_fetch_array($result)){
echo "<option value='".$row['client_name']."'>".$row['client_name']."</option>";
}
?>
</select>
<p id="locationdiv">
<select style="width: 150px;" name="add-event-dialog-location" id="add-event-dialog-location" disabled="disabled">
<option>Select Client First</option>
</select>
</p>
<p id="zonediv">
<select style="width: 150px;" name="add-event-dialog-zone" id="add-event-dialog-zone" disabled="disabled">
<option>Select Location First</option>
</select>
</p>
Both of the JS Functions:
function getLocation(client_name) {
var strURL="display/getLocation.php?client_name="+client_name;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('locationdiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getZone(client_name,location) {
var strURL="display/getZone.php?client_name="+client_name+"&location="+location;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('zonediv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
getLocation.php:
<?php
include 'connect.php';
$client = $_GET['client_name'];
$query="SELECT location FROM spc_clients WHERE client_name='$client'";
$result=mysql_query($query) or die(mysql_error());
?>
<select style="width: 150px;" id="add-event-dialog-location" name="add-event-dialog- location" onchange="getZone(<?=$client?>,this.value)">
<option selected='selected' disabled='disabled'>-Location-</option>
<?php
while($row = mysql_fetch_array($result)){
echo "<option value='".$row['location']."'>".$row['location']."</option>";}
?>
</select>
getZone.php:
<?php
include 'connect.php';
$client = $_GET['client_name']; echo $client;
$location = $_GET['location']; echo $location;
$query="SELECT zone FROM spc_clients WHERE (client_name='$client' && location='$location')";
$result=mysql_query($query) or die(mysql_error());
?>
<select style="width: 150px;" id="add-event-dialog-zone" name="add-event-dialog-zone">
<option selected='selected' disabled='disabled'>-Zone-</option><option><?php
while($row = mysql_fetch_array($result)){
echo $row['zone'];}
?>
</option>
</select>
Try putting quotes around Client1 -- without quotes, javascript thinks it's a variable, and since you haven't defined any variable called Client1, you're getting an error. Putting quotes around it makes it a string, which is what you want to pass to getZone().
Try putting this in getLocation.php:
<select style="width: 150px;" id="add-event-dialog-location" name="add-event-dialog- location" onchange="getZone('<?=$client?>',this.value)">
If any of your Client names have quotation marks in them, you'll have to make sure to escape them, see here for how to do that:
Pass a PHP string to a JavaScript variable (and escape newlines).

Dynamic AJAX list not posting

I have a dynamic drop down box which calls another PHP page. I've got it to work just how I want it.
Below is the part of the form:
<tr>
<td>
<p> Select a delivery date </p>
</td>
<td>
<select name='listdate' onchange='showDelivery(this.value)'>
<option value=''>select delivery type:</option>
<option value='forwardorder'>Forward Order</option>
<option value='byreturn'>By Return</option>
</select>
<div id='txtHint'>
<b>Change to drop down box to display delivery date</b>
</div>
</td>
</tr>
The Ajax
function showDelivery(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getDelivery.php?q="+str,true);
xmlhttp.send();
}
The php script
$q=$_GET["q"];
// And create a cid object
require_once $CID_INCLUDE_PATH . "/cid.php";
$cid = new CHCID();
if ($q == 'forwardorder')
{
echo"<td><select 'name'='deliveryDate'/> ";
$listCapacityDates = $cid->ListCapacity();
foreach($listCapacityDates as $x) {
echo "<option value='".$x."'>".$x."</option> </select>";
}
}
if ($q == 'byreturn')
{
echo"<div id='div1'>Enter By Return Date<input type='text''name='deliveryDate' />
</div>";
}
I know the problem, because the results from the Ajax drop downs are shown through the PHP pages when the form submits none of those values are submitted. But I'm not sure how I can even submit them? Any ideas?
echo"<td><select 'name'='deliveryDate'/> ";
should be :
echo "<td><select name='deliveryDate'/> ";
and
echo"<div id='div1'>Enter By Return Date<input type='text''name='deliveryDate' />
should be :
echo"<div id='div1'>Enter By Return Date<input type='text' name='deliveryDate' />
and:
if ($q == 'forwardorder')
{
echo"<td><select 'name'='deliveryDate'/> ";
$listCapacityDates = $cid->ListCapacity();
foreach($listCapacityDates as $x) {
echo "<option value='".$x."'>".$x."</option>";
}
echo "</select></td>";
}
</select> should be outside foreach
You seem to understand why it's not working, just not how to correct it, right?
When you return the data from your PHP page, it needs to be processed via the javascript.
Instead of returning,
echo "<option value='".$x."'>".$x."</option> </select>";
It should be possibly json.
{ val : key, val : key }
Then Javascript can insert it into the HTML DOM, then your form will recongnize the values when you submit the form.
As for the Javascript code, there are a few ways, jQuery has some plugins for handle it.
Otherwise I can dig around for some code, if someone doesn't beat me too it :)
Edit:
Off hand, I think this code should work.
It's untested, but hopefully will give more of an idea how to use it.
replace
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
with
var response = xmlhttp.responseText;
var select = document.getElementByName('listdate');
var option;
for(var i=0; i<response.length; i++)
{
option = document.createElement("OPTION");
option.text = response.key[i];
option.value = response.val[i];
select.options.add(option);
}

Categories