What problem might I have with this post method code? - php

I have a problem with the following php code. I am trying to submit password, while submission is successful the echo part of php to be displayed on webpage but am not getting any error or response.
<html>
<head>
<title>POST METHOD</title>
</head>
<body>
<form action="login.php" method="post">
Please enter your password:<br>
<input type="password" name="pwd" value="password"><br><br>
<input type="submit" name="Submit">
</form>
</body>
</html>
<?php
$password='password';
if(isset($_POST['password']) &&!empty($_POST['password'])){
echo 'submtted and filled';
}
?>

You are using wrong name for post: you need to use pwd instead of password
if(isset($_POST['pwd']) && !empty($_POST['pwd'])){
echo 'submtted and filled';
}

Hi you need to use name in post metho, to get value of any text/other fields. But in your code you are using type(password) that's why your password not coming in server side. You can use below code, I hope it may works
<html>
<head>
<title>POST METHOD</title>
</head>
<body>
<form action="login.php" method="post">
Please enter your password:<br>
<input type="password" name="pwd" value="password"><br><br>
<input type="submit" name="Submit">
</form>
</body>
<?php
if(isset($_POST['pwd']) && $_POST['pwd'] !='')
{
echo 'submtted and filled';
}
else
{
echo 'Something went wrong please try again';
}
?>

Related

Undefined index in uname and upass in php-why

I am new to PHP and i am trying to develop a small application in it. Received an undefined index error, tried to set the path right however could not overcome this error. It would be great if someone could help me solve this error. Please find the code attached below.
ERROR:Undefined index: uname in D:\wamp64\www\bbms\loginCheck.php
ERROR:Undefined index: upass in D:\wamp64\www\bbms\loginCheck.php on line 4
1.header.php
<html>
<body>
<frameset cols="20%,*">
<frame name="logo">
<img src="images/logo.jpg" width="20%" height="30%"/>
</frame>
<frame name="login">
<?php include('login.php');?>
</frame>
</body>
</html>
2.login.php
<html>
<head>
<script type="text/javascript" language="JavaScript">
function validate()
{
if(document.getElementByName("uname").value==""||document.getElementByName("upass").value=="")
{
document.getElementById("msg").innerHtml="Donor Login and password must not be left empty";
document.getElementByName("uname").focus();
document.getElementByName("upass").focus();
}
else
{
<?php header('Location:loginCheck.php');?>
}
}
</script>
<?php
if (isset($_GET['msg'])) {
$msg=$_GET['msg'];
}
else
$msg="";
?>
</head>
<body>
<p id="msg"><?php echo $msg;?></p>
<form method="post">
Donor Login<input type="text" name="uname" value="Enter your login id"><br>
Password<input type="password" name="upass" value="Enter the password"><br>
Forgot Password?<br>
<input type="submit" value="Login" onClick="validate()"/>
</form>
</body>
</html>
3.loginCheck.php
<?php
$uname=$_REQUEST["uname"];
$upass=$_REQUEST["upass"];
if($uname==""||$upass=="")
{
//echo "Must not be empty";
header('Location:header.php?msg=Donor Login and password must not be left empty');
}
else if($uname=="admin"&&$upass=="admin")
{
session_start();
echo "Welcome user";
}
else
{
header('Location:login.php?msg=Invalid Login');
}
?>
I think its not getElementByName it should be getElementsByName.
Please keep this source to cross check your syntaxes from next time.
Here is the link.
loginCheck.php should be the action of your form and not a redirect in the validate() function.
Try this way.
<html>
<head>
<script type="text/javascript" language="JavaScript">
function validate()
{
if(document.getElementByName("uname").value==""||document.getElementByName("upass").value=="")
{
document.getElementById("msg").innerHtml="Donor Login and password must not be left empty";
document.getElementByName("uname").focus();
document.getElementByName("upass").focus();
return false
}
return true
}
</script>
<?php
if (isset($_GET['msg'])) {
$msg=$_GET['msg'];
}
else
$msg="";
?>
</head>
<body>
<p id="msg"><?php echo $msg;?></p>
<form method="post" action="loginCheck.php" onsubmit="return validate()">
Donor Login<input type="text" name="uname" value="Enter your login id"><br>
Password<input type="password" name="upass" value="Enter the password"><br>
Forgot Password?<br>
<input type="submit" value="Login" onClick="validate()"/>
</form>
</body>
</html>

How to make "MadLibs" form results appear below form fields after submitting?

Thanks in advance for your help. I've searched a lot before posting this but I end up more confused than when I started :)
I'm trying to have one page contain the form fields and after pressing submit, the resulting story with user's form field entries inserted into the story.
It would be great to have the text from the form fields remain so that the user doesn't need to retype everything if they need to change a word or two.
I really appreciate your help. Hopefully this will help many people at once.
<html>
<head>
<title>My MadLib</title>
</head>
<body>
<h1>MadLib</h1>
<?php if (isset($_POST['action']) && $_POST['action'] == "show"): ?>
<p>Hello, I am a <?php echo $_POST['adj'] ?> computer that owns a <?php echo $_POST['noun'] ?>.</p>
<?php else : ?>
<form action="madlib.php" method="post">
<input type="hidden" name="action" value="show">
<p>An adjective: <input type="text" name="adj"></p>
**strong text** <p>A noun: <input type="text" name="noun"></p>
<p><input type="submit" value="Go!"></p>
</form>
<?php endif ?>
</body>
</html>
As you said you don't want to "keep it simple", you may simply add the needed value attribute to each of your <input>s, like this:
<html>
<head>
<title>My MadLib</title>
</head>
<body>
<h1>MadLib</h1>
<?php
if (isset($_POST['action']) && $_POST['action'] == "show") {
?>
<p>Hello, I am a <?php echo #$_POST['adj']; ?> computer that owns a <?php echo #$_POST['noun']; ?>.</p>
<?php
} else {
?>
<form action="madlib.php" method="post">
<input type="hidden" name="action" value="show">
<p>An adjective: <input type="text" name="adj" value="<?php echo #$_POST['adj']"; ?> /></p>
**strong text**
<p>A noun: <input type="text" name="noun" value="<?php echo #$_POST['noun']"; ?> /></p>
<p><input type="submit" value="Go!"></p>
</form>
<?php
}
?>
</body>
</html>
Note the (sometimes unloved) "#" to prevent firing a notice when $_POST['...'] doesn't exist yet. I also added the same in your <p>Hello... line.

Why does it always echo the "Not set!" though I enter data?

Though I enter data and hit Submit it always echoes the else part always. I know it isn't the type of question to be asked on Stackoverflow but...
<html>
<head>
<title>Sticky Form</title>
</head>
<body>
<form method="POST" action=<?php echo $_SERVER['PHP_SELF'] ?>>
<label for="Name">Name</label>
<input type="text" name="FName">
<input type="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$f_name = $_POST['FName'];
echo "$f_name";
}
else
{
echo "Not set!";
}
?>
</body>
</html>
Change this:
<input type="submit">
to
<input type="submit" name="submit">
P.S: key name in global arrays comes from users input ($POST,$_GET,$_COOKIE), if you want to change its key, you need to change that element's name!

header() is not redirecting to the page in PHP

