I have the below php page, which is a holding page with a contact form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<!--<link rel="icon" href="../../favicon.ico">-->
<title>Coming Soon</title>
<!-- Bootstrap -->
<link href="assets/css/bootstrap.css" rel="stylesheet">
<link href="assets/css/bootstrap-theme.css" rel="stylesheet">
<link href="assets/css/font-awesome.css" rel="stylesheet">
<!-- siimple style -->
<link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>TEST</h1>
<h2 class="subtitle">We're working hard to improve our website and we'll be ready to launch soon
<h2 class="subtitle">Feel free to contact us below with any enquiries</h2>
<p>
<?php
$name = $_POST["contactname"];
?>
<form class="Contact" method="post" action="">
<label class="alignleft">Name</label>
<input type="text" name="contactname" id="contactname" placeholder="Enter Name">
<label class="alignleft">Email</label>
<input name="Email" placeholder="Email Address">
<label class="alignleft">Enquiry</label>
<textarea name="Enquiry" placeholder="Your enquiry"></textarea><br>
<input id="submit" name="submit" type="submit" value="Submit!" class="btn btn-theme">
</form>
<div class="social">
<i class="fa fa-facebook" aria-hidden="true"></i>
<i class="fa fa-twitter" aria-hidden="true"></i>
<!--<i class="fa fa-google-plus" aria-hidden="true"></i>
<i class="fa fa-linkedin" aria-hidden="true"></i>-->
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
</body>
</html>
Within the php section I am trying to read the values of the input boxes, however even with only
$_POST["contactname"];
the website returns a 500 Internal Server Error. If i remove that line and replace with a simple:
echo "Test";
Then the site works and displays "Test"
If there something I am missing with the assigning of the variable from the input box?
You should make sure the key of the POST array is set and then validate your values first before you show them, and be sure to handle errors and invalid values!
if(isset($_POST["contactname"])){
// Do validation, like making sure its not a empty string
if (!empty($_POST["contactname"])) {
echo $_POST["contactname"];
} else {
echo "A validation error";
}
}
Also enable PHP error output which will greatly help you debug issues like this:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
And display_errors = on in your php.ini to make PHP show parse errors.
I would recommend using a second file to process the form instead of having it all on one page. Not only does it make your code look cleaner but I find it can also help with debugging. So it will look something like this:
index.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<!--<link rel="icon" href="../../favicon.ico">-->
<title>Coming Soon</title>
<!-- Bootstrap -->
<link href="assets/css/bootstrap.css" rel="stylesheet">
<link href="assets/css/bootstrap-theme.css" rel="stylesheet">
<link href="assets/css/font-awesome.css" rel="stylesheet">
<!-- siimple style -->
<link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>TEST</h1>
<h2 class="subtitle">We're working hard to improve our website and we'll be ready to launch soon
<h2 class="subtitle">Feel free to contact us below with any enquiries</h2>
<form class="Contact" method="post" action="form_process.php">
<label class="alignleft">Name</label>
<input type="text" name="contactname" id="contactname" placeholder="Enter Name">
<label class="alignleft">Email</label>
<input name="Email" placeholder="Email Address">
<label class="alignleft">Enquiry</label>
<textarea name="Enquiry" placeholder="Your enquiry"></textarea><br>
<input id="submit" name="submit" type="submit" value="Submit!" class="btn btn-theme">
</form>
<div class="social">
<i class="fa fa-facebook" aria-hidden="true"></i>
<i class="fa fa-twitter" aria-hidden="true"></i>
<!--<i class="fa fa-google-plus" aria-hidden="true"></i>
<i class="fa fa-linkedin" aria-hidden="true"></i>-->
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
</body>
</html>
form_process.php:
<?php
if(isset($_POST['contactname'])
{
$name = $_POST['contactname'];
if(empty($name))
{
die("contact name is empty")
}
else
{
echo $name;
}
}
// Continue processing form data
?>
I would also follow the recommendations from other users about displaying PHP errors.
You should validate it first
if(isset($_POST['contactname'])){
// Do your post handling
}
Firstable , you need to check if $_POST is defined , and not empty :
if(isset($_POST["contactname"]) && !empty($_POST["contactname"])){
echo $_POST["contactname"];
}
Second , i think you need to configure you'r server to show errors and not to redirect you to 500 error:
Just modify your php.ini with this line:
display_errors = on
Related
I have been writing one HTML page i will let below this, and a PHP script i will post too. But when i use the HTML page to write information and submit it, i get the "The requested URL was not found on this server." message, i have all files into one folder on htdocs named "html", the html page is named "index" and the script is named "test", thank you so much for your time.
[HTML code]
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="/docs/4.0/assets/img/favicons/favicon.ico">
<title>Signin Template for Bootstrap</title>
<link rel="canonical" href="https://getbootstrap.com/docs/4.0/examples/sign-in/">
<!-- Bootstrap core CSS -->
<link href="../../dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="signin.css" rel="stylesheet">
</head>
<body class="text-center">
<form method="post" action="next.php">
<img class="mb-4" src="https://getbootstrap.com/docs/4.0/assets/brand/bootstrap-solid.svg" alt="" width="72" height="72">
<h1 class="h3 mb-3" font-weight="normal"> Please sign in </h1>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
</form>
</body>
</html>
PHP script
<?php
$handle = fopen("pruebapracticaHE.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit
?>
You said that script name is test.php
but what i can see in your HTML code that you set action="next.php" in the form instead of action="test.php" the right code is :
<form method="post" action="test.php">
In the handle_signup.php page, I try to handle with the data which was sent from the sign up page. If the nickname blank has no value, then the error message is "Please enter your nickname". So, in the signup page, I try to include the $errorMsgs array from handle_signup.php page and show the msg below the nickname input. How can I get the array from the handle_signup.php page? Currently, I use include_once, but there is no message in the array after transferring to handle_signup.php. Could some one help this issue? Thank you.
Handle the value from signup.php
<?php
require_once('./conn.php');
$errorMsgs = array('nickname'=>'', 'email'=>'', 'password'=>'');
if(isset($_POST['submit'])) {
if(empty($_POST['nickname'])) {
$errorMsgs['nickname'] = "Please enter your nickname";
header("Location: ./signup.php");
}
};
?>
Sign up page - signup.php
<?php
include_once 'handle_signup.php';
var_dump($errorMsgs);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
<title>Message Board - Sign Up</title>
</head>
<body>
<div class="container_sign">
<h1 class="title">Create Account</h1>
<form class="sign" method="POST" action="./handle_signup.php">
<div>
<i class="far fa-user"></i>
<input type="text" placeholder="Nickname" name="nickname">
</div>
<p class="warning__msg"></p>
<div>
<i class="far fa-envelope"></i>
<input type="text" placeholder="Email" name="email">
</div>
<p class="warning__msg"></p>
<div>
<i class="fas fa-lock"></i>
<input type="password" placeholder="Password" name="password">
</div>
<p class="warning__msg"></p>
<input type="submit" value="SIGN UP" name="submit">
</form>
</div>
</body>
</html>
As DarkBee suggested, you can use $_SESSION for your error messages, or simply don't use header("Location: ./signup.php") but include all your PHP code in the same page.
I cannot get the PHP code to run within the HTML code. It simply will not work and I can't figure it out. I removed the <head></head> part of the html and the PHP login script works again. How do I fix this?
Here is my PHP:
<?php
include("db.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from Form
$username = mysqli_real_escape_string($db,$_POST['username']);
$password = mysqli_real_escape_string($db,$_POST['password']);
$password = md5($password); // Encrypted Password
$sql = "SELECT id FROM Users WHERE username='$username' and passcode='$password'";
$result = mysqli_query($db,$sql);
$count = mysqli_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1) {
header("location: greatjob.php");
}
else {
$error = "Your Login Name or Password is invalid";
}
}
?>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="test Portal | Login" />
<meta name="author" content="test" />
<title>test Portal | Login</title>
<link rel="stylesheet" href="logintemplate/js/jquery-ui/css/no-theme/jquery-ui-1.10.3.custom.min.css">
<link rel="stylesheet" href="logintemplate/css/font-icons/entypo/css/entypo.css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Noto+Sans:400,700,400italic">
<link rel="stylesheet" href="logintemplate/css/bootstrap.css">
<link rel="stylesheet" href="logintemplate/css/neon-core.css">
<link rel="stylesheet" href="logintemplate/css/neon-theme.css">
<link rel="stylesheet" href="logintemplate/css/neon-forms.css">
<link rel="stylesheet" href="logintemplate/css/custom.css">
<script src="logintemplate/js/jquery-1.11.0.min.js"></script>
<script>$.noConflict();</script>
<!--[if lt IE 9]><script src="logintemplate/js/ie8-responsive-file-warning.js"></script><![endif]-->
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body class="page-body login-page login-form-fall" data-url="http://test.dev">
<!-- This is needed when you send requests via Ajax -->
<script type="text/javascript">
var baseurl = '';
</script>
<div class="login-container">
<div class="login-header login-caret">
<div class="login-content">
<a href="index.html" class="logo">
<img src="logintemplate/images/logo#2x.png" width="120" alt="" />
</a>
<p class="description">Dear user, log in to access the admin area!</p>
<!-- progress bar indicator -->
<div class="login-progressbar-indicator">
<h3>43%</h3>
<span>logging in...</span>
</div>
</div>
</div>
<div class="login-progressbar">
<div></div>
</div>
<div class="login-form">
<div class="login-content">
<div class="form-login-error">
<h3>Invalid login</h3>
<p><?PHP echo $error; ?></p>
</div>
<form method="post" action="login.php" role="form" id="form_login">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">
<i class="entypo-user"></i>
</div>
<input type="text" class="form-control" name="username" id="username" placeholder="Username" autocomplete="off" />
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">
<i class="entypo-key"></i>
</div>
<input type="password" class="form-control" name="password" id="password" placeholder="Password" autocomplete="off" />
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block btn-login">
<i class="entypo-login"></i>
Login In
</button>
</div>
</form>
<div class="login-bottom-links">
Forgot your password?
<br />
ToS - Privacy Policy
</div>
</div>
</div>
</div>
<!-- Bottom scripts (common) -->
<script src="logintemplate/js/gsap/main-gsap.js"></script>
<script src="logintemplate/js/jquery-ui/js/jquery-ui-1.10.3.minimal.min.js"></script>
<script src="logintemplate/js/bootstrap.js"></script>
<script src="logintemplate/js/joinable.js"></script>
<script src="logintemplate/js/resizeable.js"></script>
<script src="logintemplate/js/neon-api.js"></script>
<script src="logintemplate/js/jquery.validate.min.js"></script>
<script src="logintemplate/js/neon-login.js"></script>
<!-- JavaScripts initializations and stuff -->
<script src="logintemplate/js/neon-custom.js"></script>
<!-- Demo Settings -->
<script src="logintemplate/js/neon-demo.js"></script>
</body>
</html>
Based on your provided link, your site is using AJAX for login so the jQuery is preventing the form from submitting normally. To make it work do one of these:
Change where your ajax is pointing
Make your changes to the page that the AJAX is currently pointing to
Give the login form a different id like <form id="formlogin"> and the ajax will ignore it and reload your page the way you want it to.
I cannot seem to link my login page to my home page, any help would be much appreciated. I know its connecting to the database as i can create users but for some reason its not letting me sign in with a created user.
<?php require ("insert.php"); ?>
<?php
if(isset($_POST[ 'Login' ]))
{
$email= mysqli_real_escape_string($con, $_POST ['email']);
$pswrd= mysqli_real_escape_string($con, $_POST ['pswrd']);
$result = $con->query (" select * from users where email='$email' AND pswrd='$pswrd' ");
$row = $result->fetch_array(MYSQLI_BOTH);
session_start();
$_SESSION["User ID"] = $row['UserID'];
header ('Location: home.php');
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>AMSadler login</title>
<meta charset="utf-8">
<meta name="description" content="description of webpage">
<meta name="keywords" content="keywords go here">
<meta name="author" content="Anthony">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/login.css">
<link rel="index" href="index.php">
<link rel="icon" href="img/favicon.png" sizes="16x16" type="image/png">
</head>
<body>
<div class="header">
<div id="logo">
<img src="img/logo.png" alt="logo" title="AMSadler.com"/>
</div>
<div id="signup">
<button type="button">Sign up</button>
</div>
</div>
<div id="login">
<form>
<input type="text" name="email" placeholder="Email address">
<br>
<input type="password" name="password" placeholder="Password">
<br>
<input id="Login" type="submit" name="Login" value="Login">
</form>
</div>
<footer>
<div id="copyright">
<p>© Copyright 2015</p>
</div>
</footer>
</body>
</html>
You need to change your form tags.
From,
<form>
To,
<form action="yourPageName.php" method="post">
You need to specify your method type so in your PHP code you can get it using that method. If you leave out the method part it by default uses $_GET. Seeing as your code is pointing to $_POST then you set it to method="post".
You also need to set action="" this can be set to # for same page or leave it blank or using a file name. This will redirect the form to that page.
I have a simple login page with 2 textfields and a button. When I press "Submit", the URL changes to the php page but I see absolutely nothing at all. I have no idea how to fix it so I'm hoping somebody can help me.
My HTML page:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="style2.css" />
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
<link rel="stylesheet" href="themes/customtheme.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<a class="ui-btn-left" href="index.html" data-icon="back">Back</a>
<h1><span>Login</span></h1>
<a class="ui-btn-right" href="#" data-icon="info">i & €</a>
</div>
<div data-role="content" data-position="relative">
<div class="loginform">
<form id="loginForm" action="login.php" method="POST">
<span>Email adress:</span>
<input type="text" name="email" id="email"></input>
<span>Password:</span>
<input type="password" name="password" id="password"></input>
<input type="submit" value="Login" />
</form>
</div>
</div>
<div data-role="footer" data-position="fixed"></div>
</div>
</body>
</html>
And my login.php:
<?php session_start();
error_reporting(E_ALL);
ini_set('display_errors',TRUE);
ini_set('display_startup_errors',TRUE);
echo ("Test");
?>
I don't even see 'Test' nor any error...
Its actually not a php problem. Assume you are using JQuery mobile. Disabling data-ajax may help.
<script type="text/javascript">
$(document).bind("mobileinit", function () {
$.mobile.ajaxEnabled = false;
});
</script>
Use this code on your html page.