0

I have two <a></a> tags which holds the onclick event and has PHP code in it. Obviously I need them to work. What those links do - switch between months. Here are the links.

<a href="javascript:void(0);" onclick="getCalendar('calendar_div','<?php echo date("Y",strtotime($date.' - 1 Month')); ?>','<?php echo date("m",strtotime($date.' - 1 Month')); ?>');"><span class="glyphicon glyphicon-chevron-left"></span></a>
<select name="month_dropdown" class="month_dropdown dropdown"><?php echo $this->getAllMonths($dateMonth); ?></select>
<select name="year_dropdown" class="year_dropdown dropdown"><?php echo $this->getYearList($dateYear); ?></select>
<a href="javascript:void(0);" onclick="getCalendar('calendar_div','<?php echo date("Y",strtotime($date.' + 1 Month')); ?>','<?php echo date("m",strtotime($date.' + 1 Month')); ?>');"><span class="glyphicon glyphicon-chevron-right"></span></a>

As you can see I have the $this-> pointer in <select></select> tags which allows it to be in use because I am using classes and there has to be the pointer there I assume. So how do I get the tags to work? When I add the $this-> pointer to the onclick event in front of getCalendar the PHP code just breaks down.

Do I have to use some other method in order to achieve what I am after?

elki42
  • 123
  • 1
  • 10
kaspianso
  • 43
  • 7
  • Edit your code to make more readable please – Tryliom Jun 01 '18 at 08:20
  • It's important to remember that PHP doesn't know anything about `onclick` events and vice versa; all you're doing is *building some HTML*, which as far as PHP is concerned is just text. So it doesn't make any difference *to PHP* whether the code is in an `onclick` element or a `marquee` tag. – IMSoP Jun 01 '18 at 08:22
  • I reopened (silly me), but I think you should still read this: [Execute PHP function with onClick](https://stackoverflow.com/questions/19323010/execute-php-function-with-onclick) – GolezTrol Jun 01 '18 at 08:23
  • Oh I see, I then just use AJAX I have tempered with it but this will be a new chalange for me. How silly of me that I did not know that php does not work on onclick, thanks! – kaspianso Jun 01 '18 at 08:32

2 Answers2

1

The pseudo-variable $this is available when a method is called from within an object context. If your code is not part of class definition, you must create object first:

$obj = new Class();

Then use class methods with:

$obj->getAllMonths($dateMonth);.

You can read this about PHP classes.

Zhorov
  • 28,486
  • 6
  • 27
  • 52
0

check this code. you are add '<?php echo date("Y",strtotime($date.' - 1 Month')); ?>' here single cotetion remove this.

 <a href="javascript:void(0);" onclick="getCalendar('calendar_div',<?php echo date("Y",strtotime($date.' - 1 Month')); ?>,<?php echo date("m",strtotime($date.' - 1 Month')); ?>);"><span class="glyphicon glyphicon-chevron-left"></span></a>

 <select name="month_dropdown" class="month_dropdown dropdown"><?php echo $this->getAllMonths($dateMonth); ?></select>

 <select name="year_dropdown" class="year_dropdown dropdown"><?php echo $this->getYearList($dateYear); ?></select>

 <a href="javascript:void(0);" onclick="getCalendar('calendar_div',<?php echo date("Y",strtotime($date.' + 1 Month')); ?>,<?php echo date("m",strtotime($date.' + 1 Month')); ?>);"><span class="glyphicon glyphicon-chevron-right"></span></a>