1

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

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi qumonio, Many thanks for the very prompt response. I have been busy over the past couple of days trying to get this to work and have found a couple of things. One, I am stupid. I had a couple of copies of this form on the same page as I was experimenting with it and they all had the same ID in them. This is why it was not working and after correcting this, it works, both with .value or ,selectedIndex. There is still a problem though and I will go into detail in the next comment. Thanks again, Stan...
The other problem is: When the item is selected that triggers the appearance of the additional select choice fields (notinoz), and then you click to search (php search process that gives results on a new page), when you return to the original page, "notinoz" is still in the search field, but the additional dropdown box is gone. This occurs when this search page is a part of a php include. Access it as a standalone page, it stays. On a combined "include page" it is hidden, but the value is still there. Any thoughts? Thanks again, Stan...
Just tried it and you're right. I've been doing it the long way all this time :D .value works just fine.
Regarding the second problem; It might be more suitable to make a 2nd post or re-write this post. As for a solution: I think using an onload event listener to handle showing the addition dropdown would help.
Thanks again for your thoughts and I hope you had a nice holiday. I had already been looking into some kind of event listener with "onload", but have finally settled on using target="_blank" when I call the perl search process. This gives the search results in another tab or page, depending upon the browser, and leaves the original search form untouched. This avoids the secondary field's unintentional disappearance on page return, as the original page was left as is when the other tab/page opens. May not be ideal under all situations, but it works for me...Thanks again, Stan...

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.