FahadAkram
FahadAkram

Reputation: 465

I want to alert php date as attribute in jquery

I have a piece of code below and purpose is i have php date and i want to match its value in jquery

    <?php
$startDate = date("Y-m-d");
$prevDate = date('Y-m-d', strtotime($startDate .' -1 day'));
$secondPrvDate = date('Y-m-d', strtotime($startDate . ' - 2 day'));
?>

and jquery code is

 <script>
    $(document.body).on('click', '.nbs-flexisel-nav-left', function () {
       alert(<?php echo $startDate; ?>); 
       return false;

    });
    <script>

but when it alert it show only 2008 instead of showing previous date to current date

Upvotes: 0

Views: 509

Answers (3)

Kheshav Sewnundun
Kheshav Sewnundun

Reputation: 1244

You forgot to but the content of alert between double quotes

alert('<?php echo $startDate; ?>');

Moreover you have some invalid elements:

script elements must be closed by their corresponding closing elements

eg:

    <script>
    //Your code goes here
    </script>

Upvotes: 3

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

use a quote around php code in alert like

alert('<?php echo $startDate; ?>'); 

and keep closing <script> tag at the end

</script>

so full script will be

<script>
    $(document.body).on('click', '.nbs-flexisel-nav-left', function () {
       alert('<?php echo $startDate; ?>'); 
       return false;
    });
</script>

Upvotes: 1

Sadikhasan
Sadikhasan

Reputation: 18600

Use double quote to display whole date.

alert("<?php echo $startDate;?>"); 

Instead Of

alert(<?php echo $startDate;?>); 

Upvotes: 2

Related Questions