Page 1 of 1
Dropdown box, how do I get the selected option?
Posted: Sat Dec 09, 2006 1:52 am
by Mythic Fr0st
How do I set
$_SESSION['currmonster']=(theselected option of my dropdown box)
if only I knew what to call it I could figure it out
Any help please & thanks
Posted: Sat Dec 09, 2006 4:11 am
by neel_basu
Method #1 ( If Only 1 option Would be Selected )
===================================
Code: Select all
$_SESSION['currmonster']=$_POST['select_field_name'];
Method #2 ( If multiple options Would be Selected )
====================================
Code: Select all
<select size="2" name="select_field_name[]" multiple>
<option value="val1">val1</option>
<option value="val2">val2</option>
</select>
Code: Select all
if(!empty($_POST['select_field_name']))
{
foreach($_POST['select_field_name'] as $val)
{
$_SESSION['currmonster'][] = $val;
}
}
Method #3 (Use A hidden Field To Store To Store The Value Of The Drop Down Box)
==========================================================
Code: Select all
<input type="hidden" id="hid_select" value="" name="hid_val">
Code: Select all
<form name="form_name">
<select size="1" name="select_field_name" onclick="pass_val()">
<option value="val1">val1</option>
<option value="val2">val2</option>
</select>
</form>
Store The Selected Value In A Hidden Field
Code: Select all
function pass_val()
{
var hld_slct = document.form_name.select_field_name;
var get_slct = hld_slct.options[hld_slct.selectedIndex].value;
var hld_trgt = document.getElementById("hid_select");
hld_trgt.value = get_slct;
}
And Then Track The Value Of That Hidden Field By php
Code: Select all
$_SESSION['currmonster']=$_POST['hid_val'];
ty
Posted: Sat Dec 09, 2006 3:34 pm
by Mythic Fr0st
Thanks, I basically did the same thing except
$_SESSION['currmonster']=$_POST['name']
works, why the hidden field
Posted: Sat Dec 09, 2006 9:35 pm
by neel_basu
Cause Using Hidden Fields Is one of The Method
as I Was Told You About 2 Methods Why Would Left out The Another Method .
tried the last one
Posted: Sat Dec 09, 2006 11:12 pm
by Mythic Fr0st
I tried the last one, it didnt work -.-
Didnt get a value at all o well thanks for trying
Posted: Sat Dec 09, 2006 11:17 pm
by neel_basu
What ??
What was The Error Message ??
Posted: Sat Dec 09, 2006 11:24 pm
by neel_basu
You Have To Insert
Code: Select all
<input type="hidden" id="hid_select" value="" name="hid_val">
Into The Form Named from_name
===============================
And It Would Look Like This
===============================
Code: Select all
<body>
<form name="form_name" method="get" action="some_file.php">
<select size="1" name="select_field_name" onclick="pass_val()">
<option value="val1">val1</option>
<option value="val2">val2</option>
</select>
<input type="hidden" id="hid_select" value="" name="hid_val">
<input type="submit" value="Submit" name="Submit">
</form>
</body>
And Put Leave The JavaScript As It is In The <head></head>
===============================
I Thought You Put The Hidden Field Out Of The Form
======================================
nop
Posted: Sat Dec 09, 2006 11:55 pm
by Mythic Fr0st
Nope, didnt work, my code is confusing o_O XD
Posted: Sun Dec 10, 2006 12:02 am
by neel_basu
Code: Select all
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script>
function pass_val()
{
var hld_slct = document.form_name.select_field_name;
var get_slct = hld_slct.options[hld_slct.selectedIndex].value;
var hld_trgt = document.getElementById("hid_select");
hld_trgt.value = get_slct;
}
</script>
</head>
<body>
<form name="form_name" method=get action="http://localhost/some_file.php">
<select size="1" name="select_field_name" onclick="pass_val()">
<option value="val1">val1</option>
<option value="val2">val2</option>
</select>
<input type="text" id="hid_select" value="" name="hid_val">
<input type="submit" value="Submit" name="Submit">
</form>
</body>
</html>
Code: Select all
$_SESSION['currmonster']=$_POST['hid_val'];
Not $_POST['select_field_name']
lol
Posted: Sun Dec 10, 2006 2:44 pm
by Mythic Fr0st
Yea I know, I named them all right... just didnt work -.-, thanks anyway
Posted: Sun Dec 10, 2006 10:14 pm
by neel_basu
1 Secs Here Is The Problem
Replace
<select size="1" name="select_field_name" onclick="pass_val()">
By
<select size="1" name="select_field_name" onchange="pass_val()">