Jump to content


php & javascript function


7 replies to this topic

#1 Nightscream

    Young Padawan

  • Members
  • Pip
  • 100 posts
  • Gender:Male
  • Location:Belgium

Posted 21 January 2007 - 07:25 AM

Ok my question is I have a php function that's getting info from the mysql database
but at the end it would start a javascript function, is it possible to do it like
echo '<div style="none" onLoad="java script:function name"></div>';

#2 Nightscream

    Young Padawan

  • Members
  • Pip
  • 100 posts
  • Gender:Male
  • Location:Belgium

Posted 21 January 2007 - 02:21 PM

mmh but it has to do a php function first and at the end of the php function it has to execute the javascript function

#3 Nightscream

    Young Padawan

  • Members
  • Pip
  • 100 posts
  • Gender:Male
  • Location:Belgium

Posted 21 January 2007 - 03:24 PM

yeah but it only have to do the javascript function when the php function blah is over
so if it's not over it should do the javascript function

#4 dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 21 January 2007 - 03:53 PM

So would the php be getting a variable for the Javascript to use?

#5 Nightscream

    Young Padawan

  • Members
  • Pip
  • 100 posts
  • Gender:Male
  • Location:Belgium

Posted 21 January 2007 - 07:48 PM

so My php file gets some data from the mysql database, file urls and then it puts the variable into
<div style="none">$variable</div>
then javascript function reads it and update a drop down box with the files, cause i have 2 dropdown boxes and if something changes in the first one the second one has to be updated with the right values.
I don't know ajax so i have to find another way

#6 Nightscream

    Young Padawan

  • Members
  • Pip
  • 100 posts
  • Gender:Male
  • Location:Belgium

Posted 22 January 2007 - 03:27 PM

ok this is my code
and it makes the comic drop down menu with the rows that is found in the database,
2nd it should execute Get_Pages() and at the end of that function it should start executing
the javascript function GetPages(), the comic drop down works but that's all, no drop down menu update or anything
i think the first error is the onclick event on echo '<option onclick="Get_Pages()">Chapter '.$i.'</option>';
cause it doesn't execute the get_pages() function

I hope you get it now

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<?php
	$hostname = "";
	$username = "";
	$password = "";
	$database = "";
	
	$connect = mysql_connect($hostname, $username, $password);
	mysql_select_db($database);
	
	/* --------------------------------------------------------- */
	
	$ComicName = null;
	
	function GetComicName() {
		$ComicName = $_GET['Chapter'];
		echo $ComicName;
		return $ComicName;
	}
	
	function Create_Chapter() {
		$result = mysql_query("SELECT * FROM Comics");
		$num_rows = mysql_num_rows($result);
		for($i=1; $i<$num_rows+1; $i++) {
			echo '<option onclick="Get_Pages()">Chapter '.$i.'</option>';
		}
	}
	function Get_Pages() {
		$result = mysql_query("SELECT ´Pages´ FROM Comics Where ´Name´='$ComicName'");
		echo $result;
		$num_rows = mysql_num_rows($result);
		$files = explode("", $num_rows);
		print_r('<div style="display: none;" id="files">'.$files.'</div>');
	}
		
?>
<script type="text/javascript">
	function GetPages() {   
		var filesDiv = getElementById("files")
		var files = filesDiv.value
		document.print(files)
		var num_files = files.lenght
		for(var i=0; i<=num_files; i++) {
			document.print(files[i])
		}
	}	
</script>  
</head>

<body>
<select name="Chapter">
	<option selected>&nbsp;</option>
	<?php Create_Chapter(); ?>
</select>
<select name="Pages" id="Pages">
<option selected>&nbsp;</option>
</select>

Bump :)

#7 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 28 January 2007 - 02:21 PM

You are obviously not understanding this apparently...
JavaScript cannot access PHP functions because PHP is executed before the script even reaches to the client-side browser.
Here is what you would do.
Since I honestly don't feel like trying to write the whole thing up for you, I'll run you a checklist of what you need to do, as far as I can understand from what you have.

- Grab the comics available [PHP]
- Loop each comic and echo as an option element [PHP]
- Have onchange handler set on the select menu to activate a function [JavaScript]
- This function sends an Ajax request to a PHP script, sending the value of the select menu [JavaScript]
- The PHP queries the database for all pages of the comic is gets from the Ajax call [PHP]
- Loops and echos formatted string with all pages available for that comic (seperated by '|' probably) [PHP]
- JavaScript grabs the string and splits it by that deliminator ('|') [JavaScript]
- Loops and writes each page in whatever format [JavaScript]

Simple searches for 'Ajax' in the JavaScript section will give you plenty of results on how to do this.

#8 Nightscream

    Young Padawan

  • Members
  • Pip
  • 100 posts
  • Gender:Male
  • Location:Belgium

Posted 28 January 2007 - 04:27 PM

ok ty i'll try this!!!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users