simple quotes not working inside form

Asked

Viewed 428 times

0

My code is not accepting to be written with simple quotes, so it can stay inside the echo.

How to resolve this situation?

The line with error: $("input[name='enable']")

echo
    " ...
    <script type='text/javascript'>
        ...
        $("input[name='enable']").click(function(){
            if ($(this).is(':checked')) {
                $('input.textbox:text').attr('disabled', false);
            } else if ($(this).not(':checked')) {
                var remove = '';
                $('input.textbox:text').attr ('value', remove);
                $('input.textbox:text').attr('disabled', true);
            }
        });
        ...
    </script>
    ...";
  • To make it clear, the problem is to escape the double quotes inside the string in PHP. As quoted in the question I quoted, just do $(\"input[name='enable']\"), so the string will not be finished in PHP.

1 answer

0

You start with:

echo
    " 

And in the middle of the code is this:

"input[name='enable']"

In other words, the quotation marks of "input[name='enable']" are "breaking" the quotes of the echo "...";, you should "escape" the quotes like this:

echo
    " ...
    <script type='text/javascript'>
        ...
        $(\"input[name='enable']\").click(function(){

Aside from the $ jQuery or other variables within "normal quotes" may be confused with PHP variables

However escaping multiple quotes is something that takes a lot, so I recommend two simpler ways, if you have more HTML in the script than PHP would actually be interesting to isolate PHP and HTML using alternative syntax of PHP, so for example:

<?php if (codição): ?>
    <script type='text/javascript'>
        ...
        $("input[name='enable']").click(function(){
            if ($(this).is(':checked')) {
                $('input.textbox:text').attr('disabled', false);
            } else if ($(this).not(':checked')) {
                var remove = '';
                $('input.textbox:text').attr ('value', remove);
                $('input.textbox:text').attr('disabled', true);
            }
        });
        ...
    </script>
<?php endif; ?>

Another way is to use the Heredoc:

echo <<<EOF
    <script type='text/javascript'>
        ...
        $("input[name='enable']").click(function(){
            if ($(this).is(':checked')) {
                $('input.textbox:text').attr('disabled', false);
            } else if ($(this).not(':checked')) {
                var remove = '';
                $('input.textbox:text').attr ('value', remove);
                $('input.textbox:text').attr('disabled', true);
            }
        });
        ...
    </script>
EOF;

Documentation:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.