...

View Full Version : Pass two values into mysql



thoford75
12-08-2010, 05:21 PM
Hi guys, I want to pass two values into a my database through a dropdown list.

Here is my dropdown list


<select name="roomsize" size="1">
<option value="23.8" <?php if ($_POST['roomsize'] == '23.8') { echo 'selected="selected"'; } ?>>Living Room</option>
<option value="14.1" <?php if ($_POST['roomsize'] == '14.1') { echo 'selected="selected"'; } ?>>Bedroom 1</option>
<option value="11.6" <?php if ($_POST['roomsize'] == '11.6') { echo 'selected="selected"'; } ?>>Bedroom 2</option>
</select>


I want to be able to put into the database the option value (i.e 23.8) but also into another field 'Living Room - 23.8'

Any thoughts?

jamied_usa
12-08-2010, 06:50 PM
I'm not sure I understand your question - do you want to pass the value of the selected "roomsize" to another form field value called "livingroom" with an onChange event?



Hi guys, I want to pass two values into a my database through a dropdown list.

Here is my dropdown list


<select name="roomsize" size="1">
<option value="23.8" <?php if ($_POST['roomsize'] == '23.8') { echo 'selected="selected"'; } ?>>Living Room</option>
<option value="14.1" <?php if ($_POST['roomsize'] == '14.1') { echo 'selected="selected"'; } ?>>Bedroom 1</option>
<option value="11.6" <?php if ($_POST['roomsize'] == '11.6') { echo 'selected="selected"'; } ?>>Bedroom 2</option>
</select>


I want to be able to put into the database the option value (i.e 23.8) but also into another field 'Living Room - 23.8'

Any thoughts?

thoford75
12-08-2010, 07:00 PM
I'm not sure I understand your question - do you want to pass the value of the selected "roomsize" to another form field value called "livingroom" with an onChange event?

I want to pass into the database two values.
1) the value of the selected "roomsize"
2) another value "Living Room" into a different field

DataTalk
12-09-2010, 05:11 AM
if you are trying to do this with php you will have to use onChange=submit()

1. parse_str($value); to Field1 and Field2 (which will require a double post)

2. This works much better if you split the values using java


<option value="$field1,$field2">$field2<option>

abduraooft
12-09-2010, 11:15 AM
You don't have to repeat everything everywhere! Just make an array in the serverside, say

$roomsize=array(
'23.8'=>'Living room',
'14.6'=>'Bedroom 1'
'14.1'=>'Bedroom 2'
); and use this through out your code (by making it's available by an include). For example, you may simplify your php for dropdown to

<select name="roomsize" size="1">
<?php
foreach($roomsize as $key=>$value){
printf('<option value="%s" %s>%s</option>',
$key,($_POST['roomsize']==$key?'selected="selected"':''),$value);
}
?>
</select>

DataTalk
12-09-2010, 02:14 PM
That's actually pretty slick abduraooft, but how do you control the array if you have a lot of records?
that would be a lot of work hard coding all the array values.
Is there a way to query the array without manual coding?


I have been using this method with java, but if you view the source code you can see all
the values.



<?php
include("plugin/connection.php") ;

if($_POST) mysql_query("INSERT INTO costcodes (
CostCode,
weight) VALUES(
'".$_POST['CostCode']."',
".$_POST['weight'].")");
}
header("Location: ./costcodes.php");
}
?>

<html>
<head>
<script type="text/javascript">
function Split(sel,first)
{
if (sel.selectedIndex == 0 ) return;
var temp = sel.value.split(",");
sel.form["F"+first].value = temp[0];
sel.form["F"+(first+1)].value = temp[1];
}
</script>
</head>

<body>
<form method="post" name='Validation' target='_self' onsubmit="return validateform(this);">
<!-- First Dropdown -->
<select name="Sel1" onChange="Split(this,1)" >
<option>=== Selection 1 ===</option>
<?php
$sel1 = mysql_query("SELECT * FROM costcodes");
while($r1=mysql_fetch_assoc($sel1)){
echo "<option value='$r1[id],$r1[CostCode]'>$r1[CostCode]</option>\n";
}
?>
</select>

<!-- if you dont want to see these fields type="hidden" -->
<br><input type="text" name="id" id="F1" value="" size="5">
<br><input type="text" name="CostCode" id="F2" value="" size="40">
</body>
</html>

abduraooft
12-09-2010, 02:27 PM
but how do you control the array if you have a lot of records?
that would be a lot of work hard coding all the array values.
Is there a way to query the array without manual coding? That depends on the location where the values are coming from. If it's from a DB, then there's no need for an extra hard coded array. Anyway, wouldn't it be easier to hard code that array once than repeating the values everywhere (in the client side and serverside/DB) ?

DataTalk
12-09-2010, 02:46 PM
I tried your example using array query, it only loads the first record in the database?



<?php
$sql = mysql_query("SELECT * FROM costcodes");
while($row = mysql_fetch_array($sql)){
$costcode = array(
$row[id]=>$row[CostCode],
);
}
?>


<select name="CostCode" size="1">
<?php
foreach($costcode as $key=>$value){
printf('<option value="%s" %s>%s</option>',
$key,($_POST['costCode']==$key?'selected="selected"':''),$value);
}
?>
</select>

DataTalk
12-09-2010, 03:21 PM
That depends on the location where the values are coming from. If it's from a DB, then there's no need for an extra hard coded array. Anyway, wouldn't it be easier to hard code that array once than repeating the values everywhere (in the client side and serverside/DB) ?


If the db-values get changed and updated from time. It would be easier to store the values in the database and be able to manage the db-values +add ?edit -delete.

How would you properly write the array in my example:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum