0

I've been trying to get this to work for alittle over a week and im about to lose hope in my capabilities. I need some help, someone who can write the working code and explain what is happening.

Basically, i want to display a message that is called from posts.php and have is display right in between the checkbox of the form and the submit button in my loginform. I don't just want a solution that can only be used for submitting forms, i want to be able to display a message anywhere in a website at any given time without being dependent on buttons being clicked or forms being submitted.

As an example, I used a form that if both username and password would be left blank it would display an error. I wanted to use this just as an example so others could understand what I was trying to accomplish easily. Let me stress again that I don’t want a solution dependent on form submissions or the form calling a javascript function, I want to be able to display a message anywhere in a section of a website at anytime. Without further adieu, here are the files:

Edited by Jamesiscrazzy: n/a

Attachments
<?php
	
	
	//Function: Display main.php login
	function displayLogin()
	{
  
		//Break php, then return to PHP
		?> 
		<div style="float: left; padding: 10px; width:20%;">
		<fieldset>
		<legend>
		<h1>Login</h1>
		</legend>
		<form action="" method="post" >
		<table align="left" border="0" cellspacing="0" cellpadding="3">
		<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
		<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
		<tr><td colspan="2" align="left"><input type="checkbox" name="remember">
		<font size="2">Remember username and password</td></tr>	
		<!-- Here is where i want to display the message.-->
		<tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>	
		</table>	
		</form>
		</fieldset>
		</div>

		<?php

	}
	
		
	//Function: Display text on screen using colour
	function printText($txtmsg, $txtcol)
	{
		/// Here is where i need help, i want to display the error between the check box and the submit button
		
			?>
			<b><font color="<?echo $txtcol;?>"><?echo $txtmsg;?></font></b>		
			<?php	
			
		//What i did above just displays it on the top of the browser, not anywhere i want it to display
		/*Please let me stress this, i dont want to use anything that has to do with the form submitting or
		the submit button being clicked, i want to be able to display an error anywhere inside of a webpage at any given time.
		To make it easier for others to understand i just used having nothing in text boxes as an example i think everyone would comprehend.*/
		
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
Booking Website
<head>
<title>Log Into AV Booking System</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php 
	//Start session
	session_start(); 
	//Require functions.php
	require("functions.php");
	//Require posts.php
	require("posts.php");	
?>
</head>
<body>
<?php 
		//Call function to display login
		displayLogin(); 
	
?>
</body>
</html>
<?php



	//POST: Main login page
	if (isset($_POST['sublogin']))
	{
		//Display error if feilds are not filled in
		if(!$_POST['user'] || !$_POST['pass'])
		{
			/// ================
			//Here is where i call a function to display the message
			/// ================
			printText("Please make sure all required fields are filled in.", "red");
		}
		else
		{
			//Some database minipulation here
		}
	}
	

?>
1
Contributor
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Jamesiscrazzy
0

Just to mention i’ve seen a solution that involved calling a javascript function that would use innerhtml and an elementID and it seemed great but that particular solution to a question i saw was done in a way that was a massive head ache because the script had to be written after the form in order to actually edit the innerhtml of the <div> and the only way to call it in printMessage() would make the calling to that javascript at the very top of main.php before the function is even defined giving object errors and null errors.

Edited by Jamesiscrazzy: n/a

0

Special thanks to hkx from you know where that is keeping me sane. Here is a simpler version of what i want to accomplish. Pretty much i tried over doing it by putting the JavaScript in functions.php and ineffectually ended up calling the function before its creation(or something). Here is the solution. You must always have the javascript code under the <div> you will be changing.

For future people, here are some tags for others find this page:
'document.getElementById(...)' is null or not an objec
<body onload=
onSubmit="return"
Dynamic Form Error
Display Error
PHP
Javascript
Html

Attachments
<?php
	
	
	//Function: Display main.php login
	function displayLogin()
	{
  
		//Break php, then return to PHP
		?> 
		<div style="float: left; padding: 10px; width:20%;">
		<fieldset>
		<legend>
		<h1>Login</h1>
		</legend>
		<form name = "mainlogin" action="" method="post">
		<table align="left" border="0" cellspacing="0" cellpadding="3">
		<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
		<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
		<tr><td colspan="2" align="left"><input type="checkbox" name="remember">
		<font size="2">Remember username and password</td></tr>	
		<tr><td><div id="error"></div></tr><td>
		<tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>	
		</table>	
		</form>
		</fieldset>
		</div>

		<?php

	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
Booking Website
<head>
<title>Log Into AV Booking System</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php 
	//Start session
	session_start(); 
	//Require functions.php
	require("functions.php");


?>
</head>
<body>
<?php 
		//Call function to display login
		displayLogin(); 
		//Require posts.php
		require("posts.php");
?>
</body>
</html>
<?php
	//POST: Main login page
	if (isset($_POST['sublogin']))
	{
		//Display error if feilds are not filled in
		if(!$_POST['user'] || !$_POST['pass'])
		{
			echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure all required fields are filled in.";</script>';
			return false;
		}
		else
		{
			//Some database minipulation here....
			//...
			if ($_POST['user'] != $databaseUsername)
			{
			echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure your username is correct.";</script>';
			return false;
			}
			elseif($_POST['pass'] != $databasePassword)
			{
			echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure your password is correct.";</script>';
			return false;			
			}
		}
		return true;
	}

?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.