Jump to content


Properly Calling A Function


8 replies to this topic

#1 Steve Marcano

    Young Padawan

  • Members
  • Pip
  • 31 posts
  • Gender:Male
  • Location:Tucson, Arizona

Posted 28 October 2009 - 11:38 AM

The following is my code for my backstage.php file. What I'm trying to do is figure out how to call a function from my backstagefunctions.php file when a user clicks a link when they make it to the admin panel. An example follows the backstage.php file.

<?php 

require "backstageconfig.php";
require "backstagefunctions.php";

ob_start();
//if the login form is submitted
if(isset($_POST['submit']))
{
	// makes sure they filled it in
	if(!$_POST['username'] || !$_POST['password'])
	{
		die('You did not fill in a required field.');
	}
   $username = mysql_real_escape_string($_POST['username']); 
   $pass = mysql_real_escape_string($_POST['password']); 

	$check = mysql_query("SELECT * FROM users WHERE username = '".$username."'")or die(mysql_error());

	//Gives error if user dosen't exist
	$check2 = mysql_num_rows($check);
	if ($check2 == 0)
	{
		die('That user does not exist in our database.');
	}
	while($info = mysql_fetch_array( $check )) 
	{
		$pass = md5(stripslashes($_POST['password']));
		$info['password'] = stripslashes($info['password']);
		//$_POST['pass'] = md5($_POST['pass']); THIS IS DONE IN THE ABOVE STATEMENT
		//gives error if the password is wrong
		if ($pass != $info['password'])
		{
			die('Incorrect password, please try again.');
		}
		else 
	  
	  // if login is ok then we add a cookie and send them to the correct page
		{ 
			$username = stripslashes($username); 
		 $_SESSION['username'] = $username; 
		 $_SESSION['loggedin'] = time();
			
			// Finds out the user type
			$query = "SELECT `admin` FROM `users` WHERE `username` = '" . $username . "'";
			$result = mysql_query($query) or die(mysql_error()); 
			$row = mysql_fetch_array($result); 
			$admin = $row['admin'];
		 $_SESSION['admin'] = $admin;

#########################################
######## ADMIN SCRIPT CAN BE ADDED BELOW
#########################################
if(isset($_SESSION['admin'])) { ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Language" content="en-us">
<meta name="language" content="en-us">
<title>Backstage V1 Administration Console</title>
<link rel="stylesheet" href="backstage.css" type="text/css" media="screen">
</head>
<body>
<div id=container>
<div class=header>
<table cellpadding="0" cellspacing="0" border="0" width="95%">
<tr>
<td width=110 align=center></td>
<td></td>
<td width=40 valign=bottom align=right>
<a href="#" onclick="">Home</a> | <a href="#" onclick="">Logout</a> | <a target="_blank" href="http://kansasoutlawwrestling.com/phpBB3">Forums</a></td>
</tr>
</table>
</div>
<div id=container2>
<div id=nav>
<?php if(isset($_SESSION['loggedin']))   { ?>
<h1>Character</h1>
<ul>
<li><a href="#" onclick="">Biography</a></li>
<li><a href="#" onclick="">Allies</a></li>
<li><a href="#" onclick="">Rivals</a></li>
<li><a href="#" onclick="">Quotes</a></li>
</ul>
<?php } ?>
<?php if(isset($_SESSION['loggedin']))   { ?>
<h1>Submit</h1>
<ul>
<li><a href="#" onclick="">Roleplay</a></li>
<li><a href="#" onclick="">News</a></li>
<li><a href="#" onclick="">Match</a></li>
<li><a href="#" onclick="">Seg</a></li>
</ul>
<?php } ?>
<?php if(isset($_SESSION['loggedin']) && $_SESSION['admin'] == 1) { ?>  
<h1>Handler</h1>
<ul>
<li><a href="#" onclick="">Directory</a></li>
</ul>
<?php } ?>
<?php if(isset($_SESSION['loggedin']) && $_SESSION['admin'] == 1) { ?>  
<h1>Booking</h1>
<ul>
<li><a href="#" onclick="">Champions</a></li>
<li><a href="#" onclick="">Booker</a></li>
<li><a href="#" onclick="">Compiler</a></li>
<li><a href="#" onclick="">Archives</a></li>
</ul>
<?php } ?>
<?php if(isset($_SESSION['loggedin']) && $_SESSION['admin'] == 1) { ?>  
<h1>Fed Admin</h1>
<ul>
<li><a href="#" onclick="">Handlers</a></li>
<li><a href="#" onclick="">Characters</a></li>
<li><a href="#" onclick="">Applications</a></li>
<li><a href="#" onclick="">Event Names</a></li>
<li><a href="#" onclick="">Title Names</a></li>
<li><a href="#" onclick="">Match Types</a></li>
<li><a href="#" onclick="">Divisions</a></li>
<li><a href="#" onclick="">Arenas</a></li>
</ul>
<?php } ?>
<?php if(isset($_SESSION['loggedin']) && $_SESSION['admin'] == 1) { ?>  
<h1>Site Admin</h1>
<ul>
<li><a href="#" onclick="">Templates</a></li>
<li><a href="#" onclick="">Content</a></li>
<li><a href="#" onclick="">Bio Configuration</a></li>
<li><a href="#" onclick="">News Categories</a></li>
<li><a href="#" onclick="">Menus</a></li>
</ul>
<?php } ?>
</div>
<div id=content>
</div>
<div id="footer">Backstage 1 &copy; 2009
</div>
</div>
</div>
</body>
</html>
<?php  
#########################################
######## ADMIN SCRIPT HAS TO END ABOVE
#########################################
	}
		} 
	} 
} 
else 
{
// if they have not submitted the form
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Language" content="en-us">
<meta name="language" content="en-us">
<title>Backstage V1 Administration Console</title>
<link rel="stylesheet" href="backstage.css" type="text/css" media="screen">
</head>
<body>
<div id=login>
<form method="POST" action="/mybackstage/backstage.php">
<h1>KOW Backstage</h1>
<p><label>Username:<br><input type="text" name="username" id="log" tabindex="1"></label></p>
<p><label>Password:<br><input type="password" name="password" id="pwd" tabindex="2"></label></p>
<p style="text-align: center;"><input type="submit" class="button" name="submit" id="submit" value="Login &raquo;" tabindex="4"></p>
</form>
</div>
</body>
</html>
<?php
}
?>


