Jump to content

[SOLVED] show error message on same page


Daryl B

Recommended Posts

Hi Guys,

 

I have Index.php which contains the HTML form with a username and password field + submit button, action: is my check.php.

 

Say for example the user only input's a username and miss out the password field; when he clicks the submit button. I want the same page to be refreshed with a error message appearing below the submit button saying "error, please fill all in". I can get this to display on another page. but want this to be on refereshed on the log-in page.

 

If anyone can help that would be great.

 

Thanks ???

Link to comment
Share on other sites

yeah, what i do is this:

if (isset($_POST['submit'])) {  // Check if the form has been submitted.

require_once ('connect.php');  // Connect to the database

if (empty($_POST['username'])) {  // Validate the username.
	$u = FALSE;
	echo '<p>You forgot to enter your username!</p>';
} else {
	$u = ($_POST['username']);
}

if (empty($_POST['password'])) { // Validate the password.
	$p = FALSE;
	echo '<p>You forgot to enter your password!</p>';
} else {
	$p = ($_POST['password']);
}

if ($u && $p) { // Everything is ok.

 

This checks that the fields have been entered before it checks if the username and password are correct.

 

Then after that i query the database, and then i end the php and have the html form.

 

Hope that helps, if not, i can give you my whole index script which will achieve what you want.

Link to comment
Share on other sites

Hey,

 

cant seem to get it working:

 

this is my code:

 

<?php
session_start();
include('db.php');

if(isset($_POST['submit'])) : // Check if the form has been submitted.
// Username and password sent from signup form
// First we remove all HTML-tags and PHP-tags, then we create a sha1-hash

$username = strip_tags($_POST['username']);
$password = sha1(strip_tags($_POST['password']));

// Make the query a wee-bit safer
$query = sprintf("SELECT ID FROM members WHERE username = '%s' AND password = '%s' LIMIT 1;", mysql_real_escape_string($username), mysql_real_escape_string($password));

$result = mysql_query($query);
if(1 != mysql_num_rows($result)) :
	// MySQL returned zero rows (or there's something wrong with the query)
	header('Location: index.php?msg=login_failed');
else :
	// We found the row that we were looking for
	$row = mysql_fetch_assoc($result);

	// Register the user ID for further use
	$_SESSION['member_ID'] = $row['ID'];
	header('Location: members-only.php');
endif;
endif;

if( ($username == null) or ($password == null) )
{header("location:index.php"); exit();}
?>

 

Edited by SA: Please use the [code][/code] tags!

Link to comment
Share on other sites

What do you mean by "cant seem to get it working"? What's it (not) doing?

 

Side note: I didn't know that was valid syntax for if constructs in PHP, couldn't find anything in the manual - Where did you learn to use them that way?!

Link to comment
Share on other sites

Regarding the if constructs in PHP: I have just learnt myself using Mike McGeth's PHP5 book and the net. It must be a valid syntax as it does work.

 

Which part of the code did pick up on the syntax issue?

 

I have sorted the orginal issue, thanks. You post helped.

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.