Tim
Tim

Reputation: 273

How do I include a window close command in HTML assembled with PHP?

I am attempting to assemble some html with PHP and then echo it out. The HTML includes a button and I want the window to close when the button is clicked. I realize the example I am providing could be done without the PHP. I have simplified my code to the lowest level, so therefore the php vars used in the string have been removed to provide the simplest code that recreates the problem.

When executing this code, I get the following error:

SyntaxError: Unexpected token '}'

The error goes away when I remove the onclick attribute on the button.

Can someone help with the proper syntax for including the window close command, when assembling the html code using PHP?

<?php
?>

<!DOCTYPE html>
<HTML>
<HEAD>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

<SCRIPT>
    $( document ).ready(function() {
        $('#header_wrapper').addClass('ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all');

        $('input[type=button]').hover(function() {
            $(this).toggleClass('ui-state-hover');
        });
    });
</SCRIPT>
</HEAD>

<BODY>
    <div id="groups_bg">
        <?php
            $mtext = '<p>Test Request.</p>' .
                     '<div id="btn_area">' .
                        '<div style="text-align:center;">' .
                            '<input type="button" id="ok" name="ok" value="OK" onclick=window.open("", "_self", ""); window.close();>' .
                        '</div>' .
                    '</div>';     
            echo $mtext;
        ?>
    </div>
</BODY>
</HTML>

The result I am wanting to achieve is that the window closes when the button is pressed.

Upvotes: 0

Views: 61

Answers (1)

Miroslav Ćurčić
Miroslav Ćurčić

Reputation: 146

Wrap whole onclick attribute in double qoutes:

  .'<input type="button" id="ok" name="ok" value="OK" onclick="window.open(\'\', \'_self\', \'\'); window.close();">'

Note that all qoutes inside attribute are single-quote and that they must be escaped with "\" to not terminate PHP string.

Upvotes: 1

Related Questions