When the user clicks on Arenas in the admin panel it grabs the following code:

function arenas() 
{
	 $country = $_GET['country'];
	$query = mysql_query("SELECT * FROM `arenas` WHERE `country` = '" . $country . "'");
	 $row = mysql_fetch_array($query); 
	print '<h1 class="backstage">Arena Management</h1><br />';
	print "<h2 class=\"backstage\">".$country." Arenas ::  <a href=\"#\" onclick=\"addarena('addarena', 'content', '". $country ."'); return false;\">Add New Arena</a></h2><br />";
	 $query = "SELECT * FROM arenas  WHERE `country` = '" . $country . "' ORDER BY `citystate`";
	$result = mysql_query ( $query ); // Run The Query
	$rows = mysql_num_rows($result);
	if ($rows > 0) 
	{
		print '<table width="100%" class="table1">';
		print '<tr class="rowheading">';
		print '<td>&nbsp;</td>';
		print '<td>City</td>';
		print '<td>Arena</td>';
		print '<td align="center">Capacity</td>';
		print '</tr>';
		// Fetch and print all records.
		$i = 0;
		while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) ) 
		{
			$sClass = 'row2';
			if ($i ++ & 1) 
			{
				$sClass = 'row1';
			}
			printf ( "<tr class=\"%s\">", $sClass );
			print "<td valign=\"top\" align=\"center\" width=\"30\"><a href=\"#\" onclick=\"editarena('editarena', 'content' , '". $row ['arena'] ."'); return false;\">Edit</a></td>";
			printf ( "<td valign=\"top\">%s</td>", $row ['citystate'] );
			printf ( "<td valign=\"top\">%s</td>", $row ['arena'] );
			printf ( "<td valign=\"top\" align=\"center\">%s</td>", $row ['capacity'] );
			print '</tr>';
		}
		echo '</table><br />';
	}
	else 
	{
	print '<span>There are no arenas for this country.</span><br /><br />';
	}
	print '<input type="button" value="Return to Country List" class="button200"><br /><br />';
	print '<h2 class="backstage"><input type="button" value="Return to Main Menu" class="button200"></form></h2>';
}


