PDA

View Full Version : Select Box Help



junestag
05-25-2008, 03:45 AM
Hello,
I'd like to make a states select box size 6 in a form that allows a user to select a state then hit a button titled 'add state' that then adds the state to another select box size 6 that starts out empty. the state they selected is then added to the empty select box and taken away from the first select box. there would also be a button to put the state back into the first select box. anyone have any ideas on how to do such a thing? i assume it uses javascript...

s.

Nile
05-25-2008, 12:50 PM
Does this help you?
BYK Found an error in my code, here's the right code:


<html>
<head>
<script type="text/javascript">
function putIn(to,from){
var x = document.getElementById(from);
var i = document.getElementById(to);
var opt = new Option(x.options[x.selectedIndex].text, x.options[x.selectedIndex].value);
i.add(opt, undefined);
x.remove(x.selectedIndex);
}
</script>
</head>
<body>
<select id="1" size="6" style="width:114px">
<option value="1">Cake</option>
<option value="2">Cookies</option>
<option value="3">Ice Cream</option>
<option value="4">Lolly Pop</option>
<option value="5">Chocolate</option>
<option value="6">Gummy Bears</option>
</select>
<button onClick="putIn(2,1)">Take Away</button><br />
<select id="2" size="6" style="width:114px">
</select>
<button onClick="putIn(1,2)">Put Back</button><br />
</body>
</html>

junestag
05-25-2008, 07:42 PM
That DOES allow me to add the text from one select box to the other and remove it accordingly, so we're on the right track. However when I try to process the form field with php i get a NULL result for the values in the second select box with id=2.

Here's the code I'm using with your javascript function added to my select boxes:

<td><select name=\"geo_scope0[]\" style=\"width:auto\" id=\"1\" size=\"6\">$option_block1</select><button type=\"button\" onClick=\"putIn(2,1)\">Add >></button><br /><button type=\"button\" onClick=\"putIn(1,2)\"><< Remove</button><select name=\"geo_scope[]\" id=\"2\" style=\"width:auto\" size=\"6\"></td></tr>

The contents of $option_block1 is so:

