Algorithmatic
Algorithmatic

Reputation: 1882

Running javascript inside php

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.

Upvotes: 1

Views: 1290

Answers (3)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382092

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

Upvotes: 6

Gaurav
Gaurav

Reputation: 628

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

Upvotes: 0

John Conde
John Conde

Reputation: 219804

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.

Upvotes: 1

Related Questions