How is this done?

#2 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 28 October 2009 - 02:47 PM

<?php echo arenas(); ?>


#3 Steve Marcano

    Young Padawan

  • Members
  • Pip
  • 31 posts
  • Gender:Male
  • Location:Tucson, Arizona

Posted 28 October 2009 - 02:50 PM

Where at in my link here do I put that?

<li><a href="#" onclick="">Arenas</a></li>

#4 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 28 October 2009 - 03:00 PM

Looks like you don't have any if statements, or the switch() function for calling the function of choice. What i mean is this:

You want to go to page "a" from the main page... You have a function called "a" as well....

your link is this: http://domain.com?page=a with your if statement you will do this:

$page = $_GET['page'];
if ($page == 'a') {

echo a();

}

do you understand what I am saying? that would be a proper (pretty pathetic) way, but it gets the point across.

#5 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 October 2009 - 03:23 PM

View Postderek.sullivan, on Oct 28 2009, 02:47 PM, said:

<?php echo arenas(); ?>

Technically you don't need to use echo, as the arenas() function echos what it needs and does not return anything.

You would not have the user invoke the function like you have put, because the user does not invoke PHP functions through the browser; that would be JavaScript if anything, the server is what executes the PHP. Have the page call the arenas() function from wherever it is needed on whatever page the user navigates to. So basically do as derek.sullivan showed, but adapt it to where you need it to be used.

#6 Steve Marcano

    Young Padawan

  • Members
  • Pip
  • 31 posts
  • Gender:Male
  • Location:Tucson, Arizona

Posted 28 October 2009 - 04:35 PM

Now I"m confused.

#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 28 October 2009 - 08:29 PM

