Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need your help. I have a problem with dropdown value. I can't save value from dropdown list,which has appeared.


I did everything like this example, but I have appearing , not . And the problem is - I cant save value I chosen. I need this value for insert into my database.What must I change here?


[http://jsfiddle.net/Bksk7/2/]


myview.php

<script>
$(function() {
$(".forms").hide();
$("#type").change(function() 
{
    var e = $(this).val();
    if(e == 1 || e == 2) //for example, I have 2 another dropdown
    {
        $(".forms").hide().parent().find('#' + e).show();
    }
});

$('#submit').click(function(e)
{
    e.preventDefault();

    var resolve_data = function() {
        var output = {};
        $('.form-active select, .default select').each(function() 
        {
            output[$(this).attr('value')] = $(this).val();
        });
        return output;
    };
 });
});

</script>

<form action="http:/../insertdata" method="post"> 
<table class="main">
...
<select name="type" id="type"> //it works clear__ dropdown #1
    <?php
    $selected_1 = 'selected="selected"';
    $selected_2 = '';

    foreach ($types as $item) 
    {
        $id=$item['type_id'];
        $selected = $selected_2;
        if (isset($_POST['type']) && $_POST['type'] == $id)
        $selected = $selected_1;
        echo '<option value="'.$id.'" ' . $selected . '>'.$item['title'].'</option>';
    }       

    ?>
    </select>
....
<tr class="forms" id="1">
<td>
Введите категорию:
</td>
<td>
<select name="kategory"  >    //___dropdown #2
    <?php
    $selected_1 = 'selected="selected"';
    $selected_2 = '';

    foreach ($categories1 as $item) 
    {
        $id=$item['id'];
        $selected = $selected_2;
        if (isset($_POST['kategory']) && $_POST['kategory'] == $id)
        $selected = $selected_1;
        echo '<option value="'.$id.'" ' . $selected . '>'.$item['title'].'</option>';
    } 
    ?>

    </select>
</td>
</tr>

//and another the same
<tr class="forms" id="2">
<td>
Введите категорию:
</td>
<td>
<select name="kategory"  >   //__dropdown #3
    <?php
    $selected_1 = 'selected="selected"';
    $selected_2 = '';

    foreach ($categories2 as $item) 
    {
        $id=$item['id'];
        $selected = $selected_2;
        if (isset($_POST['kategory']) && $_POST['kategory'] == $id)
        $selected = $selected_1;
        echo '<option value="'.$id.'" ' . $selected . '>'.$item['title'].'</option>';
    } 
    ?>

    </select>
</td>
</tr>
....
<input type="submit" id="submit" "name="add" value="Добавить" />
</form>

controller insertdata.php

....
//I do this for values from dropdown #2 and #3
$add['categories1']=$this->ych_material_model->get_categories('programmnie');
$add['categories2']=$this->ych_material_model->get_categories('teoreticheskie');

And thats all right, but when I call .... $add['kategory']=$this->input->post('kategory');

in controller, it give me '1'.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

I'm not sure this what you asked but to retrieve the current value of a select you just have to do $('#myselect').val()

For the php side:

<form action="process.php" method="post">
    <select name="myselect">
        <option value="1" selected="selected">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>

    <input type="submit"/>
</form>

Be sure to have a default value selected and to have chosen a method!

On process.php :

<?php echo 'the value selected from my select : " . $_POST["myselect"]; ?>

Simple as that.

share|improve this answer
    
I did what you said, but it still recording into database the value '1'.. =( –  Maria Jun 4 '13 at 15:39
add comment

from what i can see, your resolve_data function is only fetching the values from the inputs, the dropdown is a select element, not an input, so you need to modify it to also get the values from the select. additionally, add the name attribute to your select.

share|improve this answer
    
Hm.. I can take value from my first drop down, and now, when I added this code (I don't understand $.ajax and so on), I cant take value of selected option. I edited what you say, and it show me msg I dont want it to be shown, and still not record this value =( –  Maria Jun 4 '13 at 15:36
    
i'm not really sure what you are asking here, could you clarify with some example please? –  Julien Jun 4 '13 at 15:52
    
I have dropdown #1. When I choose smth in it another dropdown #2 must appear. It works clear. BUT. When I'm recording index from dropdown #2 into my database, it record index = 1. Not 2, not 3. Just 1. Always. I dont know what else I should write. –  Maria Jun 4 '13 at 16:45
    
then you should show us your PHP code as well as the JS so that we can see what you are doing there. –  Julien Jun 4 '13 at 16:47
    
I added something:) –  Maria Jun 4 '13 at 17:50
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.