php code problem

May 9, 2009 at 11:51:56
Specs: Windows XP
Hi Guys,
I am almost through with my project then I hip a big problem: I have a mySQl db holding a list of item names for sale with their respective columns of prices and quantities for each item.So far I can populate all these item names in a select combo. Now what I want is that when I scrow down the combo and select (onClick) any of the item names, I should have its price and respective quantity showing in the same page without sending it to the server again.

Looking forwaard to your assistance.

See More: php code problem

Report •

May 12, 2009 at 16:56:28
Without seeing what you're up to, an idea might be to populate the select's value with all fields separated by a deliminator of some type, and then only use the name for the text property.

for example using javascript

<script language="javascript">

function process(val, idx) {

   var d = document.getElementById("out");
   var t = val.split(":");

   d.innerHTML = "item: " + parseInt(idx + 1) + 
                 " (" + t[1] + " / " + t[2] + ")";


<div id="out"></div>
<select id="sel" size="1" onchange="process(this.value, this.selectedIndex);">
<option value="item_name:item_price:item_quantity">item_name</option>
<option value="item_name:item_price:item_quantity">item_name</option>
<option value="item_name:item_price:item_quantity">item_name</option>
<option value="item_name:item_price:item_quantity">item_name</option>
<option value="item_name:item_price:item_quantity">item_name</option>

How you would generate / output the select would depend on how you've done your code, so I can't really offer anything there.

Hope that helps.

My work in progress. I hate JS. :P

Report •

May 18, 2009 at 09:27:59
Thank you Shutat you have been great help. Tho I should mention that I needed to indicate to you that my select menus are actually htlm populated with PHP variables like bellow:

<select onChange='ajaxFunction()' name="select1" id='select111' > //combostarts here

// populate the combo
foreach($namearray as $mane){
<option value="<?php echo $mane ?>"><?php echo $mane ?></option>



As you can see, I have so far been able to sqeez in some ajax function looks like so:

<script language="javascript" type="text/javascript">
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!

// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!It can't support latest technologies. Up grade it please!");
return false;
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('price1');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
var select111 = document.getElementById('select111').value;
var queryString = "?select111=" + select111;"GET", "gets_price_for_ajax.php" + queryString, true);


This function grabs the clicked option value from the select combo and passes it to file "gets_price_for_ajax.php" which looks like below:

// Retrieve data from ajax Query String
$select111 = $_GET['select111'];

if ($select111 != "SELECT ITEM") //examine for selected iterms

{//get the price for the elected item
//extract itermname and the db in which the item is
$$select111 = "$select111"; // first convert it to string
$pieces = explode("_", $select111); // this bursts the string into an array
//echo $pieces[0]; // piece1
$wdb=$pieces[0]; //wanted db
$dbname1 = metalcast._.$wdb;
$table1 = $pieces[1]; //wanted table
//echo $table1;
//echo $dbname1;

//echo $pieces[1]; // piece2

//connection to the db
$user = "root";
$host = "localhost";
$pass = "$$$$$$$$";
$db1 = "".$dbname1."";

//create connection the database
$conn = @mysql_connect($host,$user,$pass) or die("connection failure ".mysql_error());
//select database

$select = @mysql_select_db("".$db1."") or die ("database not selected ".mysql_error());

$table1 = mysql_real_escape_string($table1);

$sql = "SELECT price, quant FROM ".$table1." WHERE actualname = '$select111'";


$db_select1= @mysql_query($sql) or die ("SELECT faild////////////////".mysql_error());
$ar = mysql_fetch_array($db_select1);//packs the selected data into an array

echo $price1; // to actually echo to the form //next to the section combo <DIV>id =1</DIV>
echo $quant1; // to actually echo to the form //next to the section combo but in a separate <DIV>id =2</DIV>


As you can see, am not yet happy with myself as I cant get the price $price1 and the stock quantity $quant1 printed separately. Please help me with this last bit and any other observations on my code!



Report •

May 19, 2009 at 13:09:04
I just glanced over it a bit, but I think you can probably output the sql result using a deliminator, and then use javascript's split function from within the ajax.responseText to set each of your required divs. For example

the php

die($ar[0] . ":" . $ar[1]);

The ajax responseText

var res = ajaxRequest.responseText.split(":");
if(res.length == 2) {
   document.getElementById("price"),innerHTML = res[0];
   document.getElementById("quan").innerHTML = res[1];
} else {
   // error retrieving $_GET request

the two divs
<div id="price"></div>
<div id="quan"></div>

I *think* that would do, but I haven't tested it myself. For the ajax call, I don't really know how your database it setup, but you'd probably need to pass along the select index and the select id, so maybe the ajaxFunction could look similar to.

<select id="select111" onchange="ajaxFunction(this.selectedIndex,;">
   loop {
      echo "<option>...</option>";

When you call the request, pass along the desired select id and the item's index. If you can, post the database table that you're using... that way, I'd have a better idea of what you'd need to do.

Hope that helps.

My work in progress. I hate JS. :P

Report •

Related Solutions

May 20, 2009 at 08:32:51
Thanks Shutat! You are just great.
Everything has gone so well for me today.In fact i have been able to do more than I previously perceived with your code. What is remaining will surely easily find itself in the upgrade release! For now let me get something going.

Thanks once more Shutat. i will post again when i hit another hitch.


Report •

May 20, 2009 at 14:57:24
no worries mate; if I can help someone out, then it's all worth it. :)

My work in progress. I hate JS. :P

Report •

Ask Question