0

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...

2
Contributors
3
Replies
6
Views
4 Years
Discussion Span
Last Post by Bachu
0

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>

Edited by Bachu

0

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

0

Hi @bettybarnes;

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

Edited by Bachu

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.