Jump to content

Onclick event calling php function from generated code


krkec

Recommended Posts

Hi

 

I created php function that creates table. I want to create onclick evant on <td> that calls php function showinfo($year,$month,$day). Variable $list_day is inserted when table is generated(local variable for function).  Variables $year,$month are global variables.

 

This is how I created <td>:

$calendar.= '<td onclick=showinfo($year,$month,'.$list_day.'); class="calendar-day_used">'.$list_day.'</td>';

This is what is generated from above code:

<td onclick="showinfo($year," $month,="" 21);="" class="calendar-day_used2">21</td>

What am I doing wrong???

 

Thanks in anvance

Link to comment
Share on other sites

if you are assembling the javascript on the server this should probably look more like...

$calendar.= '<td onclick=showinfo("'.$year.'","'.$month.'","'.$list_day.'"); class="calendar-day_used">'.$list_day.'</td>';

Hi

 

I created php function that creates table. I want to create onclick evant on <td> that calls php function showinfo($year,$month,$day). Variable $list_day is inserted when table is generated(local variable for function).  Variables $year,$month are global variables.

 

This is how I created <td>:

$calendar.= '<td onclick=showinfo($year,$month,'.$list_day.'); class="calendar-day_used">'.$list_day.'</td>';

This is what is generated from above code:

<td onclick="showinfo($year," $month,="" 21);="" class="calendar-day_used2">21</td>

What am I doing wrong???

 

Thanks in anvance

Link to comment
Share on other sites

you can't directly call a php function from javascript. php is server-side. javascript is client-side. In order to do this, you need to have js do an ajax request to a php script that can run the function and return results, and then use javascript to show updated info or whatever.

There are literally thousands of AJAX tutorials out there on the web; it is a very common thing. Most all of them involve pulling and returning info from a database when something is clicked or changed (e.g. auto-complete search, dynamic multiple dropdowns, etc.). They are all the same principle. Just google "php ajax tutorial". If you get stuck at something specific, feel free to ask for help.

Link to comment
Share on other sites

I study ajax as you suggested and come up with this code, but it still wont work.

Please help me figure out why it wont work???

 

in main page i put this:

<script>
function showData(godina, mjesec, dan)
{
showdiv();
/*if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } */
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("ispis").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST","analiza.php",true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("god =" + godina + "mj =" mjesec + "d =" +  dan);
}
</script>

and i call JS function by this:

$calendar.= '<td onclick= showData('.$year.','.$month.','.$list_day.'); class="calendar-day_used">'.$list_day.'</td>';

 in analiza.php I put:

<?php
if (array_key_exists('god', $_POST) && array_key_exists('mj', $_POST)) {
//xmlhttp.send("god =" + godina + "mj =" mjesec + "d =" +  dan);
    $godina = $_POST['god'];
    $godina = $_POST['mj'];
    $dan = = $_POST['d'];
    //$mjesec = ($_POST['bar']);
    // do stuff with params
		$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';
		$sql="SELECT * FROM `record` WHERE (`Datum` = '" . $godina . "-" . $mjesec . "-" . $dan. "')";
		$result=mysql_query($sql);
	
		// Mysql_num_row is counting table row
		$row = mysql_fetch_array($result, MYSQL_BOTH);
		$polje = explode("-", $row["Datum"]);
		$calendar = 'Ime i prezime: '.$row["ime"].'<br>'.
					'Datum dolaska je: ' . $row["Datum"].'<br>'.
					'Datum dolaska je: ' . $polje[0] . '-' . $polje[1] . '-' . $polje[2]+$row["3"].'<br>'.
					'Broj noćenja je: ' . $row["4"].'<br>'.
		$calendar = '<\table>';
    echo $calendar;

} else {
    echo 'Invalid parameters!';
}
?>
Link to comment
Share on other sites

Where do I even begin... I count at least a dozen basic syntax errors, and that's not even counting logic errors,straight up missing needed code for other code to function, no error checking, no input validation... and judging by what I see here, my money is on there being massive errors in the code you didn't post.

 

I strongly advise you to step back and invest some time in learning the basic syntax rules of javascript and php, and also take some time to learn the methods of debugging javascript and php code How to check for errors. How to find out what the errors mean, etc.. You can't just copy and paste other people's code and expect it to work. You're trying to run before you've learned to walk or even crawl. You have to take the time to actually learn the language so that you can understand what it is doing and what is wrong with it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.