Dropdown box, how do I get the selected option?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Mythic Fr0st
Forum Contributor
Posts: 137
Joined: Sat Dec 02, 2006 3:23 am
Contact:

Dropdown box, how do I get the selected option?

Post 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
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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'];
Mythic Fr0st
Forum Contributor
Posts: 137
Joined: Sat Dec 02, 2006 3:23 am
Contact:

ty

Post by Mythic Fr0st »

Thanks, I basically did the same thing except

$_SESSION['currmonster']=$_POST['name']

works, why the hidden field
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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 .
Mythic Fr0st
Forum Contributor
Posts: 137
Joined: Sat Dec 02, 2006 3:23 am
Contact:

tried the last one

Post by Mythic Fr0st »

I tried the last one, it didnt work -.-

Didnt get a value at all o well thanks for trying
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

What ??
What was The Error Message ??
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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
======================================
Mythic Fr0st
Forum Contributor
Posts: 137
Joined: Sat Dec 02, 2006 3:23 am
Contact:

nop

Post by Mythic Fr0st »

Nope, didnt work, my code is confusing o_O XD
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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']
Mythic Fr0st
Forum Contributor
Posts: 137
Joined: Sat Dec 02, 2006 3:23 am
Contact:

lol

Post by Mythic Fr0st »

Yea I know, I named them all right... just didnt work -.-, thanks anyway
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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()">
Post Reply