Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8035125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:13:19+00:00 2026-06-05T02:13:19+00:00

Disclaimer: I’m an iOS developer with little JQuery, PHP or MySQL experience. Here’s the

  • 0

Disclaimer: I’m an iOS developer with little JQuery, PHP or MySQL experience.

Here’s the deal: I have a MySQL database containing Film info (FilmTitle, FilmGenre, FilmDirector, etc).

My goal is to:

  1. Grab just the Film Titles from the database and store them in an Array.
    I’m using PHP for this and its working fine.
  2. Pass this PHP Array to a JavaScript Array.
    This also works.
  3. Finally, using the JavaScript Array, I want to populate a drop-Down menu with its contents using createElement().
    This does NOT work.

I’m getting all sorts of goofy stuff: Some document.write() commands are working and some aren’t; The drop-down menu isn’t getting populated; etc.

First question: Is this even a good strategy to begin with? I figured that as the number of films in the database changes, the only way to properly update the drop-down menu would be to do what I described.

Second Question: This should all be in a PHP file, not an HTML file, correct? But can PHP even execute JavaScript functions? Or does something special need to be done to make this work?

The code

<?php
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
echo "<allFilms>";

$link = mysql_connect(" . . . .  ");
$query = "select * from FilmsSmallTable";
$result = mysql_query($query);
// Declare array variable:
$allFilmTitles = array();

    while($row = mysql_fetch_assoc($result))
    {
        echo "<film>";

            echo "<FilmName>";
            echo $row['FilmTitle'];
            array_push($allFilmTitles, $row["FilmTitle"]);
            echo "</FilmName>";

        echo "</film>";
    }

// Print array out to verify:    
echo "Here is the array's full contents:<br/>";
print_r ($allFilmTitles);

//convert array to string using ":#:" as separator
$stringedArray = implode(":#:",$allFilmTitles);

echo "<br/><br/>";
echo "PHP Array converted to a PHP String (the 'stringedArray' variable) = <br/>";
print_r ($stringedArray);
echo "</allFilms>";
?>

<html>
<head>
<title>PHP & JS</title>

<script type="text/javascript">

function showArray() {
   //Convert PHP string to JavaScript string
   var JSArrayString = "<? print $stringedArray; ?>";

  //Create JavaScript array
   var JSarray = new Array();

   var x;
   //Fill JavaScript array from the converted string   
   JSarray = JSArrayString.split(":#:");
   // And sort it:
   JSarray.sort();

   //document.write("JSarray's length = ", JSarray.length);
   document.write("JSarray contains: ", JSarray);

   // reset the Form's drop-down menu to contain no elements:
   FilmsDropDownMenu.length = 0;

   for (counter = 0;  counter < JSarray.length; counter++) {
    document.write("Counter = " + counter + "<br/>");
    var newMenuItem=document.createElement("option");
    newMenuItem.text = JSarray[counter];
    try {
        //currentMenu.add(newMenuItem, null); // standards compliant
        FilmsDropDownMenu.add(newMenuItem, null); // standards compliant
    }
    catch(ex) {
        //currentMenu.add(newMenuItem); // IE only
        FilmsDropDownMenu.add(newMenuItem);
    }
}


function displayUsersChoice() {
// Grab the INDEX number of the choice the user selected from that menu:
var tempMenu = document.getElementById("FilmsDropDownMenu").selectedIndex; 
choice = tempMenu[choiceSelected].value;
alert ("You chose: " + choice);
}

</script>


</head>

<body onload="showArray()">
Hello!

<form id="myForm">
<select id="FilmsDropDownMenu" onChange="displayUsersChoice()">
    <option>==Choose Film to Edit:==</option>
    <option>Star Wars</option>
    <option>Raiders</option>
    <option>2001</option>
</select>

<input type="submit" value="Display Array" onclick="showArray()" />
</form>

<br/>

</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:13:20+00:00Added an answer on June 5, 2026 at 2:13 am

    This is just an example how you can create the drop down directly from PHP. Sometimes the easy way is the right way:

    $allFilmTitles = array("Godfather", "Colombo", "Naked Gun");
    print '<select name="FilmsDropDownMenu" id="FilmsDropDownMenu">';
    
    foreach ($allFilmTitles as $film)
    {
        print "<option>{$film}</option>";
    }
    
    print "</select>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: I have little experience with linq. One of my tasks at my work
Disclaimer: JS novice I have a JS widget that depends on JQuery. The widget's
Disclaimer: I am new to python and django but have Drupal programming experience. I'm
Disclaimer: I don't have a whole ton of experience with Java Generics, but my
Disclaimer: Never used php in my life. New to Unix and Jenkins I have
disclaimer: I must use a microsoft access database and I cannot connect my app
Disclaimer, I'm not a PHP programmer, so you might find this question trivial. That's
Disclaimer: I am a C# guy with virtually no VS C++/MFC experience. I am
Disclaimer : I have seen the exact questions, but have not seen any answers
Disclaimer: I am new to django but have coded in Drupal I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.