user3097736
user3097736

Reputation: 284

Php variable echo in Jquery

How to put my php variable inside of startDate:'01-01-1996 ?

I want just like this startDate:'01-01-date' How to do that?

$curYear = date('Y');

<script type = "text/javascript"charset = "utf-8">
    $(function () {
    var date = "<?php echo $curYear; ?>";

    $('.date-pick').datePicker({
        startDate: '01-01-1996'
    });
});
</script>

Upvotes: 0

Views: 154

Answers (4)

dev4092
dev4092

Reputation: 2891

if you want to echo variable in javascript or jquery then you want to simply using one line code

<?php echo $variable_name;?>;

Upvotes: 0

vladkras
vladkras

Reputation: 17228

It's really a very bad idea to mix jQuery with PHP. Try to find another way.

If you need current year:

$(function () {
    var cur_year = new Date().getFullYear();
    $('.date-pick').datePicker({
        startDate: '01-01-'+cur_year
    });
});

If only php script can do it (e.g. if this value is stored in DB) you can use ajax

$(function () {
    $.get('path_to_script_that_returns_year.php', function(resp) {
        $('.date-pick').datePicker({
            startDate: '01-01-'+resp
        });
    });
});

where php script could be

<?php
// ...
// get your year and send
// ...
echo $var_with_year;

Upvotes: 0

D. Melo
D. Melo

Reputation: 2239

What about this?:

<script type = "text/javascript"charset = "utf-8">
    $(function () {
    var date = "<?php echo $curYear; ?>";

    $('.date-pick').datePicker({
        startDate: '01-01-<?=date("Y")?>'
    });
});
</script>

Upvotes: 0

Try

var date = <?php echo $curYear; ?>;
$('.date-pick').datePicker({startDate:date });


Updated after OP's comment

var date = '01-01-' + <?php echo $curYear; ?>;
$('.date-pick').datePicker({startDate:date });

$date = '01-01-'.date('Y'); //in php
var date = <?php echo $date; ?>;
$('.date-pick').datePicker({startDate:date });

Upvotes: 1

Related Questions