Welcome Guest, Not a member yet? Register   Sign In
Get value from selected form_dropdown()
#1

[eluser]bondjp[/eluser]
Hi,
I have a form_dropdown where the user selects one item and this dropdown box is populated from database fields.
When the user selects an item how can i pass that value to a variable without submiting any form?

Here's what i have which is the standard form_dropdowm():

Quote:echo form_dropdown('whychange', $why);
#2

[eluser]dark_lord[/eluser]
Just to give you an idea:

Code:
<?php
    $options = array('Single'  => 'Single','Married' => 'Married', 'Widowed' => 'Widowed');

    echo form_dropdown('CivilStatus', $options, $this->validation->CivilStatus);
    echo $this->validation->CivilStatus_error;
?>

To be able to get the value of that particular form object, in my own way I used Javascript getElementbyId(), try to take a look at it on the http://www.w3schools.com/jsref/met_doc_g...ntbyid.asp
#3

[eluser]bondjp[/eluser]
[quote author="WishBear*" date="1268860622"]Just to give you an idea:

Code:
<?php
    $options = array('Single'  => 'Single','Married' => 'Married', 'Widowed' => 'Widowed');

    echo form_dropdown('CivilStatus', $options, $this->validation->CivilStatus);
    echo $this->validation->CivilStatus_error;
?>

To be able to get the value of that particular form object, in my own way I used Javascript getElementbyId(), try to take a look at it on the http://www.w3schools.com/jsref/met_doc_g...ntbyid.asp[/quote]

thanks.
Can you be a bit more specific about $this->validation->CivilStatus and $this->validation->CivilStatus_error ?
Sorry, i'm still newbie Sad .
#4

[eluser]dark_lord[/eluser]
Try to copy and paste the below code and see if this is what your requirement is...

Code:
<html>
<head>
<script>
function getValue()
{
  var x=document.getElementById("ops1");
  alert(x.value);
}
</script>
</head>
<body>
<form>
<select name="ops1" id="ops1">
<option value="1">One</option>
<option value="2">Two</option>
</select>
&lt;input type="button" onclick="getValue()"&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
#5

[eluser]bondjp[/eluser]
Using the link you gave me i just did this:
Code:
[removed]
function getWhy()
  {
  var x=document.getElementById("getwhy");
  alert(x[removed]);
  }
[removed]



$js = 'id="getwhy" onChange="getWhy();"';
echo form_dropdown('why', $why, 'Because I Want',$js);?&gt;


Ok, when i select an item, a pop up window appears with ALL the items in the dropdown box and not the one i chose...
#6

[eluser]dark_lord[/eluser]
can you try changing

alert(x.value);

to

alert(x.selectedIndex);

lets see if this works...
#7

[eluser]bondjp[/eluser]
Ok tried and now gives me a number.

like this:


item1
item2
item3
item4

If i select item 4 it gives me the number 3...

PS: Ok i got it it starts at 0...

Now how can i use this variable in my php code?
#8

[eluser]dark_lord[/eluser]
Workaround?

alert(x.selectedIndex + 1);
#9

[eluser]bondjp[/eluser]
[quote author="WishBear*" date="1268865245"]Workaround?

alert(x.selectedIndex + 1);[/quote]

Ok the value starts at 0 so no need to work around :)...

Now how can i pass this value and use it in my php code?
#10

[eluser]danmontgomery[/eluser]
You won't be able to pass this value directly to a PHP variable on the page, because by the time the page is rendered PHP has finished processing. If you're unsure about this, I'd suggest googling the difference between server-side and client-side programming.

What you can do, however, is use javascript to interact with the page after it's loaded. You can do this by using javascript's onChange() functionality... Normally, I'd suggest using a framework like jQuery, but I'm not sure that would be helpful without first having a grasp on javascript.

I'd suggest starting here, depending on what you want to do (show/hide fields, load content, load a url, etc) this can be relatively easy or rather complicated.




Theme © iAndrew 2016 - Forum software by © MyBB