lollew
lollew

Reputation: 43

is it valid to use php inside jquery?

I want to change a jquery command via PHP.

So for example:

$envelope<?php echo'1'?>.toggleClass( 'card-out' );

is this valid to use? I tried it and i think it didn't work, but i don't know if the mistake is in that line or in the rest of the code.

this is the main part of my code:

<div class="library-card envelope<?php echo'2'?>">
    <div class="front">
        <p class="stamp">Jacob Haase<br> 18.05.2000</p>
    </div>
    <div class="card">sdsdsd</div>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/javascript">
        var $envelope = $( '.library-card.envelope' ),
            $card = $envelope.find( '.card' ),
            $front = $envelope.find( '.front' );

        $front.on( 'click', function( event ) {
            $envelope<?php echo'2'?>.toggleClass( 'card-out' );
        });

        $card.on( 'click', function( event ) {
            $envelope<?php echo'2'?>.toggleClass( 'card-out' );
        });
    </script>

</div>

thank you!

Upvotes: 4

Views: 4111

Answers (2)

maryam p
maryam p

Reputation: 69

If you are trying to bound some PHP code with the click event then this is impossible in the way you are trying and PHP code will be executed as soon as page load without waiting for a click event.

If you are trying to generate final javascript or jquery code using PHP then this is okay.

for more info Is it okay to use PHP inside a jQuery script?

for your problem use like this

$(".envelope"+<?php echo'1';?>).toggleClass( 'card-out' );

for Learning http://wptricks.co.uk/include-php-variables-into-jquery/

Upvotes: 1

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

It's perfectly valid if you are using jQuery in .php file.

In this line:

$envelope<?php echo'1'?>.toggleClass( 'card-out' );

This will be $envelope1.toggleClass('card-out');, but I'm not sure if you have $envelope1 jQuery variable. But may be, you are trying to do like this:

$('.envelope1').toggleClass('card-out');

If so, use:

$(".envelope"+<?php echo'1';?>).toggleClass( 'card-out' );

Upvotes: 3

Related Questions