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 8426295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:30:24+00:00 2026-06-10T04:30:24+00:00

I have this javascript that i want to run in php, the code bellow

  • 0

I have this javascript that i want to run in php,
the code bellow is supposed to be submitted in a form and then then prints it
but when submitted the javascript doesn’t execute, the output is simply

var text = document.getElementById(\'course1\').options[document.getElementById(\'course1\').selectedIndex].text; document.write(text);

this is the whole thing,

            echo "<form name\"find\" action=\"postEnrolled.php\" method=\"get\" class=\"required\" onsubmit=\"return validate(this);\">";
            echo "<table width=\"225\" border=\"0\" align=\"center\" >";
            echo "<tr>";
            echo "<td width=\"181\"><label>Course#1:</label> <select name=\"dep1\" style=\"width:190px\" class=\"dep1\">";
            echo "<option selected=\"selected\" value=\"\">Select Department</option>";


            include('db.php');
            $sql=mysql_query("select id,data from data where weight='1'");
            while($row=mysql_fetch_array($sql))
            {
                $id = $row['id'];
                $data = $row['data'];
                echo '<option value="'.$id.'">'.$data.'</option>';
            } 


            echo "</select><br/></td>";
            echo "<td width=\"267\">";


            echo "<label>&nbsp;</label><select name=\"course1\" class=\"course1\" style=\"width:200px\">";
            echo "<option selected=\"selected\" value=\"\">Select Course</option>";

            echo "</select>";

            echo "<input type=\"hidden\" name=\"course_1\" value=\"

            <script language='javascript' > 

                    var text = document.getElementById('course1').options[document.getElementById('course1').selectedIndex].text;
                    document.write(text);


            </script>

Am I missing something?
what I really want is to submit the text in the options and not the value of the options.

  • 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-10T04:30:26+00:00Added an answer on June 10, 2026 at 4:30 am
    1. getElementById will only find an element whose id is course_1, not the name.

    2. Don’t put the script element inside the input element

    3. You must have the DOM ready when calling it (use document.onload=function(){...yourcodehere...};)

    At first sight, there is no PHP really involved in this problem. But are you aware that the code, as it is, wouldn’t be executed when you change the value of the option ? If that’s what you need, use onchange="yourcodehere;". But as it is an hidden field, maybe you should describe what you really want to achieve.

    EDIT :

    If what you want is change the hidden input when the user selects another option, here’s how you can do it :

     <input type=hidden name=course_1 id=course_1>
     <select onchange="document.getElementById('course_1').value=this.options[this.selectedIndex].text;" ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an index.php file that loads other php files via this javascript/ajax code:
I have this array, that I want to parse into javascript. $offerText[] = (
I have a JavaScript app that produces a string. I want to transfer this
I have this Javascript function that sends username and password to php file through
I have this JavaScript code that creates a table, and puts in the rows
I have this javascript code below that uses jquery, it is suppoed to be
I have this chunk of javascript that's kind of hacked around from http://www.developphp.com/view.php?tid=1248 and
I have this code: <script id=toModify > Invalid javascript that will cause errors </script>
I want to have horizontal lists that can run as wide as possible but
I have a javascript that does this (http is your XMLHttpRequest object) var r

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.