Stan
Stan

Reputation: 63

Javascript on not working in PHP page include

I am using php to generate pages on my website. Fine there. I am trying to include a site wide generic search box by using the include function so as when I add or modify to this search box, I just have to work on one page, not every single page on my site. Fine there also.

The problem arises when I am now trying to modify the search box page to have another choice selection field appear (using javascript) when the "other" selection is chosen on the basic search form. I have the coding set up and it works fine when I access the search page directly, but when I have the search page embedded in another page using the include function, the javascript function does not work. It will also work fine if I embed the search page using iframe, but I do not want to use iframe and have switched over to the php include function wherever possible.

The example I am using is from another stackexchange question

Here is the code used from the other stackexchange question so you do not have to bounce back and forth.

<script type="text/javascript">

function test() {
if (document.getElementById("state").value == "notinoz") {
    document.getElementById("extra").style.display = "block";
} else {
    document.getElementById("extra").style.display = "none";
}
}

</script>

<P>
<select id="state" name="state" style="width: 212px;" onclick="test()">
<option value="nsw">New South Wales</option>
<option value="qld">Queensland</option>
<option value="vic">Victoria</option>
<option value="nt">Northern Territory</option>
<option value="tas">Tasmania</option>
<option value="sa">South Australia</option>
<option value="wa">Western Australia</option>
<option value="act">Australian Capital Territory</option>
<option value="notinoz">Not in Australia</option>    
 </select>

<select id="extra" name="extra" style="display: none">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

include coding from any page on site:

HTML;
include("/home/seocom5/public_html/postcard/iframe_pc_eph_mob.php");
echo <<<HTML

Like I said, works fine if search page accessed directly, or by embedding using iframe, but does not work by embedding with php include.

Thanks for any thoughts on this one, Stan...

Upvotes: 1

Views: 86

Answers (1)

ŽaMan
ŽaMan

Reputation: 396

I just realized you're trying to get the <select> .value when you want its .selectedIndex which just gives the index of the selected <option> inside that <select>.

From there you can use the index as document.getElementById("state")[document.getElementById("state").selectedIndex] or some variation.

Upvotes: 1

Related Questions