PHP is a server side language, meaning all the processing is done on the server before it is sent to the client (i.e. you/your browser). What your browser recieves is plain HTML which cannot interact directly with PHP (in can interact indirectly with some help form JavaScript, but that's about it).

So, in order to call the areans() function when you click the areans link, you have to either:
1. Figure out how to use AJAX (not recommended until you get some more experience).
2. Define something in the url which would then tell the PHP script you want to execute the areans() function when somebody goes to that url.

The following tutorials express the idea we're trying to get across:
http://www.pixel2life.com/tutorials/php_co..._and_templates/

#8 Steve Marcano

    Young Padawan

  • Members
  • Pip
  • 31 posts
  • Gender:Male
  • Location:Tucson, Arizona

Posted 29 October 2009 - 08:57 AM

I had some help doing some of this but what I'm trying to do is get my functions to retrieve its values from the backstagefunctions.php file and place the contents in the content div but when you click on the link it doesn't do anything and then when you try and manually plug the function name in it just logs you out. I'm trying to figure out why it won't work properly. I also want as the default function called mainscreen to be loaded into the content div once the user logs in. Anyone know?

<?php 
require "backstageconfig.php";
require "backstagefunctions.php";
ob_start();
//if the login form is submitted
if(isset($_POST['submit']))
{
if(!$_POST['username'] || !$_POST['password'])
{
die('You did not fill in a required field.');
}
$username = mysql_real_escape_string ($_POST['username']);
$pass = md5 (mysql_real_escape_string ($_POST['password']));
$check = mysql_query("SELECT * FROM users WHERE username = '".$username."' AND password = '".$pass."' LIMIT 1") or die (mysql_error());
$check2 = mysql_num_rows($check);
if ($check2 == 0)
{
die('That user does not exist in our database.');
}
while($info = mysql_fetch_array( $check )) 
{
$pass = md5(stripslashes($_POST['password']));
$info['password'] = stripslashes($info['password']);
if ($pass != $info['password'])
{
die('Incorrect password, please try again.');
}
else 
{ 
$username = stripslashes($username); 
$_SESSION['username'] = $username; 
$_SESSION['loggedin'] = time();
$query = "SELECT `admin` FROM `users` WHERE `username` = '" . $username . "'";
$result = mysql_query($query) or die(mysql_error()); 
$row = mysql_fetch_array($result); 
$admin = $row['admin'];
$_SESSION['admin'] = $admin;
if(isset($_SESSION['admin'])) 
if(isset($_GET['method']))
{
	 $func = $_GET['method'];
}
{ 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Language" content="en-us">
<meta name="language" content="en-us">
<title>Backstage V1 Administration Console</title>
<link rel="stylesheet" href="backstage.css" type="text/css" media="screen">
</head>
<body>
<div id=container>
<div class=header>
<table cellpadding="0" cellspacing="0" border="0" width="95%">
<tr>
<td width=110 align=center></td>
<td></td>
<td width=40 valign=bottom align=right>
<a href="#" onclick="">Home</a> | <a href="#" onclick="">Logout</a> | <a target="_blank" href="http://kansasoutlawwrestling.com/phpBB3">Forums</a></td>
</tr>
</table>
</div>
<div id=container2>
<div id=nav>
<?php if(isset($_SESSION['loggedin']))   { ?>
<h1>Character</h1>
<ul>
<li><a href="#" onclick="backstagefunctions.php?method=biography">Biography</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=allies">Allies</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=rivals">Rivals</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=quotes">Quotes</a></li>
</ul>
<?php } ?>
<?php if(isset($_SESSION['loggedin']))   { ?>
<h1>Submit</h1>
<ul>
<li><a href="#" onclick="backstagefunctions.php?method=roleplays">Roleplay</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=news">News</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=matches">Match</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=segments">Seg</a></li>
</ul>
<?php } ?>
<?php if(isset($_SESSION['loggedin']) && $_SESSION['admin'] == 1) { ?>  
<h1>Handler</h1>
<ul>
<li><a href="#" onclick="backstagefunctions.php?method=directory">Directory</a></li>
</ul>
<?php } ?>
<?php if(isset($_SESSION['loggedin']) && $_SESSION['admin'] == 1) { ?>  
<h1>Booking</h1>
<ul>
<li><a href="#" onclick="backstagefunctions.php?method=champions">Champions</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=booker">Booker</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=compiler">Compiler</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=archives">Archives</a></li>
</ul>
<?php } ?>
<?php if(isset($_SESSION['loggedin']) && $_SESSION['admin'] == 1) { ?>  
<h1>Fed Admin</h1>
<ul>
<li><a href="#" onclick="backstagefunctions.php?method=handlers">Handlers</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=characters">Characters</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=applications">Applications</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=events">Event Names</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=titles">Title Names</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=matchtypes">Match Types</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=divisions">Divisions</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=arenas">Arenas</a></li>

</ul>
<?php } ?>
<?php if(isset($_SESSION['loggedin']) && $_SESSION['admin'] == 1) { ?>  
<h1>Site Admin</h1>
<ul>
<li><a href="#" onclick="backstagefunctions.php?method=templates">Templates</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=content">Content</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=bioconfig">Bio Configuration</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=newscat">News Categories</a></li>
<li><a href="#" onclick="backstagefunctions.php?method=menus">Menus</a></li>
</ul>
<?php } ?>
</div>
<div id=content>
<?php
if(isset($_GET['method']))
echo $func();
?>	 
</div>
<div id="footer">Backstage 1 &copy; 2009
</div>
</div>
</div>
</body>
</html>
<?php  
}
} 
} 
} 
else 
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Language" content="en-us">
<meta name="language" content="en-us">
<title>Backstage V1 Administration Console</title>
<link rel="stylesheet" href="backstage.css" type="text/css" media="screen">
</head>
<body>
<div id=login>
<form method="POST" action="/mybackstage/backstage.php">
<h1>KOW Backstage</h1>
<p><label>Username:<br><input type="text" name="username" id="log" tabindex="1"></label></p>
<p><label>Password:<br><input type="password" name="password" id="pwd" tabindex="2"></label></p>
<p style="text-align: center;"><input type="submit" class="button" name="submit" id="submit" value="Login &raquo;" tabindex="4"></p>
</form>
</div>
</body>
</html>
<?php
}
?>


#9 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 29 October 2009 - 06:38 PM

You have no session_start(); add that to the very top of the script you've provided.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users