for ($i=0; $i < 40; $i++) {
$option_block1 .= "<option value=\"$states[$i]\">$states[$i]</option>";

Any idea why the values aren't processing correctly in my addrecord script?

Nile
05-25-2008, 09:55 PM
Can I see your whole code?

junestag
05-25-2008, 11:39 PM
Absolutely:

<?php
session_start();
if ((isset($_SESSION['username'])) && (isset($_SESSION['password']))) {

// initialize variables
$states = array('Statewide','Adams County','Asotin County','Benton County','Chelan County','Clallam County','Clark County','Columbia County','Cowlitz County','Douglas County','Ferry County','Franklin County','Garfield County','Grant County','Grays Harbor County','Island County','Jefferson County','King County','Kitsap County','Kittitas County','Klickitat County','Lewis County','Lincoln County','Mason County','Okanogan County','Pacific County','Pend Oreille County','Pierce County','San Juan County','Skagit County','Skamania County','Snohomish County','Spokane County','Stevens County','Thurston County','Wahkiakum County','Walla Walla County','Whatcom County','Whitman County','Yakima County');
$option_block1 = "";
$display_block = "";

// populate states dropdown
for ($i=0; $i < 40; $i++) {
$option_block1 .= "<option value=\"$states[$i]\">$states[$i]</option>";
}

// create html form
$display_block ="
<form method=\"post\" action=\"addrecord.php\" id=\"record\" name=\"record\" onsubmit=\"return checkform();\">
<fieldset>
<legend>PROVIDER INFORMATION</legend>
<table class=\"formtable\">
<tr>
<td class=\"label\">Provider Name:</td><td><input type=\"text\" id=\"provider_name\" name=\"provider_name\" size=\"50\" /> <font color=\"red\"<strong>REQUIRED FIELD</strong></font></td></tr>
<tr>
<td class=\"label\">Contact First Name:</td><td><input type=\"text\" id=\"contact_fname\" name=\"contact_fname\" size=\"30\" /></td></tr>
<tr>
<td class=\"label\">Contact Last Name:</td><td><input type=\"text\" id=\"contact_lname\" name=\"contact_lname\" size=\"30\" /></td></tr>
<tr>
<td class=\"label\">Phone:</td><td><input type=\"text\" id=\"phone\" name=\"phone\" size=\"10\" /></td></tr>
<tr>
<td class=\"label\">E-mail:</td><td><input type=\"text\" id=\"email\" name=\"email\" size=\"30\" /></td></tr>
<tr>
<td class=\"label\">URL:</td><td><input type=\"text\" id=\"url\" name=\"url\" size=\"50\" /></td></tr>
<tr>
<td class=\"label\">Eligibility Requirements:</td><td><textarea name=\"eligibility_req\" cols=\"90\" rows=\"12\"></textarea></td></tr>
<tr>
<td class=\"label\">Program Description:</td><td><textarea name=\"program_desc\" cols=\"90\" rows=\"12\"></textarea></td></tr>
<tr>
<td class=\"label\">Deadline:</td><td><select name=\"deadline\" id=\"deadline\"><option value=\"yes\">yes</option><option value=\"no\">no</option></select></td></tr>

<tr>
<td class=\"label\">Application Procedures:</td><td><input type=\"text\" id=\"app_proc\" name=\"app_proc\" size=\"50\" value=\"See Website\" /></td></tr>
<tr>
<td class=\"label\">Geographic Scope:<br />
(may select multiple)
</td>
<td><select name=\"geo_scope0[]\" style=\"width:auto\" id=\"1\" size=\"6\">$option_block1</select><button type=\"button\" onClick=\"putIn(2,1)\">Add >></button><br /><button type=\"button\" onClick=\"putIn(1,2)\"><< Remove</button><select name=\"geo_scope[]\" id=\"2\" style=\"width:auto\" size=\"6\"></td></tr>
</table>
</fieldset>
<div align=\"center\">
<input type=\"submit\" name=\"submit\" value=\"Add this Record\" class=\"submit\" id=\"submit\" /></div>
</form>";

?>

<html>
<head>
<title>Enter a New Record</title>
<link rel="stylesheet" href="style/infra-db.css" type="text/css">
<script src="scripts/checkform.js" type="text/javascript"></script>
<script type="text/javascript">
function putIn(to,from){
var x = document.getElementById(from);
var i = document.getElementById(to);
var opt = new Option(x.options[x.selectedIndex].text, x.options[x.selectedIndex].value);
i.add(opt, undefined);
x.remove(x.selectedIndex);
}
</script>
</head>
<body>

<div class="header">
<p class="title">Washington Infrastructure Database</p>
<p class="subtitle">Add New Record</p>
<p class="links"><strong>Add Record</strong> | Modify Record | <a href="pick_viewrecord.php">View Records</a></p>
</div>

<?php

echo "$display_block";

} else { header("Location: http://" . $_SERVER['HTTP_HOST'] .
dirname($_SERVER['PHP_SELF']) . "/login.php");
exit(); }?>

</body>
</html>

Nile
05-25-2008, 11:42 PM
Can I see addrecord.php, it will help me. :D Because you said that you were having problems with when it sends the data.

junestag
05-25-2008, 11:50 PM
Yes, it's here. You'll notice there are a lot more fields i'm processing from the previous form (i edited those out of my last post to be less confusing for you).

<?php
session_start();
if ((isset($_SESSION['username'])) && (isset($_SESSION['password']))) {

//includes
include("connection.php");
include("escape.php");

// set variables

$geo_scope = $_POST['geo_scope'];

if (isset($geo_scope) && ($geo_scope != "")) {
foreach ($geo_scope as $v) {
switch ($v) {
case 'Statewide':
$statewide = 'statewide';
break;
case 'Adams County':
$adams = 'adams';
break;
case 'Asotin County':
$asotin = 'asotin';
break;
case 'Benton County':
$benton = 'benton';
break;
case 'Chelan County':
$chelan = 'chelan';
break;
case 'Clallam County':
$clallam = 'clallam';
break;
case 'Clark County':
$clark = 'clark';
break;
case 'Columbia County':
$columbia = 'columbia';
break;
case 'Cowlitz County':
$cowlitz = 'cowlitz';
break;
case 'Douglas County':
$douglas = 'douglas';
break;
case 'Ferry County':
$ferry = 'ferry';
break;
case 'Franklin County':
$franklin = 'franklin';
break;
case 'Garfield County':
$garfield = 'garfield';
break;
case 'Grant County':
$grant = 'grant';
break;
case 'Grays Harbor County':
$grays_harbor = 'grays harbor';
break;
case 'Island County':
$island = 'island';
break;
case 'Jefferson County':
$jefferson = 'jefferson';
break;
case 'King County':
$king = 'king';
break;
case 'Kitsap County':
$kitsap = 'kitsap';
break;
case 'Kittitas County':
$kittitas = 'kittitas';
break;
case 'Klickitat County':
$klickitat = 'klickitat';
break;
case 'Lewis County':
$lewis = 'lewis';
break;
case 'Lincoln County':
$lincoln = 'lincoln';
break;
case 'Mason County':
$mason = 'mason';
break;
case 'Okanogan County':
$okanogan = 'okanogan';
break;
case 'Pacific County':
$pacific = 'pacific';
break;
case 'Pend Oreille County':
$pend_oreille = 'pend_oreille';
break;
case 'Pierce County':
$pierce = 'pierce';
break;
case 'San Juan County':
$san_juan = 'san juan';
break;
case 'Skagit County':
$skagit = 'skagit';
break;
case 'Skamania County':
$skamania = 'skamania';
break;
case 'Snohomish County':
$snohomish = 'snohomish';
break;
case 'Spokane County':
$spokane = 'spokane';
break;
case 'Stevens County':
$stevens = 'stevens';
break;
case 'Thurston County':
$thurston = 'thurston';
break;
case 'Wahkiakum County':
$wahkiakum = 'wahkiakum';
break;
case 'Walla Walla County':
$walla_walla = 'walla walla';
break;
case 'Whatcom County':
$whatcom = 'whatcom';
break;
case 'Whitman County':
$whitman = 'whitman';
break;
case 'Yakima County':
$yakima = 'yakima';
break;
}
}
}

$last_updated = date('Y-m-d');

$provider_name = escape_data($_POST['provider_name']);
$contact_fname = escape_data($_POST['contact_fname']);
$contact_lname = escape_data($_POST['contact_lname']);
$phone = escape_data($_POST['phone']);
$email = escape_data($_POST['email']);
$url = escape_data($_POST['url']);
$eligibility_req = escape_data(nl2br($_POST['eligibility_req']));
$program_desc = escape_data(nl2br($_POST['program_desc']));
$app_proc = escape_data($_POST['app_proc']);
$deadline = $_POST['deadline'];

$sql = "INSERT INTO providers (provider_name, contact_fname, contact_lname, phone, email, url, eligibility_req, statewide, adams, asotin, benton, chelan, clallam, clark, columbia, cowlitz, douglas, ferry, franklin, garfield, grantt, grays_harbor, island, jefferson, king, kitsap, kittitas, klickitat, lewis, lincoln, mason, okanogan, pacific, pend_oreille, pierce, san_juan, skagit, skamania, snohomish, spokane, stevens, thurston, wahkiakum, walla_walla, whatcom, whitman, yakima, el_all, el_cities_towns, el_county, el_state, el_federal, el_nat, el_nonprofit, el_private, el_individual, el_spd, el_other, sc_all, sc_grants, sc_loans_pri, sc_gloans, sc_ta, sc_other, description, app_procedures, last_updated, ced_all, ced_disaster, ced_bus_incubators, ced_historic_pres, ced_housing, ced_industrial, ced_nr, ced_pub_safety, ced_research, ced_rural_plan, ced_sust_dev, ced_urb_plan, ced_other, comm_all, comm_broadcast, comm_tele, comm_other, ed_all, ed_childcare, ed_k12, ed_he, ed_lib, ed_comm_ed, ed_research, ed_other, ener_all, ener_alt, ener_coal, ener_elec, ener_hydro, ener_nuclear, ener_solar, ener_wind, ener_other, ener_bio, env_all, env_ag, env_brownfields, env_ener_eff, env_forestry, env_grazing, env_green_bldg, env_landscaping, env_landuse, env_riparian, env_sp, env_other, pf_all, pf_art, pf_comm_centers, pf_ad_al, pf_fs, pf_ps, pf_pj, pf_pl, pf_health_fac, pf_mun_bldgs, pf_museums, pf_other, rec_all, rec_greenbelt, rec_outdoor, rec_parks, rec_sports, rec_trails, rec_other, rec_play, trans_all, trans_air, trans_bridges, trans_marine, trans_offroad, trans_pt, trans_ped_bike, trans_parking, trans_pub_lighting, trans_streets, trans_rail, trans_other, wm_all, wm_hazard, wm_recycling, wm_solid_waste, wm_waste_water, wm_other, water_all, water_dams, water_irr, water_storm, water_drinking, water_flood, water_other, pp_all, pp_planning, pp_feas, pp_design, pp_construction, pp_other, deadline) VALUES ('$provider_name', '$contact_fname', '$contact_lname', '$phone', '$email', '$url', '$eligibility_req', '$statewide', '$adams', '$asotin', '$benton', '$chelan', '$clallam', '$clark', '$columbia', '$cowlitz', '$douglas', '$ferry', '$franklin', '$garfield', '$grant', '$grays_harbor', '$island', '$jefferson', '$king', '$kitsap', '$kittitas', '$klickitat', '$lewis', '$lincoln', '$mason', '$okanogan', '$pacific', '$pend_oreille', '$pierce', '$san_juan', '$skagit', '$skamania', '$snohomish', '$spokane', '$stevens', '$thurston', '$wahkiakum', '$walla_walla', '$whatcom', '$whitman', '$yakima', '$el_all', '$el_cities_towns', '$el_county_gov', '$el_state_gov', '$el_fed_gov', '$el_nat', '$el_nonprofit', '$el_private', '$el_ind', '$el_spd', '$el_other', '$sc_all', '$sc_grants', '$sc_loans_pri', '$sc_g_loans', '$sc_ta', '$sc_other', '$program_desc', '$app_proc', '$last_updated', '$ced_all', '$ced_dr_aid', '$ced_bi', '$ced_hp', '$ced_housing', '$ced_ind_park', '$ced_nr', '$ced_ps', '$ced_research', '$ced_rural', '$ced_sd', '$ced_up', '$ced_other', '$comm_all', '$comm_bs', '$comm_tele', '$comm_other', '$ed_all', '$ed_childcare', '$ed_k12', '$ed_he', '$ed_lib', '$ed_ce', '$ed_rf', '$ed_other', '$en_all', '$en_ae', '$en_coal', '$en_elec', '$en_hydro', '$en_nuc', '$en_solar', '$en_wind', '$en_other', '$en_bio', '$env_all', '$env_ag', '$env_brown', '$env_ee', '$env_forest', '$env_grazing', '$env_gb', '$env_landscaping', '$env_lup', '$env_riparian', '$env_shoreline', '$env_other', '$pf_all', '$pf_art', '$pf_cc', '$pf_adal', '$pf_fire', '$pf_ps', '$pf_prisons', '$pf_pl', '$pf_hcf', '$pf_mb', '$pf_museums', '$pf_other', '$rec_all', '$rec_greenbelt', '$rec_or', '$rec_parks', '$rec_sports', '$rec_trails', '$rec_other', '$rec_playgrounds', '$trans_all', '$trans_air', '$trans_bridges', '$trans_marine', '$trans_ors', '$trans_pts', '$trans_pbf', '$trans_park', '$trans_pl', '$trans_streets', '$trans_rs', '$trans_other', '$wm_all', '$wm_hazard', '$wm_recycle', '$wm_sw', '$wm_wwss', '$wm_other', '$water_all', '$water_dams', '$water_irrigation', '$water_storm', '$water_drinking', '$water_flood', '$water_other', '$pp_all', '$pp_planning', '$pp_feas', '$pp_design', '$pp_construct', '$pp_other', '$deadline')";

$result = @mysql_query($sql,$connection) or die(mysql_error());

?>

<html>
<head>
<title>Record Added Successfully</title>
<link rel="stylesheet" href="style/infra-db.css" type="text/css">
</head>

<body>

<div class="header">
<p class="title">Washington Infrastructure Database</p>
<p class="subtitle">Record Added Successfully</p>
<p class="links"><a href="show_add.php">Add Record</a> | Modify Record | <a href="pick_viewrecord.php">View Records</a></p>
</div>

<?php

} else { header("Location: http://" . $_SERVER['HTTP_HOST'] .
dirname($_SERVER['PHP_SELF']) . "/login.php");
exit(); }?>

</body>
</html>

Nile
05-26-2008, 03:38 AM
Sorry, but I'm gonna ask for one more thing before I get to work. ;)
Can I have an example? (Not needed, just curious and it helps).
Also, when you put any kinda code use the
tags, when your using html use the
tags, and when your using php use the
tags.

junestag
05-26-2008, 08:20 PM
I'm not sure what you mean by an example?

Anyway, i've been playing with the code for awhile now. When i do a var_dump on the geo_scope[] array field which is the select box that was filled by clicking the add button on the show_add.php script i get NULL. I thought it was the javascript you gave me that was causing the problem but using the web developer toolbar for firefox, after adding several counties into the new select box it shows them accurately having the right values for each option.

i also thought maybe the problem might be that the id and the name fields for the select boxes are different but i tried this with a text field and it didnt matter what i gave as the value for the id field the addrecord script still recognized it's value correctly.

I don't know why the variable is coming out as NULL using var_dump()??

Nile
05-26-2008, 08:43 PM
Just to straighten this out, I thought that the value wasn't following the options when the javascript fixed it. So your saying that it passes the values correctly and it's the PHP? Try running it through print_r and see if you get the same results.
Example as in an example page, the page that this script is on with the php.

junestag
05-27-2008, 01:14 AM
Ok, i made show_add-test.php and addrecord-test.php just for you (doesn't require login).

http://infrafunding.myonlineplace.org/show_add-test.php

That's the form and you can submit it yourself. I've added var_dump($_POST['geo_scope']; and print_r($_POST['geo_scope']); to addrecord-test.php so when you submit you'll see that the value you get for var_dump is NULL and for print_r nothing at all.

sage

Nile
05-27-2008, 01:28 AM
I see null. But also can you answer my other question(I missed the answer).

Just to straighten this out, I thought that the value wasn't following the options when the javascript fixed it. So your saying that it passes the values correctly and it's the PHP?

junestag
05-27-2008, 05:16 AM
well with the web developer plugin in firefox after i add a county from the first select box it shows it as <select><option value="county name">county name</option>...so you would think it would pass the value as such. But when I click the submit button it gives me null on the php processing script which doesn't seem to make any sense. I'm sure I'm missing something simple...

Nile
05-27-2008, 10:48 PM
Hmm, I tried but got good results:
http://niler.net/examples/select.php
In the brackets is the value, and if you select something and click "Get Data", something will appear next to it that says: Value: x(x as in the value you chose). And then if you select somethings in both select fields (if you don't, it will output null because of the if statement in $val[1] and $val[2]), and click "Get Full Data", then you should see your results. So, it worked perfectly for me. Here's my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Select Script, PHP Example.</title>
<script type="text/javascript">
function putIn(to,from){
var x = document.getElementById(from);
var i = document.getElementById(to);
var opt = new Option(x.options[x.selectedIndex].text, x.options[x.selectedIndex].value);
i.add(opt, undefined);
x.remove(x.selectedIndex);
}
function getData(ins){
var i = document.getElementById(ins);
var x = document.getElementById(ins+"_show");
x.innerHTML = "Value: "+i.options[i.selectedIndex].value;
}
</script>
</head>
<body>
<div style="padding-bottom:15px;">
<?php
if(isset($_GET['show'])){
$val[1] = ($_GET[1] != "") ? $_GET[1] : "null";
$val[2] = ($_GET[2] != "") ? $_GET[2] : "null";
echo "The value of Example 1 is: <b>$val[1]</b>, and the value of Example 2 is: <b>$val[2]<b>.";
}
?>
</div>
<form action="<?php echo $_SERVER[PHP_SELF];?>" method="GET">
<b>Example 1</b><br /><br />
<select id="1" name="1" size="6" style="width:125px">
<option value="1">Cake[1]</option>
<option value="2">Cookies[2]</option>
<option value="3">Ice Cream[3]</option>
<option value="4">Lolly Pop[4]</option>
<option value="5">Chocolate[5]</option>
<option value="6">Gummy Bears[6]</option>
</select>
<input type="button" onClick="putIn(2,1)" value="Take Away"><input type="button" onClick="getData(1)" value="Get Data">&nbsp;<span id="1_show" style="color:#FF0000;font-weight:bold"></span><br />
<br /><b>Example 2</b><br /><br />
<select id="2" name="2" size="6" style="width:125px">
</select>
<input type="button" onClick="putIn(1,2)" value="Take Away"><input type="button" onClick="getData(2)" value="Get Data">&nbsp;<span id="2_show" style="color:#FF0000;font-weight:bold"></span><br />
<br /><br /><input type="submit" name="show" value="Get Full Data" />
</form>
</body>
</html>


I just took a look at your page again, and I figured out why it says null, instead of just adding it to the other select box, you need to select it.

Nile
05-29-2008, 10:24 PM
I'm not getting a response.