1

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.

3 Answers 3

6
  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;" ...
Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying to submit the text of an option instead of its value, i cant not change the values of the options cause it will ruin everything, i just edited the code for more information
What you want is fill the hidden field when the user selects another course ?
yea basically, whenever the user chooses a course, i want to submit the course name (which is $data) and not the value ($id). I understand i can run another query to get the data but i don't want to run so many queries
1

Your problem is that you're putting a <script> tag inside of the value attribute of the <input> tag. That isn't valid HTML or JavaScript and will not work.

Comments

0

why you don't post what is the actual error you got? actually this approach (including the javascript code into php tags) is not good at all.if you want to use javascript on any page, you just have to put it on the very top of your page under the script tags. try it, will help u ..!

1 Comment

I don't get any errors, it's just not executing at all, it justs outputs the javascript code. putting it on top of the page wouldn't do want I want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.