Jump to content

Editing User Info While Going through multiple functions


Xtremer360

Recommended Posts

Here's where it shows all the handlers names with the edit links. How do I connect the two functions so that i'll get the users info when it goes to the edithandler function.

 

function handlers() {
print '<h1 class=backstage>Handler Management</h1><br />';
print "<h2 class=backstage>Handlers :: <a href=\"#\" onclick=\"ajaxpage('addhandler', 'content'); return false;\">Add New</a></h2><br />";
print '<table width="100%" class="table1">';
print '<tr class="rowheading">';
print '<td width=30> </td>';
print '<td>Username</td>';
print '<td>Surname</td>';
print '<td>First Name</td>';
print '<td>E-Mail</td>';
print '</tr>';
$query = "SELECT * FROM users";
$result = mysql_query ( $query ); // Run The Query
if ($result) {
	// 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=\"ajaxpage('edithandler', 'content'); return false;\">Edit</a></td>";
		printf ( "<td valign=\"top\">%s</td>", $row [username] );
		printf ( "<td valign=\"top\">%s</td>", $row [surname] );
		printf ( "<td valign=\"top\">%s</td>", $row [firstname] );
		printf ( "<td valign=\"top\">%s</td>", $row [email] );
		print '</tr>';
	}
}
print '</table><br />';
print '<h2 class=backstage><form method=POST><input type=hidden name=action value=mainmenu><input type=submit value="Return to Main Menu" class=button200></form></h2>';
}

 

Here's the edit handler function.

 

function edithandler() {
print '<h1 class=backstage>Handler Management</h1><br />';
print '<h2 class=backstage>Edit Handler Details</h2><br />';
print '<form name="edithandler" method="post" action="backstage.php" id="edithandler">';
print '<table width="100%" class="table2">';
print '<tr>';
print '<td width=120 class=rowheading>Username:</td><td class=row3><input type=text name=login class=fieldtext490 value="'.$row['username'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Password:</td><td class=row3><input type=password name=password class=fieldtext490 onfocus="this.select()" value=""></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Surname:</td><td class=row3>';
print '<input type=text name=surname class=fieldtext490 value="'.$row['surname'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Firstname:</td>';
print '<td class=row3><input type=text name=firstname class=fieldtext490 value="'.$row['firstname'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Email:</td>';
print '<td class=row3><input type=text name=email class=fieldtext490 value="'.$row['email'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>AIM:</td>';
print '<td class=row3><input type=text name=aim class=fieldtext490 value="'.$row['aim'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>MSN:</td>';
print '<td class=row3><input type=text name=msn class=fieldtext490 value="'.$row['msn'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Forum ID:</td>';
print '<td class=row3><input type=text name=forumid class=fieldtext490 value="'.$row['forumid'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Account:</td>';
print '<td class=row3><select name=enabled class=selection>';
print '<option value=1>Enabled</option><option value=0>Disabled</option>';
print '</select></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Administrator:</td>';
print '<td class=row3><select name=isadministrator class=selection>';
print '<option value=1>Yes</option><option value=0>No</option>';
print '</select></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Default Character:</td>';
print '<td class=row3></td>';
print '</tr>';
print '</table><br />';
print '<input type=checkbox name=deletehandler> <span class=table1heading>Delete Handler?</span><br /><br />';
print '<input type="submit" value="Save Handler" class="button" name="edithandler"></form><br />';
print '<form method=POST><input type=hidden name=action value=handler><input type=submit value="Return to Handler List" class=button200></form><br />';
print '<h2 class=backstage>Characters<br /><br /><form method=post><input type=hidden name=action value=handler><input type=hidden name=routine value=addcharacter><input type=hidden name=option value=0><input type=hidden name=id value="130"><select name=characterid class=dropdown>';
print '<option value=0>- Select -</select>  <input type=submit value="Add" class=button></form></h2><br />';
print '<br /><br />';
print '<h2 class=backstage><form method=POST><input type=hidden name=action value=mainmenu><input type=submit value="Return to Main Menu" class=button200></form></h2>';
}

 

 

Here's my Form Submission Handling Code

 

  //Form was submitted - determine the form
    if ( isset ( $_POST['edithandler'] ) ) {
        // Define the query.
        $password = md5($p); // Currently $p does not have a value
        $login = $_POST['login'];
        $p = $_POST['password'];
        $surname = $_POST['surname'];
        $firstname = $_POST['firstname'];
        $email = $_POST['email'];
        $aim = $_POST['aim'];
        $msn = $_POST['msn'];
        $forumid = $_POST['forumid'];
        $account = $_POST['account'];
        $admin = $_POST['admin'];
        
        $query = "UPDATE INTO `users` (`username`, `password`, `surname`, `firstname`, `email`, `aim`, `msn`, `forumid`, `status`, `admin`) VALUES ('".addslashes($login)."', '".addslashes($p)."', '".addslashes($surname)."','".addslashes($firstname)."', '".addslashes($email)."', '".addslashes($aim)."', '".addslashes($msn)."', '".addslashes($forumid)."', '".addslashes($account)."', '".addslashes($admin)."')";
        
        // Execute the query.
        if (@mysql_query ( $query )) {
            print '<p>The handler has been edited.</p>';
        } else {
            print '<p>Could not add the entry because: <b>"' . mysql_error() . '"</b>. The query was '.$query.'.</p>';
        }
        
        //mysql_close ();
    
    }

You want the function edithandler to have extra information it can insert into the html?

 

Use arguments:

 

function edithandler ($user_details="nodetails"){

  // you can access anything passed to this function via $user_details

  // If it was not called with this argument, it will default to nodetails (you can use a if statement to check for this)

}

 

to call this function now you would use edithandler($some_user_information);

 

i hope this helps.

oh you just want to grab user info and paste it into a form?

 

[1]. You need a Mysql query:

 

<?php

$query = "SELECT * FROM `users` WHERE `username`='".$userid."'";

?>

 

[2]. Then you need to execute the query and get the results. (and check u got any results)

 

<?php

$result = mysql_query($query);
if(mysql_num_rows($result) <= 0){
   echo("Error, Could not gather user information from database;");
}else{
   $User = mysql_result($result,0); // Grab row 0 from $result. (first match, should only b 1 anyway)
   // display the form here
   
   echo("<input type='text' name='username' value='".$User['username']."' />");
}
?>

 

Info:

 

So basically you grab the user information via the mysql_query with the query $query - lol.

Then your checking that mysql returned at least 1 row of information,

- If it does then display the form with the user information inserted into the "value" field of each <input> object as necessary.

- If it doesn't then display an error (since i guess you have to be logged in to see this form).

You will have to substitute:

 

username (in the $User['username'] and i the $query string) with your mysql field name.

$userid (used in the query) with the variable that holds the username of the client viewing the page.

 

Hope this helps.

One problem however my problem is when I have it lists the handlers names with the link to the edit handler function. Its already inside one function with the list of handlers then it goes to another function but how do i tell it with that link with the edit to pass along the corresponding username id?

function handlers() {
print '<h1 class=backstage>Handler Management</h1><br />';
print "<h2 class=backstage>Handlers :: <a href=\"#\" onclick=\"ajaxpage('addhandler', 'content'); return false;\">Add New</a></h2><br />";
print '<table width="100%" class="table1">';
print '<tr class="rowheading">';
print '<td width=30> </td>';
print '<td>Username</td>';
print '<td>Surname</td>';
print '<td>First Name</td>';
print '<td>E-Mail</td>';
print '</tr>';
$query = "SELECT * FROM users";
$result = mysql_query ( $query ); // Run The Query
if ($result) {
	// 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=\"ajaxpage('edithandler', 'content'); return false;\">Edit</a></td>";
		printf ( "<td valign=\"top\">%s</td>", $row [username] );
		printf ( "<td valign=\"top\">%s</td>", $row [surname] );
		printf ( "<td valign=\"top\">%s</td>", $row [firstname] );
		printf ( "<td valign=\"top\">%s</td>", $row [email] );
		print '</tr>';
	}
}
print '</table><br />';
print '<h2 class=backstage><form method=POST><input type=hidden name=action value=mainmenu><input type=submit value="Return to Main Menu" class=button200></form></h2>';
}

 

function edithandler() {
$query = "SELECT * FROM `users` WHERE `username`='".$username."'";
$result = mysql_query($query);
if(mysql_num_rows($result) <= 0){
   echo("Error, Could not gather user information from database;");
}else{
   $User = mysql_result($result,0); // Grab row 0 from $result. (first match, should only b 1 anyway)
print '<h1 class=backstage>Handler Management</h1><br />';
print '<h2 class=backstage>Edit Handler Details</h2><br />';
print '<form name="edithandler" method="post" action="backstage.php" id="edithandler">';
print '<table width="100%" class="table2">';
print '<tr>';
print '<td width=120 class=rowheading>Username:</td><td class=row3><input type=text name=login class=fieldtext490 value="'.$row['username'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Password:</td><td class=row3><input type=password name=password class=fieldtext490 onfocus="this.select()" value=""></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Surname:</td><td class=row3>';
print '<input type=text name=surname class=fieldtext490 value="'.$row['surname'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Firstname:</td>';
print '<td class=row3><input type=text name=firstname class=fieldtext490 value="'.$row['firstname'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Email:</td>';
print '<td class=row3><input type=text name=email class=fieldtext490 value="'.$row['email'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>AIM:</td>';
print '<td class=row3><input type=text name=aim class=fieldtext490 value="'.$row['aim'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>MSN:</td>';
print '<td class=row3><input type=text name=msn class=fieldtext490 value="'.$row['msn'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Forum ID:</td>';
print '<td class=row3><input type=text name=forumid class=fieldtext490 value="'.$row['forumid'].'"></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Account:</td>';
print '<td class=row3><select name=enabled class=selection>';
print '<option value=1>Enabled</option><option value=0>Disabled</option>';
print '</select></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Administrator:</td>';
print '<td class=row3><select name=isadministrator class=selection>';
print '<option value=1>Yes</option><option value=0>No</option>';
print '</select></td>';
print '</tr>';
print '<tr>';
print '<td class=rowheading>Default Character:</td>';
print '<td class=row3></td>';
print '</tr>';
print '</table><br />';
print '<input type=checkbox name=deletehandler> <span class=table1heading>Delete Handler?</span><br /><br />';
print '<input type="submit" value="Save Handler" class="button" name="edithandler"></form><br />';
print '<form method=POST><input type=hidden name=action value=handler><input type=submit value="Return to Handler List" class=button200></form><br />';
print '<h2 class=backstage>Characters<br /><br /><form method=post><input type=hidden name=action value=handler><input type=hidden name=routine value=addcharacter><input type=hidden name=option value=0><input type=hidden name=id value="130"><select name=characterid class=dropdown>';
print '<option value=0>- Select -</select>  <input type=submit value="Add" class=button></form></h2><br />';
print '<br /><br />';
print '<h2 class=backstage><form method=POST><input type=hidden name=action value=mainmenu><input type=submit value="Return to Main Menu" class=button200></form></h2>';
}
}

ou want to pass the variable through ajax?

 

unfortunately im not to up on ajax but firs ou would probabl do something like:

 

onclick=\"ajaxpage('edithandler', 'content', '".$row['username']."'); return false;\"

 

Then you would need to edit the ajax "ajaxpage()" function to accept the new argument (username) and pass it to the php page maybe via a GET query.

 

(As i said i dont know ajax i could be completely wrong lol)

Okay I'm going to try and use that thanks uniflare however here's my ajax.js file.

 

var loadedobjects=""
var rootdomain="http://"+window.location.hostname

function ajaxpage(url, containerid)
{
url = 'backstagefunctions.php?f=' + url;

var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject)
{
	// if IE
	try
	{
		page_request = new ActiveXObject("Msxml2.XMLHTTP")
	} 
	catch (e)
	{
		try
		{
			page_request = new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch (e)
		{
		}
	}
}
else
{
	return false
}

page_request.onreadystatechange=function()
{
	loadpage(page_request, containerid)
}

page_request.open('GET', url, true)
page_request.send(null)
}

function loadpage(page_request, containerid)
{
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs()
{
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++)
{
	var file=arguments[i]
	var fileref=""
	if (loadedobjects.indexOf(file)==-1)
	{ 
		//Check to see if this object has not already been added to page before proceeding
		if (file.indexOf(".js")!=-1)
		{
			//If object is a js file
			fileref=document.createElement('script')
			fileref.setAttribute("type","text/javascript");
			fileref.setAttribute("src", file);
		}
		else if (file.indexOf(".css")!=-1)
		{
			//If object is a css file
			fileref=document.createElement("link")
			fileref.setAttribute("rel", "stylesheet");
			fileref.setAttribute("type", "text/css");
			fileref.setAttribute("href", file);
		}
	}
	if (fileref!="")
	{
		document.getElementsByTagName("head").item(0).appendChild(fileref)
		loadedobjects+=file+" " //Remember this object as being already added to page
	}
}
}
function WrestlerList()
{
var addWrestler = document.getElementById("character_selection").value
if (addWrestler) {
// Here we append a new wrestler to your hidden field using a comma
document.getElementById("chars").value += addWrestler+",";
// Continue your original functionality
document.getElementById("characterlist").innerHTML += "<li>" +
addWrestler + "</li>";
return WrestlerList;
}
}

Ok so, your ajax page function grabs a page... but you need to tell that page which username u want?

 

Use my code above and; you need to add an argument to the ajaxpage() function, like so:

function ajaxpage(url, containerid, usernameid)
{
   url = 'backstagefunctions.php?f=' + url;

--

 

Then you need to add that variable somewhere the page can read it from - you should use the GET global variable, ie: Pass the username through the URL. Like so;

function ajaxpage(url, containerid, usernameid)
{
   url = 'backstagefunctions.php?userid=' + usernameid + '&f=' + url;

---

 

Now on that page that ajax calls, you can access that variable in the url with the $_GET array. Like $_GET['userid'];

-

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.