1.

Solve : PHP login field check.?

Answer»

I have this PIECE of code here and what it's supposed to do is to check if the user has selected their name from the dropdown list and then pressed "submit". If the user hasn't chosen his/her name then it should give a message saying "Please select a name".

For some reasons this isn't working.

Code: [Select]<?php
include 'config.php';

if ($_POST['kasutaja']) {
$_SESSION['user'] = $_POST['kasutaja'];
}
else {
echo "<p style=' font-size: 50px; text-align: center;' id='tkt'><br><br>Palun sisesta enda nimi</p>";
echo "<br>";
echo "<center><form method='link' action='index.php'>
<input id='nav' type='submit' style='padding: 10px;' value='Tagasi algusesse'>
</form></center>";
}
header('Location: search.php');
?>If you want to DISPLAY the message before sending anything to the web server, you should use a client-side script LANGUAGE (the best supported is javascript.) instead of PHP.

It's hard to tell what's going on in this small amount code without seeing more of it, but you are using the header() command after sending  output (the echo command here). That's wrong and will usually cause an error. What is the purpose of the Location: redirect anyway?

The echo commands need to go in the same page as where the original form is displayed to SHOW the message there, or on this page but with the header() call moved to show the message on a separate page after clicking submit.What exactly is the error?It's working when I do not enter the name. It gives me the "please enter your name" but if I do select the name, it doesn't redirect me to search.php as it should. I recorded a video as well of what it's doing.

http://www.youtube.com/watch?v=zqI-d83yUsg


index.php
Code: [Select]<?php
include "config.php";

$query = mysql_query("set names 'utf8'");
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
ob_start('mb_output_handler');
header('Content-type: text/html; charset=utf-8');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>Sisene</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<link rel="stylesheet" type="text/css" href="css/index.css">
</head>
<body>
<h1>Sisselogimine</h1>
<hr>
<div id="login">
<form name="form" action="user.php" enctype="multipart/form-data" method="post">
<div id="inputtitle">Sisesta enda nimi:</div><br>
<select name="kasutaja" id="kasutaja">
<option> </option>
<option>Kalle Kaalikas</option>
<option>Pirjo Põim</option>
<option>Janar Pilv</option>
<option>Erlend Anderson</option>
<option>Sille Suursaar</option>
<option>Kaisa Viljar</option>
<option>Liis Kurvits</option>
<option>Lenne Kontor</option>
<option>Lindsay Meadows</option>
<option>Ronnie Coleman</option>
<option>Marek Kalmus</option>
</select>
<input type="submit" name="submit" value="Sisene">
</form>
</div>
</body>
</html>


User.php

Code: [Select]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="css/search.css">
<title>Algus</title>
</head>
<body>
<?php
include 'config.php';

if ($_POST['kasutaja']) {
$_SESSION['user'] = $_POST['kasutaja'];
}
else {
echo "<p style=' font-size: 50px; text-align: center;' id='tkt'><br><br>Palun sisesta enda nimi</p>";
echo "<br>";
echo "<center><form method='link' action='index.php'>
<input id='nav' type='submit' style='padding: 10px;' value='Tagasi algusesse'>
</form></center>";
}
header('Location: search.php');
?>
</body>
</html>
Yeah, the problem is in user.php.

You cannot use the header() function after you have outputted HTML to the page. I also think (but not sure) that you can't initiate the $_SESSION variable after outputting HTML either.

Instead of using the header function there you could just use the following:
Code: [Select]<script>
window.location.href='search.php';
</script>
Thank you kpac! That somewhat worked. I do not get a blank page anymore but now if I don't select a username from the list it displays the error page really quickly, like 1/4th of a second maybe and then redirects to the search.php page. When I do select the name, it's alright.

Sorry but I'm a complete rookie at PHP. I can UNDERSTAND the code but I do not know how to write it entirely by myself.
This is here is beyond my knowledge.



Discussion

No Comment Found