Hi, I'd playing with JavaScipt nowadays. I'm trying to display the selected value in a dropdown list (those values are the array elements).

Here is what I've tried so far:

<html>
<head>
<script type="text/javascript">
window.onload = function() {
var artists_arr = new Array();
artists_arr[0] = "The Beatles";
artists_arr[1] = "The Doors";
artists_arr[2] = "The Cascades";
var Artists = document.getElementById("Artists");
for (i=0;i<artists_arr.length;i++)
{
    var Entry = document.createElement("option");
    Entry.text = artists_arr[i].toUpperCase();
    Artists.add(Entry ,null);
}

var strUser = Artists.options[Artists.selectedIndex].text;

}
</script>

<title>Artists Page</title>
</head>
<body>

<Form method="POST" action="artists.php">
Select your favorite artist: <select id="Artists" name="artists"></select><br />
<input type="submit" name="submit" value="Ok" />

</Form>

<?php if(!isset($_POST['submit'])){

} else{
echo "Your favorie artist is: ";
}

?>
</html>

In the echo "Your favorie artist is: "; I'd like to display the selected value. I need your help. Thanks I would appreciate it...

Try this

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function(){
    var artists_arr = new Array();
    artists_arr[0] = "The Beatles";
    artists_arr[1] = "The Doors";
    artists_arr[2] = "The Cascades";
    for (i=0;i<artists_arr.length;i++)
    {
        $('#Artists').append('<option value="'+artists_arr[i].toUpperCase()+'">'+artists_arr[i].toUpperCase()+'</option>');
    }

    <?php 
if(isset($_POST['submit'])){
?>
        var val = '<?php echo $_POST['artists']; ?>';
        $("#Artists option").filter(function() { return $(this).text() == val;}).attr('selected', true);
<?php
}
?>
});     
</script>

<title>Artists Page</title>
</head>
<body>

<Form method="POST" action="#">
Select your favorite artist: <select id="Artists" name="artists"></select>
<input type="submit" name="submit" value="Ok" />

</Form>

<?php if(!isset($_POST['submit'])){

} else{
echo "Your favorie artist is: ".$_POST['artists'];
}

?>
</html>

Thanks it works! But how about in JavaScript? I noticed you used jQuery in here.

Hi @bettybarnes;

I like to use jquery, its very easy for me..
you check the logic what I used..and try..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.