PHP is trying to execute my javascript

I am having an odd problem.

I have a chunk of javascript that I need to insert into my payment page view file.

But PHP is trying to execute it!!!????!!!





$stripescript2 = <<<JS2


    <script type="text/javascript">

    var $form = jQuery("#payment-form");

    $form.submit(function(event) {

        // Disable the submit button to prevent repeated clicks:

        $form.find(".submit").prop("disabled", true);


        // Request a token from Stripe:

        Stripe.card.createToken($form, stripeResponseHandler );


        // Prevent the form from being submitted:

        return false;

    });

    </script>

JS2;


$this->registerJs( $stripescript2 , \yii\web\VIEW::POS_READY);



Here is the PHP error…


PHP Notice – yii\base\ErrorException


Undefined variable: form

1. in /home/nginx/public/basic/views/checkout/checkout.php at line 101


</script>

JS1;

 

$this->registerJs( $stripescript1 , \yii\web\VIEW::POS_END);

 

$stripescript2 = <<<JS2

 

    <script type="text/javascript">

    var $form = jQuery("#payment-form");

    $form.submit(function(event) {                <<------------   ERROR ON THIS LINE

        // Disable the submit button to prevent repeated clicks:

        $form.find(".submit").prop("disabled", true);

 

        // Request a token from Stripe:

        Stripe.card.createToken($form, stripeResponseHandler );

 

        // Prevent the form from being submitted:

        return false;

    });



Thanks

-John

You must escape "$" when using heredoc syntax:




var \$form = jQuery("#payment-form");



AH HA!!!

That was it. I had not thought about needing to escape anything with HEREDOC.

I figured it was something simple I was missing.

Thanks

-John

You should use some IDE or editor with syntax highlighting - it is pretty obvious then. :)

Not with PHPStorm. It did not give me any clues…

Then again… It was not really an error. It was just not what I wanted.

PHP was trying to replace $form in the text, but $form was not defined anywhere…

-John

That’s odd. This is how it looks in NetBeans: