Jump to content

Function not being called


Vel

Recommended Posts

I have 2 forms on a page, 1 with a selection of newly registered accounts and another that displays the information on the user. What I need to happen is when a user is selected from the first form, the second form is populated with the data. I'm trying to do this with a combination of php and javascript, however whenever a user is selected the function isn't being called at all as far as I can tell. Can anyone help me get this working please?

 

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Fearless Bandits :: Admin Control Panel</title>
</head>

<body>
<?php
//Check for config file and initiate session.
require("config.php");
session_start();

//Connect to DB
if(!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("$dbname") or die("Cannot select DB");

//Check user authorisation
if(!isset($_COOKIE['user'])) {
echo "Error: Cookies must be enabled. Please enable cookies in your browser and try again.<br>";
return;
}
else {
$user=$_SESSION['user'];
$sql="SELECT * FROM users WHERE username='$user' and access='2'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count == 0) {
	echo "Error: Unauthorised access detected.";
	return;
}
}

include("header.php");
echo "Admin Control Panel<br>";
//Check for mode, if no mode set to 0
if(isset($_GET['mode']))
$mode=$_GET['mode'];
else
$mode=0;

//Display information based on mode
switch($mode){
//Check for new applications
case 1:
	echo "<a href=\"admin.php?mode=0\">Back</a><br>";
	$sql="SELECT * FROM users WHERE newreg='1'";
	$result=mysql_query($sql);
	$count=mysql_num_rows($result);
	$newuser = NULL;
	$newuid = NULL;
	$newapi = NULL;
	$newmember = 0;
	//If there is more than one load results into an array
	if($count > 0) {
		echo "There is at least 1 new user:<br>";
		$i = 0;
		while($row=mysql_fetch_array($result)) {
			$newaccounts[$i] = $row['username'];
			$i++;
		}
		for($i=0; $i < $count; $i++) {
			echo "User " . $i . ": " . $newaccounts[$i] . "<br>";
		}
		//Select user
		echo "<form name=\"selectuser\" action=\"\">";
		echo "<select name=\"username\" onChange=\"newaccntsUpdate(this);\">";
		echo "<option value=\"\" selected=\"selected\"></option>";
			for($i=0; $i < $count; $i++) {
				echo "<option value=\"" . $newaccounts[$i] . "\">" . $newaccounts[$i] . "</option>";
			}
		echo "</select><br>";
		echo "</form>";

		//Output array into form.
		echo "<form name=\"newaccnts\" method =\"post\" action=\"verify_admin.php?form=1\">";
		echo "Username: <input name=\"newuser\" type=\"text\" id=\"newuser\" value=\"" . $_SESSION['newuser'] . "\">";
		echo "UID: <input name=\"newuid\" type=\"text\" id=\"newuid\" value=\"" . $_SESSION['newuid'] . "\">";
		echo "API: <input name=\"newapi\" type=\"text\" id=\"newapi\" value=\"" . $_SESSION['newapi'] . "\">";
		if($newmember == 0)
			echo "Member: <input name=\"newmember\" type=\"checkbox\" id=\"newmember\" value=\"1\"><br>";
		else
			echo "Member: <input name=\"newmember\" type=\"checkbox\" checked= \"checked\" id=\"newmember\" value=\"1\"><br>";
		echo "<input name=\"submit\" type=\"submit\" value=\"Submit\">";
		echo "</form>";
	}
	break;
//Select a mode
default:
	echo "Please select a function below:<br>";
	echo "<ul>";
		echo "<li><a href=\"admin.php?mode=1\">Check for new applications</a></li>";
		echo "<li><a href=\"admin.php?mode=2\">Modify an existing account</a></li>";
		echo "<li><a href=\"admin.php?mode=3\">Reset a password</a></li>";
		echo "<li><a href=\"admin.php?mode=4\">Delete an account</a></li>";
	echo "<ul>";
	break;
}
mysql_close($con);

//Update newaccnts function. Pull user info from databse and then run javascript
function newaccntsUpdate() {
echo "newaccntsUpdate executed<br>"; //Error checking
//Identify username
$newuser=identifyUser();
//If username is NULL or blank return
if($newuser == NULL || $newuser == "")
	return;
$sql = ("SELECT * FROM users WHERE username='$newuser'");
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$_SESSION['newuser'] = $newuser;
$_SESSION['newuid'] = $row['uid'];
$_SESSION['newapi'] = $row['api'];
$_SESSION['newmember'] = $row['member'];
//Update form with details
updateForm();
return;
}
?>

<script language = "Javascript">
//Identify which user has been selected
function identifyUser() {
$newuser = document.selectuser.username.value;
alert('identifyUser executed'); //Error checking
return($newuser);
}

//Update form
function updateForm() {
document.newaccnts.newuser.value = $newuser;
document.newaccnts.newuid.value = $_SESSION['newuid'];
document.newaccnts.newapi.value = $_SESSION['newapi'];
alert('updateForm executed'); //Error checking
return;
}
</script>
</body>
</html>

 

I apologise if I've butchered the code, I'm still learning.

Link to comment
Share on other sites

That won't work. PHP is executed server-side and JavaScript is executed client-side. Once the page is delivered to the user (i.e. is displayed in their browser) you can't dynamically execute more code in that same PHP file. You have to do a new request to the server.

 

So, you can "submit" the page when the user changes the selection so you can recreate the form with the appropriate data populated in the 2nd form OR you can use AJAX to dynamically do a server call to get the data and populate the data with JavaScript. Take a look at some tutorials to get started.

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.