Jump to content

can't retrieve values from Multiple selected list


Cyjm1120
Go to solution Solved by Barand,

Recommended Posts

Hi everyone, I am having trouble passing/displaying the values inside of a selected list.
I created a add/remove list using Jquery and I tried to display the values passed using foreach and for loops but it is still not working. The values I am trying to get are $existing_mID[$j], which is inside of the option value attribute.

Please kindly let me know what should I do in order to get the values and I really appreciate your help.

<?php
$selected = $_POST['selectto'];
if(isset($selected))
{
    echo "something in selected<br />";
    for ($i=0;$i<count($selected);$i++)
    echo "selected #1 : $selected[$i]";
    foreach ($selected as $item)
    echo "selected: item: $item";
}
?>

This is the form

<form> 
<select name="selectfrom[]" id="select-from" multiple="" size="10"> 
<?php $j=0; foreach($existing_mTitle as $item) { ;?>
<option value="<?php $existing_mID[$j];?>" > 
<?php echo "$existing_mID[$j] & $item";$j++;?> 
</option> <?php }?> 
</select> 
<a href="JavaScript:void(0);" id="btn-add">Add »</a> 
<a href="JavaScript:void(0);" id="btn-remove">« Remove</a>

<select name="selectto[]" id="select-to" multiple="" size="10"></label> </select> 
<input type="submit" name="addArticle" value="Add" /> </form>

Below is my Jquery code that I implemented.

$(document).ready(function() {


    $('#btn-add').click(function(){
        $('#select-from option:selected').each( function() {
                $('#select-to').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
                $('#select-to option').attr('selected',true);
            $(this).remove();
        });
    });
    $('#btn-remove').click(function(){
        $('#select-to option:selected').each( function() {
            $('#select-from').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
            $('#select-to option[value=' +$(this).val()+ ']').attr('selected',true);
            $(this).remove();
        });
    });


});
Edited by Cyjm1120
Link to comment
Share on other sites

I don't see that your form has a method defined - or even an action. I think it typically defaults to GET if you don't define it as post. Even if you want to post to itsself, at least add the action attribute with an empty value. Try

 

<form action="" method="post">

 

Second, you have this

 

<?php
$selected = $_POST['selectto'];
if(isset($selected))

 

$selected will ALWAYS be set because you set it on the line right before you check it!

 

Try

 

<?php
if(isset($_POST['selectto']))
{
    $selected = $_POST['selectto'];
    echo "something in selected<br />";
    foreach ($selected as $idx => $item)
     {
         echo "selected: {$idx}: {$item}<br>\n";
     }
}
?>
Link to comment
Share on other sites

Hi,

I am sorry that I didn't include that in the code. 

 

As for the foreach loop that you provided, the results displayed was 

 

Selected 0:

Selected 1:

Selected 2:     (when I selected 3 values.)

 

However I still think that the original values set within the option value attribute are not properly displayed.

 

For example, if my selected $existing_mid[$j] options was 3. 5, 7. I would like the foreach loop to display 3,5,7.

 

Is there a way to do that?

 

In addition, your suggestion to move   $selected = $_POST['selectto']; inside of the if statement made my code notice-free when some fields are not selected.

 

Thank you so much for your attention and I appreciate your help.

Edited by Cyjm1120
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.