By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,711 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,711 IT Pros & Developers. It's quick & easy.

combo box / text box php mysql

P: n/a
this is the the code, now here is the final result.... I get one combo box
and
one tex box come up, but they are empty...
DAMN!!!
Any ideas on what Im doing wrong please..
Thanks Again

<?php require_once('..\library_database\Connections\onli nequote.php'); ?>

<?
function select_cases_price ()
{
$qy = "SELECT SellingPrice, Description FROM cases";
$rs = mysql_query ($qy) or die (mysql_error ());
while (mysql_fetch_array ($rs))
{ $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
$prices[] = $r{1};
}
return array ($select, $prices);
}
?>

On your web page, call the function.

<? list ($select, $prices) = select_cases_price (); ?> And a bit of JS:
<script>
function showPrice() {
array prices (<? echo $prices ?>);
var i = document.form.item.selectedIndex; document.form.SellingPrice =
prices[i]; </script>

<select name="Description" onUpdate="showPrice()"> <? echo $select; ?>
</select> <input type="text" name="SellingPrice" value="">
Jul 17 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
Phill Long wrote:
<?
function select_cases_price ()
{
// I like to initialize variables
$prices = array();
$select = '';

$qy = "SELECT SellingPrice, Description FROM cases";
$rs = mysql_query ($qy) or die (mysql_error ());
while (mysql_fetch_array ($rs))
while ($r = mysql_fetch_array ($rs))
// ____^^^^^________________________

{ $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
$prices[] = $r{1};
// ?? __________^_^_ is this ok?
// shouldn't it be
$prices[] = $r[1];
// ... I didn't test it with curly braces

}
return array ($select, $prices);
// $prices is an array

}
?>

On your web page, call the function.

<? list ($select, $prices) = select_cases_price (); ?> And a bit of JS:
// $prices is an array!

<script>
function showPrice() {
array prices (<? echo $prices ?>);


// same as
array prices (Array);
// probably wrong I think :-)

(snip)

Happy bug hunting :-)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

P: n/a
Pedro Graca wrote:
<script>
function showPrice() {
array prices (<? echo $prices ?>);


// same as
array prices (Array);
// probably wrong I think :-)


Maybe imploding the array is enough?

array prices (<?php echo implode(', ', $prices); ?>);
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #3

P: n/a
Did the things requested, but still not working, any ideas??
I get the combo box to read all the case details, but I just cant get the
thing to put a price in the little text box.. thats all I ask, please help
me..
here is a link to the site..
:http://djsleepy.servebeer.com/all%5F...jsleepy/_test/

<?php require_once('..\library_database\Connections\onli nequote.php'); ?>

<?
function select_cases_price ()
{
$qy = "SELECT Description, SellingPrice FROM onlinequote.cases";
$rs = mysql_query ($qy) or die (mysql_error ());
while ($r=mysql_fetch_array ($rs))
{ $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
$prices[] = $r{1};
}
return array ($select, $prices);
}
?>

On your web page, call the function.

<? list ($select, $prices) = select_cases_price (); ?>

And a bit of JS:

<script>
function showPrice()
{
array prices (<? echo $prices ?>);
var i = document.form.item.selectedIndex; document.form.SellingPrice =
prices[i];
</script>

<select name="Description" onUpdate="showPrice()"> <? echo $select; ?>
</select> <input type="text" name="SellingPrice" value="">
Jul 17 '05 #4

P: n/a
Phill Long wrote:
Did the things requested, but still not working, any ideas?? (snip) And a bit of JS:

<script>
function showPrice()
{
array prices (<? echo $prices ?>);


Still printing "Array" inside the JS? :)

See the source of your page and you'll see

<script>
function showPrice()
{
array prices (Array);

which, obviously (though I don't know JavaScript), is wrong.

Try the implode(', ', $prices) I told you on my last post. I expect the
JS to become

<script>
function showPrice()
{
array prices (4.5, 126.33, 88, 0.3);
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #5

P: n/a
Phill Long wrote:

Did the things requested, but still not working, any ideas??
I get the combo box to read all the case details, but I just cant get the
thing to put a price in the little text box.. thats all I ask, please help
me..
here is a link to the site..
:http://djsleepy.servebeer.com/all%5F...jsleepy/_test/

<?php require_once('..\library_database\Connections\onli nequote.php'); ?>

<?
function select_cases_price ()
{
$qy = "SELECT Description, SellingPrice FROM onlinequote.cases";
$rs = mysql_query ($qy) or die (mysql_error ());
while ($r=mysql_fetch_array ($rs))
{ $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
$prices[] = $r{1};
}
return array ($select, $prices);
}
?>


There were a few errors in the JS. Use the implode() thing Pedro mentioned.
The code below is tested on IE5/Win98:

<script>
function showPrice()
{
var prices = new Array(100, 110, 120, 130, 140,150);
var i = document.forms[0].Description.selectedIndex;
document.forms[0].SellingPrice.value = prices[i];
}
</script>
<form>
<select name="Description" onChange="showPrice()"> <option value='Thermaltake
Lanfire Black ATX Case With Side Window No PSU'>Thermaltake Lanfire Black ATX
Case With Side Window No PSU</option>
<option value='Thermaltake Lanfire Silver ATX Case With Side Window No
PSU'>Thermaltake Lanfire Silver ATX Case With Side Window No PSU</option>
<option value='Thermaltake Screwless Skull Atx Case in Black No PSU'>Thermaltake
Screwless Skull Atx Case in Black No PSU</option>
<option value='Thermaltake Xaser III Supertower Black ATX Case With Window No
PSU'>Thermaltake Xaser III Supertower Black ATX Case With Window No PSU</option>
<option value='ATX Midi Tower Case 350w PSU in Black'>ATX Midi Tower Case 350w
PSU in Black</option>
<option value='ATX Case In Silver With Front USB/Audio/1394'>ATX Case In Silver
With Front USB/Audio/1394</option>
</select>

<input type="text" name="SellingPrice" value="">
</form>

Notes:
Use implode or join to print out the array so it looks like the JS code above.
I changed the onUpdate() handler to onChange.
"item" in the original should be "Description".
You need <FORM> tags if you're going to reference using document.forms...etc.
If this isn't the first form, you'll have to change forms[0] to whatever.
You might want to put onLoad="showPrice()" in the body tag to set the intial
price

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #6

P: n/a
Yet again I have ammended the code to suit what has been suggested, but
still cant get that ole text box to display the price.. HELP ME PLEASE!

<?php require_once('..\library_database\Connections\onli nequote.php'); ?>
<body onLoad="showPrice()">
</body>
<?
function select_cases_price ()
{
$qy = "SELECT Description, SellingPrice FROM onlinequote.cases";
$rs = mysql_query ($qy) or die (mysql_error ());
while ($r=mysql_fetch_array ($rs))
{ $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
$prices[] = $r[1];
}
return array ($select, $prices);
}
?>

On your web page, call the function.

<? list ($select, $prices) = select_cases_price (); ?>

And a bit of JS:

<script>
function showPrice()
{
var prices = new Array(100, 110, 120, 130, 140,150);
var i = document.forms[0].Description.selectedIndex;
document.forms[0].SellingPrice.value = prices[i];
}
</script>
<select name="Description" onChange="showPrice()"> <? echo $select; ?>
</select> <input type="text" name="SellingPrice" value="">
Jul 17 '05 #7

P: n/a
"Phill Long" <dj******@djsleepy.co.uk> wrote in message news:<c0**********@hercules.btinternet.com>...
Yet again I have ammended the code to suit what has been suggested, but
still cant get that ole text box to display the price.. HELP ME PLEASE!

<?php require_once('..\library_database\Connections\onli nequote.php'); ?>
<body onLoad="showPrice()">
</body>
<?
function select_cases_price ()
{
$qy = "SELECT Description, SellingPrice FROM onlinequote.cases";
$rs = mysql_query ($qy) or die (mysql_error ());
while ($r=mysql_fetch_array ($rs))
{ $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
$prices[] = $r[1];
}
return array ($select, $prices);
}
?>

On your web page, call the function.

<? list ($select, $prices) = select_cases_price (); ?>

And a bit of JS:

<script>
function showPrice()
{
var prices = new Array(100, 110, 120, 130, 140,150);
var i = document.forms[0].Description.selectedIndex;
document.forms[0].SellingPrice.value = prices[i];
}
</script>
<select name="Description" onChange="showPrice()"> <? echo $select; ?>
</select> <input type="text" name="SellingPrice" value="">


I checked your previous mail and the URL you have posted.
You are not using any *form* tags to enclose your *select-box* and *text-box*

Use form tags, your page will work fine.

Hope it will help.

--
Cheers,
Rahul Anand
Jul 17 '05 #8

P: n/a
Phill Long wrote:

Yet again I have ammended the code to suit what has been suggested, but
still cant get that ole text box to display the price.. HELP ME PLEASE!

<?php require_once('..\library_database\Connections\onli nequote.php'); ?>
<body onLoad="showPrice()">
</body>
<?
function select_cases_price ()
{
$qy = "SELECT Description, SellingPrice FROM onlinequote.cases";
$rs = mysql_query ($qy) or die (mysql_error ());
while ($r=mysql_fetch_array ($rs))
{ $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
$prices[] = $r[1];
}
return array ($select, $prices);
}
?>

On your web page, call the function.

<? list ($select, $prices) = select_cases_price (); ?>

And a bit of JS:

<script>
function showPrice()
{
var prices = new Array(100, 110, 120, 130, 140,150);
var i = document.forms[0].Description.selectedIndex;
document.forms[0].SellingPrice.value = prices[i];
}
</script>

<select name="Description" onChange="showPrice()"> <? echo $select; ?>
</select> <input type="text" name="SellingPrice" value="">


Yes, what Rahul said. And when you're working with Javascript, always put it in
a proper HTML document. That means, put in <HTML> tags, put your select and
option tags within <FORM> tags, and don't close your <BODY> before you start
your form. If you insist on taking these kind of shortcuts you're only going to
end up frustrating yourself.
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #9

P: n/a
Ok I did all that re-coding and the page works fine apart from I want the
array of prices to come from the mysql table. I dont want to have to enter
them. as I am going to use this for quite a few products.. can this be
done..

Thanks in Advance Guys, u've been brilliant so far!
"Shawn Wilson" <sh***@glassgiant.com> wrote in message
news:40***************@glassgiant.com...
Phill Long wrote:

Yet again I have ammended the code to suit what has been suggested, but
still cant get that ole text box to display the price.. HELP ME PLEASE!

<?php require_once('..\library_database\Connections\onli nequote.php'); ?> <body onLoad="showPrice()">
</body>
<?
function select_cases_price ()
{
$qy = "SELECT Description, SellingPrice FROM onlinequote.cases";
$rs = mysql_query ($qy) or die (mysql_error ());
while ($r=mysql_fetch_array ($rs))
{ $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
$prices[] = $r[1];
}
return array ($select, $prices);
}
?>

On your web page, call the function.

<? list ($select, $prices) = select_cases_price (); ?>

And a bit of JS:

<script>
function showPrice()
{
var prices = new Array(100, 110, 120, 130, 140,150);
var i = document.forms[0].Description.selectedIndex;
document.forms[0].SellingPrice.value = prices[i];
}
</script>

<select name="Description" onChange="showPrice()"> <? echo $select; ?>
</select> <input type="text" name="SellingPrice" value="">
Yes, what Rahul said. And when you're working with Javascript, always put

it in a proper HTML document. That means, put in <HTML> tags, put your select and option tags within <FORM> tags, and don't close your <BODY> before you start your form. If you insist on taking these kind of shortcuts you're only going to end up frustrating yourself.
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com

Jul 17 '05 #10

P: n/a
<?php require_once('..\library_database\Connections\onli nequote.php'); ?>

<html>
<body onLoad="showPrice()">

<?
function select_cases_price ()
{
$qy = "SELECT Description, SellingPrice FROM onlinequote.cases";
$rs = mysql_query ($qy) or die (mysql_error ());
while ($r=mysql_fetch_array ($rs))
{ $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
$prices[] = $r[1];
}
return array ($select, $prices);
}
?>

On your web page, call the function.

<? list ($select, $prices) = select_cases_price (); ?>

And a bit of JS:

<script>
function showPrice()
{
var prices = new Array(120,130,110,120,120,110);
var i = document.forms[0].Description.selectedIndex;
document.forms[0].SellingPrice.value = prices[i];
}
</script>

<form>
<select name="Description" onChange="showPrice()"><? echo $select;
?></select>
<input type="text" name="SellingPrice" value="">
</form>

</body>
</html>
Jul 17 '05 #11

P: n/a
Phill Long wrote:
<script>
function showPrice()
{
var prices = new Array(120,130,110,120,120,110);


You once almost had this right!
The prices come from the DB, they are not constants.

You had something like
var prices = new Array(<?php echo $prices; ?>);

and, as $prices is an array that didn't work.

You have to transform the array of prices into a string of values.

[code working="false"]
<?php echo $prices; ?>
[/code]
outputs "Array"

[code working="false" better="true"]
<?php
foreach ($prices as $value) {
echo "$value,";
}
?>
[/code]
outputs "120,130,110,120,120,110," with the final extra comma

but
[code working="true"]
<?php echo implode(",", $prices); ?>
[/code]
outputs "120,130,110,120,120,110" right the way you want :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #12

P: n/a
nearly right.. Sorry

this displays the price as 16.2, if the price is 16.20... But is there a way
to display the whole value (e.g 16.20) ??????
"Pedro Graca" <he****@hotpop.com> wrote in message
news:c0*************@ID-203069.news.uni-berlin.de...
Phill Long wrote:
<script>
function showPrice()
{
var prices = new Array(120,130,110,120,120,110);


You once almost had this right!
The prices come from the DB, they are not constants.

You had something like
var prices = new Array(<?php echo $prices; ?>);

and, as $prices is an array that didn't work.

You have to transform the array of prices into a string of values.

[code working="false"]
<?php echo $prices; ?>
[/code]
outputs "Array"

[code working="false" better="true"]
<?php
foreach ($prices as $value) {
echo "$value,";
}
?>
[/code]
outputs "120,130,110,120,120,110," with the final extra comma

but
[code working="true"]
<?php echo implode(",", $prices); ?>
[/code]
outputs "120,130,110,120,120,110" right the way you want :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--

Jul 17 '05 #13

P: n/a
Phill Long wrote:
nearly right.. Sorry

this displays the price as 16.2, if the price is 16.20... But is there a way
to display the whole value (e.g 16.20) ??????


You're printing the price in JavaScript. I don't know that.

If it were PHP

<?php echo number_format(16.2, 2); ?>

outputs 16.20.

You might try to replace the implode() thing with another method that
writes the JavaScript array as an array of strings
formatted the way you want -- left as an exercise

or

.... do it in JavaScript
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #14

P: n/a
but
[code working="true"]
<?php echo implode(",", $prices); ?>
[/code]
outputs "120,130,110,120,120,110" right the way you want :)

I mean the price from the mysql database is 16.20, and the webpage displays
16.2 from this code... So how do I alter this code to make it display 16.20
on the page
Jul 17 '05 #15

P: n/a
Phill Long wrote:
but
[code working="true"]
<?php echo implode(",", $prices); ?>
[/code]
outputs "120,130,110,120,120,110" right the way you want :)

I mean the price from the mysql database is 16.20, and the webpage displays
16.2 from this code... So how do I alter this code to make it display 16.20
on the page


I think your best option is using JavaScript ... but I don't know how to
do it in JavaScript.

Leave the PHP as is and format the string with JS functions.
Probably comp.lang.javascript is a better place to ask your question.
....................................

If you really want to do it in PHP, then you have to change that code
above so that it outputs

'120.00','130.00','110.00','120.00','120.00','110. 00'

which is left as an exercise :-)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #16

P: n/a
Phill Long wrote:

but
[code working="true"]
<?php echo implode(",", $prices); ?>
[/code]
outputs "120,130,110,120,120,110" right the way you want :)

I mean the price from the mysql database is 16.20, and the webpage displays
16.2 from this code... So how do I alter this code to make it display 16.20
on the page


Pedro has already given you the solution. You want the resulting HTML code to
look like this:

var prices = new Array("120.00","130.00","110.00","120.00","120.00" ,"110.00");

He's even suggested the function, number_format, that will format the prices
with the .00 (or whatever) on the end. It's now a simple matter to print the
array out to make it look like the above. Use basic php like foreach and
modifying the code.

Hint: when people in this group use phrases like "left as an exercise" it's
usually because the code is too long or tiresome to write out or they feel that
they're being called on to do something so simple that they've crossed the line
from providing tips and guidance into doing someone else's work for them.

Hint2: the code to do this is not tiresome or long. :)

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #17

P: n/a
please gimme another clue, Im really new to all this and just wanna learn..

I am also still waiting for a slow delivery of my php/mysql book from
amazon...

PRETTY PLEASE WITH SUGAR ON TOP!
Jul 17 '05 #18

P: n/a
Phill Long wrote:

please gimme another clue, Im really new to all this and just wanna learn..

I am also still waiting for a slow delivery of my php/mysql book from
amazon...

PRETTY PLEASE WITH SUGAR ON TOP!


Why don't you read php.net for foreach, join and number_format and try it. You
really do learn a lot more when you try things yourself. If you run into
trouble, post what you've done. Actually, if you don't run into trouble, let us
know that too.

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.