JohnT
JohnT

Reputation: 65

PHP echo javascript to change iframe source

I have a text input search form for support tickets. If the user inputs a ticket id (checked using a PHP if statement), the background color of the search box is changed to yellow (using a PHP echo) to highlight the search text entered. This part works.

I would like to also change the source value of an iframe as part of the PHP if condition like this:

    <td align="left">
        <input type="text" name="ticket" id="ticket" placeholder="Ticket" value="<?php echo $this->state->ticket;?>" class="input-mini search-query" onchange="document.adminForm.submit();"
        <?php if ($this->state->ticket != "")
            {
            echo 'style="background-color: yellow;"';
            echo '<script language="javascript">';
            echo 'document.getElementById("ticketIframe").contentWindow.document.location.href="http://myurl.com"';
            echo '</script>';       
            }?>/>
    </td>

The javascript function (document.getElementById...) displays on the page rather than executing. I tried a simple javascript alert, but got the same result.

Suggestions?

Upvotes: 0

Views: 225

Answers (1)

charlietfl
charlietfl

Reputation: 171700

Need to close the input tag before you insert script tag. You simply are generating invalid html that looks like:

<input <script></script> />

You get unexpected results when html is invalid

Upvotes: 2

Related Questions