6 Replies - 883 Views - Last Post: 28 December 2012 - 06:28 AM Rate Topic: -----

#1 worldofwar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 26-June 11

Display a message in html?

Posted 27 December 2012 - 03:10 PM

Hello DIC,

I've been searching for quite few hours so I guess I'm safe to post a thread on my favorite programming website,
I've been working on a user system, a lot of it is done through tutorials because I'm doing this small project for learning purposes.

My problem is that when I want to log in, and certain fields are empty, it kills the site and displays a message.
What I'd like to do is to place that message inbetween the login form and the login button.

<?php 


 mysql_connect("no", "info", "foryou") or die(mysql_error()); 
 mysql_select_db("denied") or die(mysql_error()); 


	 if(isset($_COOKIE['ID_my_site'])) { 
		$username = $_COOKIE['ID_my_site']; 
		$pass = $_COOKIE['Key_my_site'];
		$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());

		while($info = mysql_fetch_array( $check )) {
			if ($pass != $info['password']) {

			} else {
				header("Location: members.php");
			}
		}

	 }



	 if (isset($_POST['submit'])) {
		if(!$_POST['username'] | !$_POST['pass']) {
			die('<center>You didn\'t fill in all required information</center>');
		}

		$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());
		$check2 = mysql_num_rows($check); //Bestaat naam?

		if ($check2 == 0) {
			die('<center>That user does not exist in our database. <a href=register.php>Click Here to Register</a></center>');
		}
		
		while($info = mysql_fetch_array( $check )) {
			$_POST['pass'] = stripslashes($_POST['pass']);
			$info['password'] = stripslashes($info['password']);
			$_POST['pass'] = md5($_POST['pass']);

			if ($_POST['pass'] != $info['password']) {
				die('<center>Incorrect password, please try again.</center>');
			} else { 
				// cookie als login juist is
				$_POST['username'] = stripslashes($_POST['username']); 
				$hour = time() + 3600; 
				setcookie(ID_my_site, $_POST['username'], $hour); 
				setcookie(Key_my_site, $_POST['pass'], $hour);	 
				//naar member area
				header("Location: members.php"); 
			} 
		} 
	} else {	 
	// als ze niet ingelogd zijn
	
?> 
<div class="bootstrap">
	 <center>
	 <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
	 <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
	 <table border="0"> 
	 <tr><td colspan="4" align="center"><h1>Login</h1></td></tr> 
	 <tr><td>Username:</td><td> 
	 <input type="text" name="username" maxlength="40"> 
	 </td></tr> 
	 <tr><td>Password:</td><td> 
	 <input type="password" name="pass" maxlength="50"> 
	 </td></tr> 
	 <tr><td colspan="2" align="center"> 
	 <button type="submit" name="submit" class="btn btn-primary">Log in</button>
	 </td></tr> 
	 </table> 
	 </form> 
	 </center>
	 <script src="js/bootstrap.min.js"></script>
	 
	<style type="text/css">
      body {
        padding-top: 40px;
        padding-bottom: 40px;
        background-color: #f5f5f5;
      }
    </style>

<?php 

 } 

 

 ?> 



This is my whole login.php file,
the comments are in Dutch, but I don't think it's necessary to translate them.

This page is displayed upon loading the directory:

Posted Image

The whole page turns into this when not enough information has been filled in:

Posted Image

I'd like to have the error message displayed where the red line is:

Posted Image

Thank you,
Worldofwar.

Is This A Good Question/Topic? 0
  • +

Replies To: Display a message in html?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Display a message in html?

Posted 27 December 2012 - 03:29 PM

note: line #26, | is bitwise OR, not logic OR (that would be || or or).

basically, you have to place the element for the error message into the form to begin with, just don’t put anything in unless there is an error (keyword: "template").

of course you can also employ Java​Script to test whether all fields are filled and stop submitting the form if not.

This post has been edited by Dormilich: 27 December 2012 - 03:31 PM

Was This Post Helpful? 1
  • +
  • -

#3 worldofwar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 26-June 11

Re: Display a message in html?

Posted 27 December 2012 - 03:52 PM

View PostDormilich, on 27 December 2012 - 03:29 PM, said:

note: line #26, | is bitwise OR, not logic OR (that would be || or or).

basically, you have to place the element for the error message into the form to begin with, just donít put anything in unless there is an error (keyword: "template").

of course you can also employ Java​Script to test whether all fields are filled and stop submitting the form if not.


so what I did now was add

$errormessage '';

then instead of the die(blabla);
I did: $errormessage = 'Error message here';

then in the html part:

	 <tr><td>Password:</td><td> 
	 <input type="password" name="pass" maxlength="50"> 
	 </td></tr> 
	 <?php
	 echo $errormessage;
	 ?>
	 <tr><td colspan="2" align="center"> 
	 <button type="submit" name="submit" class="btn btn-primary">Log in</button>
	 </td></tr> 



what happens now is, whenever one of the errors occurs, it just shows a blanc page without anything on it at all.
when I inspect the sourcecode of that page, it is empty too.

Am I missing something/Doing something wrong?

Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: Display a message in html?

Posted 27 December 2012 - 04:03 PM

Your current code is like this:

if (isset($_POST['submit'])) {
    // echo error message(s) or
    // re-direct to another page
} else {
    // construct the form


You need to re-write the page so that the form is built IF there is an invalid submission OR the form has not yet been submitted.

BTW The center :sadlike: tag no longer exists.

This post has been edited by andrewsw: 27 December 2012 - 04:05 PM

Was This Post Helpful? 2
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Display a message in html?

Posted 27 December 2012 - 04:08 PM

note on behalf of the HTML, there must not be text between <tr> elements. the <table> element only allows for <tbody>, <thead>, <tfoot>, <tr>, <caption> and <col>/<colgroup> elements. text is only allowed within <td>, <th> and <caption>.
Was This Post Helpful? 1
  • +
  • -

#6 worldofwar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 26-June 11

Re: Display a message in html?

Posted 27 December 2012 - 04:45 PM

View Postandrewsw, on 27 December 2012 - 04:03 PM, said:

Your current code is like this:

if (isset($_POST['submit'])) {
    // echo error message(s) or
    // re-direct to another page
} else {
    // construct the form


You need to re-write the page so that the form is built IF there is an invalid submission OR the form has not yet been submitted.

BTW The center :sadlike:/> tag no longer exists.


Thanks!
worked very well :)
Was This Post Helpful? 0
  • +
  • -

#7 Nullified  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 49
  • Joined: 18-September 12

Re: Display a message in html?

Posted 28 December 2012 - 06:28 AM

As previously suggested, I would use javascript to conduct the initial form field validation. As well, be sure to conduct a more thorough validation within your php code before any sql injection. Be sure to mysql escape your form fields to ensure your db/site/server are safe from sql injection.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1