single quote is not working inside form [duplicate]

0

My code is not accepting to be written with single quotation marks, so it can stay within echo .

How do you resolve this?

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>
    ...";
    
asked by anonymous 02.05.2017 / 22:27

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 quotation marks of echo "..."; , you should "escape" the quotation marks like this:

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

Other than $ of jQuery or other variables within "normal quotes" can be confused with PHP variables

However escaping multiple quotes is something that takes a lot of time, so I recommend two simpler ways if you have more HTML in the script than PHP would in fact be interesting to isolate PHP and HTML using alternative PHP syntax , 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 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:

02.05.2017 / 23:26