-2

I am having issues inserting a php variable into my javascript code. I have a php page that calls javascript code using a tag. Doing just that, I am able to successfully run the javascript on the php page. However, the javascript code breaks as soon as I try and insert a php variable.

One method I tried was writing the script code like normal, and inserting into the script, but that breaks the page.

$(document).ready(function() {

    $('#calendar').fullCalendar({
        defaultDate: this.date,
        editable: false,
        eventLimit: true, // allow "more" link when too many events
        events: [
            {
                title: <?php 
                $em = '[email protected]';
                echo $em; ?>,
                start: '2015-02-01'
            }]
    });

});

My other attempt was to try and contain all of the above code (including tags) as a php string and then print/echo it to the page, but that didn't work either (for some reason it just printed '0').

What is the best way to achieve what I'm going for?

8

3 Answers 3

2

You have to add quotes to your javascript code because the mail address is a string:

title: '<?php 
$em = '[email protected]';
echo $em; ?>',
start: '2015-02-01'
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it was the quotes that I was missing. I guess I was thinking it would be interpreted as a string automatically because I would have been able to put a javascript variable there without the quotes. But the php interpreter turns the php variable from variable form to text by the time it reaches the script.
2

You can pass value from php to java script. Please check following example to pass value from .php file to .js -

example.php

 <?php 
    //exp_var value we need to pass
    $exp_var = 'hello';    
    ?>
    <script>
    var php_value_of_var = <?php echo $exp_var ?>
    </script>

After create above changes in php you can use 'php_value_of_var' in your jscript directly.

'php_value_of_var' - it indicates value which passed from php file.

Comments

1

Solution 1:

Using php tag inside jquery will work only of your file extension in which your javascript exist is .php otherwise assign this into a variable in your header php file as below

<?php
     var EMAIL = '<?php echo $email; ?>';
?>

And then use it in your script like below

$(document).ready(function() {

    $('#calendar').fullCalendar({
        defaultDate: this.date,
        editable: false,
        eventLimit: true, // allow "more" link when too many events
        events: [
            {
                title: EMAIL,
                start: '2015-02-01'
            }]
    });

});

But make sure that your header file included just before the jScript

Solution 2

If your javascript is already in .php extension file you can directly use like

$(document).ready(function() {

    $('#calendar').fullCalendar({
        defaultDate: this.date,
        editable: false,
        eventLimit: true, // allow "more" link when too many events
        events: [
            {
                title: '<?php echo $em; ?>',
                start: '2015-02-01'
            }]
    });

});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.