I have the following PHP printExam.php page:
<?php
$logins = array(
'user' => 'pass',
'user1' => 'pass1',
'user2' => 'pass2',
'user3' => 'pass3',
'user4' => 'pass4'
);
// Clean up the input values
foreach($_POST as $key => $value) {
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}
/******************************************************************************/
if (isset($_POST['submit'])){
$user = isset($_POST['user']) ? strtolower($_POST['user']) : '';
$pass = isset($_POST['pass']) ? $_POST['pass'] : '';
$report = $_POST['typereport'];
if ((!array_key_exists($user, $logins))||($logins[$user] != $pass)) {
showForm("Wrong Username/Password");
exit();
}
else {
if ($report == "Clinical") {
?>
<html>
<head>
</head>
<body>
CLINICAL PAGE
</body>
</html>
<?php
}
elseif ($report == "Annual Education") {
?>
<html>
<head>
</head>
<body>
ANNUAL EDUCATION PAGE
</body>
</html>
<?php
}
}
} else {
showForm();
exit();
}
function showForm($error=""){
?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Certificate Printing :: Login</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
<Script>
$(function() {
$("#user").focus();
});
</Script>
</head>
<body>
<form id="login" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd">
<h1>Log In</h1>
<fieldset id="inputs">
<input id="user" name="user" placeholder="Username" autofocus="" required="" type="text">
<input id="pass" name="pass" placeholder="Password" required="" type="password">
</fieldset>
<fieldset id="actions">
<input type="radio" name="typereport" id="cbox" value="Clinical" checked="yes" /> Clinical
<input type="radio" name="typereport" id="cbox" value="Annual Education" /> Annual Education
</fieldset>
<fieldset id="actions">
<input id="submit" name="submit" value="Log in" type="submit">
</fieldset>
<div class="caption"><?php echo $error; ?></div>
</form>
</body>
</html>
<?php
}
?>
For printCert.php and printCertHR.php, the very first line is:
<?php require_once('printExam.php'); ?>
What it is suppose to do is call the printExam.php page each time the user visits either pages. If the username AND password matches and depending on the selection, whether it's clinical or annual education, it should take the user to the correct page. I know the form is working correctly, because if I enter wrong username/password it shows me the error but once correct, it doesnt redirect. Any idea how to resolve it?
Please Note: the username/password is simplified for the example only!
The 'Location' header command in PHP should be followed with an exit;
Otherwise the code below is executed, ie any output is sent to the browser.
See the examples from the PHP header page:
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
Location (or any other header) needs to be the first thing echoed by your script. Remove the blank lines in your script.
You have contents being displayed before the header is being called (as for your comment to Rob W). The way to get around it is, put ob_start(); at the top of your php code, and ob_end_flush(); at the end of your code so the header can be called anywhere in between your code.
Is it possible that you might need to use PHP's Output Buffer to fix that? When the script is parsed, having the output of the form in a function might be throwing it off, as the function will be parsed before the rest of the script is run.
Also, you should use strcmp for comparing strings, not the == sign. strcmp vs ==
Try using the output buffer functions and see if that fixes it. it's gonna look something like this:
function showForm($error=""){
ob_start(); ?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Certificate Printing :: Login</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
<Script>
$(function() {
$("#user").focus();
});
</Script>
</head>
<body>
<form id="login" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd">
<h1>Log In</h1>
<fieldset id="inputs">
<input id="user" name="user" placeholder="Username" autofocus="" required="" type="text">
<input id="pass" name="pass" placeholder="Password" required="" type="password">
</fieldset>
<fieldset id="actions">
<input type="radio" name="typereport" id="cbox" value="Clinical" checked="yes" /> Clinical
<input type="radio" name="typereport" id="cbox" value="Annual Education" /> Annual Education
</fieldset>
<fieldset id="actions">
<input id="submit" name="submit" value="Log in" type="submit">
</fieldset>
<div class="caption"><?php echo $error; ?></div>
</form>
</body>
</html>
<?php
return ob_get_flush();
}
Output Buffers (php.net)
After your elseif ($report == "Annual Education") { block, try print_r($report) - ensure it's what you are expecting...
if ($report == "Clinical") {
header ("Location: printCert.php");
}
elseif ($report == "Annual Education") {
header ("Location: printCertHR.php");
}
print_r($report);
Also, try monitoring FireBug's NET tab (or CHROME's)

php javascript alertbox have to click 2 times before it show

I'm doing php that is textbox a value empty it will open a alertbox (I'm using javascript in here )
this is my code
<?php
include('config.php');
if(isset($_POST['submit'])){
$username=$_POST['username'];
?>
<script>
function validate(){
if(document.forms[0].username.value==""){
window.alert("You must enter both values");
return false;
}
}
</script>
<?php
}
?>
<html>
<div><p>Member Profile</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username<br>
<input class="user" type="text" name="username" id="username" /><br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</html>
The problem is i have to click 2 times before the alert show
please help me to solve this problem
It's because the script is inside the php if(isset){} block, one click submits the form, which generates the script and then it works the second time.. try this setup instead:
<?php
include ('config.php');
if (isset($_POST['submit']))
{
$username = $_POST['username'];
}
?>
<html>
<head>
<script>
function validate () {
if (document.forms[0].username.value == "") {
window.alert("You must enter both values");
return false;
}
}
</script>
</head>
<body>
<div>
<p>
Member Profile
</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username
<br>
<input class="user" type="text" name="username" id="username" />
<br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</body>
</html>
Edit:
I've moved the script tag inside the head tag. I'm not sure if there are any implications for having the script outside but just to be sure I've moved it.
2nd Edit: (OCD is kicking in)
I've added body tags, not sure if you copied and pasted this code but it looked weird to me :)

Categories