I am having an issue with my html forms not submitting properly. I am new to forms and have been trying to get a login script going but it has been failing. I am using chrome mostly. Here is the super dumbed down code that hopefully demonstrates my issue. I am guessing what is wrong in this code is in the extended code. This is the website I used for my login coding: login account setup site mainly for reference
<?php
//this is here for testing
session_start();
//this shows
echo "something";
//none of these show
if(isset($_POST['submit'])){
echo "something";
echo "something";
echo "something";
echo "something";
}
if(isset($_POST['submit2'])){
echo "something";
echo "something";
echo "something";
echo "something";
}if(isset($_POST['submit3'])){
echo "something";
echo "something";
echo "something";
echo "something";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<footer>
<!-- I have tried it with and without an action -->
<form action="button_test.php" method="post" id="this" style="width: 100%; height: 100%; background-color: darkblue;">
<button type="submit" name="submit" id="submit" class="button" value="Submit">this is something</button>
<button type="button" name="submit2" id="submit2" class="button" value="this">this is something2</button>
<input type="button" name="submit3" id="submit3" class="button" value="true"/>
</form>
</footer>
</body>
</html>
The first button predictably reloads the page, while the others do nothing. This is the extended main code that I need working. Once again, I am guessing the code above probably explains what is wrong with the code beneath. The code beneath is primarily there to give context and more accurate answers.
<?php
//require once the DatabaseController.php file,
require_once 'Controllers/DatabaseController.php';
echo "this happend1";
// Creates a data connect object that creates a connection to
// a database
$connection = new DataConnect;
//Run the user_load function to set the user
$connection->user_load();
echo "this happend2";
/*
* Runs the in_loggedin function from a
* user object and checks if it is blank
*/
if($user->is_loggedin()!="")
{
echo "this happend3";
//return user to logged in home page if
//they are logged in
$user->redirect('index.php');
}
echo "this happend4";
/*
* THIS DOES NOT WORK FOR SOME REASON
*/
if(isset($_POST['btn-login']))
{
echo "this happend5";
//Load the variables with the new form data that has been executed
$uname = $_POST['txt_uname_email'];
$umail = $_POST['txt_uname_email'];
$upass = $_POST['txt_password'];
//If the user->login call is successful, go to home fully logged in
if($user->login($uname,$umail,$upass))
{
//Go to home, fully logged in
$user->redirect('index.php');
}
else
{
//Error
$error = "Wrong Details !";
}
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title> Josiah</title>
<link href="Assets/Stylesheets/styles-home.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
include_once 'header.php';
?>
<!-- recent blog post: contains the most recent blog post-->
<div id="main-content" class="content">
<div id="main-content-inner" class="inner-content">
<!-- this is the container for the form -->
<div class="form-container">
<!-- create a form that uses the "post" method which allows php to do $_POST on
corresponding variable names -->
<form method="post">
<h2>Sign in.</h2><hr />
<!-- checks if there was an error. If the error variable is set -->
<?php
if(isset($error))
{
?>
<!-- displays the error -->
<div class="alert alert-danger">
<i class="glyphicon glyphicon-warning-sign"></i> <?php echo $error; ?> !
</div>
<?php
}
?>
<!-- this is a form-group -->
<div class="form-group">
<!-- this is a text input. -->
<input type="text" class="form-control" name="txt_uname_email" placeholder="Username or E mail ID" required />
</div>
<!-- this is a form-group -->
<div class="form-group">
<!-- this is a text input. -->
<input type="password" class="form-control" name="txt_password" placeholder="Your Password" required />
</div>
<!-- splits parts of the form -->
<div class="clearfix"></div><hr />
<!-- this is a form group holdng the submit button -->
<div class="form-group">
<!-- the button interestingly has multiple classes -->
<button type="submit" name="btn-login" class="btn btn-block btn-primary">
<!-- splits parts of the form. Dont know specifically what this does. -->
<i class="glyphicon glyphicon-log-in"></i> SIGN IN
</button>
</div>
<br />
<label>Don't have account yet ! Sign Up</label>
</form>
</div>
</div>
</div>
<!-- footer: contains social media, site map, and an up arrow-->
<?php
include 'footer.php'
?>
</body>
</html>
Since php://input shows the form value (according to the test I had you run in comments), we know that PHP is properly receiving the data.
By default, PHP will place request parameters in $_GET and $_POST as appropriate. In your case this doesn't seem to be happening. I suggest you look at your php.ini file and add the setting below:
variables_order = "GPCS"
There may already be a variables_order line, so just edit it in that case (documentation).
So after some searching I forgot to disclose that I was using PHPstorm. The reason why this did not occur to me is that PHPstorm is not installed on another computer that I have been using, however since I use Intellij on both computers, this bug in PHPstorm transferred regardless since Intellij will use PHPstorm plugins. It seems that 10.1-10.3 this bug persists where POST remains empty. I am not sure if it was fixed so I moved over to Apache and have it doing the web serving instead. Everything works, or at least works better. The 2 solutions are either wait for PHPstorm to fix this bug, or better move over to Apache.
link to site post
Related
I am new to PHP and I am trying to develop a simple login system where it echoes a success message and redirects to a secure page and when details are wrong, it echoes an error message and reloads the login form.
I have been trying to for a while now and cannot figure it out, even though I have some functionality in terms of it directing to the correct page.
My database on PhpMyAdmin is correctly configured. Also, any help on sessions would be greatly appreciated.
PHP CODE:
<?php
$servername = "localhost";
$username = "root";
$password = "cornwall";
$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill':
$username = $_POST['username'];
$password = $_POST['password'];
//These are the different PHP variables that store my posted data.
$login="SELECT * FROM users WHERE username='$username' AND password='$password'";
$result=mysqli_query($con, $login);
$count=mysqli_num_rows($result);
//This is the query that will be sent to the MySQL server.
if($count==1)
{
header('Location: http://localhost/projects/ibill_v3/html/main.html#home');
exit();
}
//This checks the 'user_details' database for correct user registration details and if successful, directs to home page.
else {
header('Location: http://localhost/projects/ibill_v3/html/loginform.html');
echo "Wrong details";
exit();
}
//If login details are incorrect
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
?>
HMTL CODE
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1; minimum-scale=1;">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<link href="/projects/ibill_v3/css/mainstyles.css" rel="StyleSheet"/>
<link href="/projects/ibill_v3/css/loginform.css" rel="StyleSheet"/>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="script.js"></script>
<script type='text/javascript' src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script type='text/javascript'>
$(document).on('pageinit', function(){
$('.loginform').validate({ // initialize the plugin
// rules & options
});
});
</script>
</head>
<body>
<!--********************************LOGIN FORM PAGE**********************************************-->
<!--****************************************************************************************-->
<!--********************************HEADER**********************************************-->
<div data-role="page" id="loginform">
<div data-role="header" data-id="foo1" data-position="fixed">
<h1>Register</h1>
</div>
<!--********************************HEADER**********************************************-->
<!--********************************MAIN**********************************************-->
<div data-role="main" class="ui-content">
<img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="250" height="190">
<h2>Sign in</h2>
<section class="loginform">
<form data-ajax="false" method="POST" action="loginform.php" >
<ul>
<li>
<label for="username">Username</label>
<input type="text" name="username" id="username" class="required" minlength="5" placeholder="enter username (min-5 characters)">
</li>
<li>
<label for="password">Password</label>
<input type="password" name="password" placeholder="enter password" minlength="6">
</li>
<div id="loginformbutton">
<button class='active' type='submit' value='submit'>Sign in</button>
</div>
<p>Don't have an account? Sign up!</p>
<div id="registerbutton">
Register
</div>
</ul>
</form>
</section>
</div>
<!--********************************MAIN**********************************************-->
<!--********************************FOOTER**********************************************-->
<div data-role="footer">
<footer class="footer">
<p>awilliams©</p>
</footer>
</div>
</div>
<!--********************************END OF LOGIN FORM PAGE**********************************************-->
<!--****************************************************************************************-->
</body>
...
else
{
header('Location: http://localhost/projects/ibill_v3/html/loginform.html');
echo "Wrong details";
exit();
}
The above is going to redirect before your echo statement is reached, so nothing will be displayed.
Secondly, the following line:
<form data-ajax="false" method="POST" action="loginform.php" > will not send any data back to your file containing the form if you're using echo statements. It is going to redirect to the loginform.php and will stay there if you do not explicitly redirect back the page with your form.
Instead, use:
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> as your form's action. And then include your loginform.php somewhere before the form in your HTML.
This is going to send data back to the form's file and replaces special characters to HTML entities (for security), it also allows you to use echo's or variables to return messages to the user.
loginform.php will need to check if specific inputs are posted:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['username'] && $_POST['password'])
{
//do work
}
}
Here is a basic php form tutorial to start you off: php tutorial
I think it's because your redirecting to the same page with no post. I didn't look through all the code, that is just my first stab at it
This will not appear on loginform.html:
echo "Wrong details";
Use something like:
$_SESSION['errorMessage'] = "Wrong details";
header('Location: http://localhost/projects/ibill_v3/html/loginform.html');
exit();
And then on loginform.html, add this code to display the error message:
if(isset( $_SESSION['errorMessage'])) echo $_SESSION['errorMessage'];
I've been making a website that requires a login, and I made it so that if the form is submitted, a little text will appear under the H1 tag in PHP that says Form Submitted. Right now that text isn't popping up. What did I do wrong?
Here's the code:
<?php
if ($_POST["submit"]) {
$result = "Form Submitted";
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1>My email form</h1>
<?php echo $result; ?>
<form method = "post">
<div class="form-group">
<label for = "name">Your Name: </label>
<input class="form-control" type="text" name = "name" />
</div>
<div class="form-group">
<label for = "email">Your Email: </label>
<input class="form-control" type="email" name = "email" />
</div>
<div class="form-group">
<label for = "comment">Your Comment: </label>
<textarea class="form-control" name = "comment" /></textarea>
</div>
<input type="button" id = "button" name = "submit" class="btn btn-success" value="Enter"/>
</form>
</div>
</div>
</div>
</body>
</html>
As I can see on http://www.remweb.org, PHP doesn't running on your server. (http://i.stack.imgur.com/kkwCW.png)
Change your button to a submit button..
That is, replace
<input type="button" id = "button" name = "submit" class="btn btn-success" value="Enter"/>
with
<input type="submit" id = "button" name = "submit" class="btn btn-success" value="Enter"/>
Try
<?php
if (isset($_POST["submit"])) {
$result = "Form Submitted";
}
?>
Try this
<input type="submit" id ="button" name ="submit" class="btn btn-success" value="Enter"/>
<?php
$result = "";
if (isset($_POST["submit"])) {
$result .= "Form Submitted";
}
?>
Sidenote: URL pulled from comments in another answer.
"If you want, this code is live at www.remweb.org, so you can see yourself. – Aaron Janke 2 hours ago"
There are a few things wrong here.
Firstly, your domain http://www.remweb.org/ has an index file named index.html (I typed in the index.html at the end just to make sure it was the same file/form), and in doing http://www.remweb.org/index.html it showed the form. Your server seems to be using the .html file as its default index.
I also (guessed and) went to http://www.remweb.org/index.php to see if you did have an PHP index file, and it showed "hello", therefore this tells me that PHP seems to be running.
Therefore, you need to replace the .html version with .php or rename your posted code's file as "form.php" for example, and it will work.
NOTA: http://www.remweb.org/index.html (being your form) contains PHP directives that are not being parsed by your server and does not by default.
Either you do as I suggest, or instruct Apache to treat .html files as PHP.
Plus, since your form contains 3 elements and the action is used for the same page, you need to use !empty() for those in order to echo the results of those input.
I.e.
if(!empty($_POST['name'])){
$name = $_POST['name'];
echo $name;
}
and do the same for the others.
Or, seperate your .html file from PHP and use a file pointer for the action and echo everything in the other file.
I.e.: action="action_file.php"
and hold the PHP directives in that file.
Your submit button's type <input type="button" needs to be a "submit" type and not "button" <input type="submit"
I am using PHP for a year now. I used to build my website using a way that i think it will not be suitable for an application that has multiple interfaces (web, Android, IOS..). I simply create an html page or a form and then handle the inputs in PHP script. This is a simple example of a login script:
This is the login form:
<?php
$error = '';
if(isset($_GET['error']))
$error = $_GET['error'];
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="dist/css/bootstrap.css" rel="stylesheet">
<title>Blogmaker Home</title>
</head>
<body>
<div class="container">
<nav class="navbar navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<a class="navbar-brand" href="#">BlogMaker</a>
</div>
<br><br>
</nav>
<!-- Static navbar -->
<!-- Main component for a primary marketing message or call to action -->
<div class="row">
<div class="col-md-8">
<div class="jumbotron">
<h1>BlogMaker</h1>
<p>You want to create your blog for free! You can do it right now.
Just click in the "Create My Blog" button below and start posting and receiving comments
from other peoples.
</p>
<p>
<a class="btn btn-lg btn-primary" href="Signup.php" role="button">Create My Blog</a>
</p>
</div>
</div>
<div class="col-md-4">
<?php
if($error=='')
echo'<br><br>';
else
echo'<div class="alert alert-danger">'.$error.'</div>';
?>
<form class="form-signin" action="login.php">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="form-control" placeholder="Email address" required autofocus name="username">
<input type="password" class="form-control" placeholder="Password" required name="pw">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
</div>
<div class="footer">
<p>© BlogMaker 2013</p>
</div>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="dist/js/bootstrap.min.js"></script>
</body>
</html>
Here is the login process handled in a PHP script:
<?php
session_start();
require_once 'DATABASE-CONFIG.php';
$username='';
$pw='';
if(isset($_GET['username'])&&isset($_GET['pw'])){
$username=$_GET['username'];
$pw=$_GET['pw'];
mysql_connect(SERVERNAME,DBUSERNAME,DBPASSWD) or die('Fail to connect to DB server');
mysql_select_db(DBNAME)or die('Fail to find the DB in the server');
$query = "SELECT * FROM users WHERE username='$username'";
$result = mysql_query($query);
mysql_close();
if(mysql_num_rows($result)>0){
$_SESSION['username']=$username;
header('Location:myblog.php');
}
else
header('Location:index.php?error=Please check your username and your password!');
}else
header('Location:index.php?error=Username and password are mandatory!');
?>
As you see, i call the html input fields by name and check user input. I believe this is a bad practice to build a website. So my question is what is the best practice to create a web application in PHP that deals with several interfaces? i just want the main concept. Also it would be very good with a small example.
Thanks
I would look into building a RESTful API. This way you can write all of your business logic once and make several clients interact with it (website, mobile apps, etc. Here is a good place to get started: API tutorial
You could use the MVC design pattern (Model - View - Controller). This will make it easy to decouple business logic from your presentation code by feeding different views to the different platforms you wish to support.
Most modern PHP web frameworks use the MVC architecture. Laravel, Fuel, etc...
Wikipedia on MVC
MVC Tutorial for beginners
so I'm new to php and I have two buttons on this html page here (the id value is included in the url):
<!DOCTYPE html>
<head>
<title>StoryBlox is a Social Story Builder Tool</title>
<meta charset="utf-8">
<!-- these support the header/footer formatting -->
<link type="text/css" rel="stylesheet" href="css/main.css">
<link type="text/css" rel="stylesheet" href="css/header_footer.css">
<script src="js/header.js"></script>
<?php //include_once 'confirm_login.php'
include_once 'story_manager.php';
include_once 'open_connection.php';
//include_once 'functions.php';
//sec_session_start();
if(isset($_GET['id'])){
$str_id = $_GET['id'];
$draft_id = get_story_attribute($str_id, 'draft');
}else{
echo "Invalid story id.";
echo "<br>";
}
?>
</head>
<body>
<div id="wrapper_main">
<div id="wrapper_content">
<?php include_once 'header.php'; ?>
<h1>Welcome to StoryBlox Create Story!</h1>
</div>
<!-- menu -->
<!--<div id="inputs"> -->
<form id="create_form" action="save_story.php?id=<?php echo $str_id?>" method="POST">
<input type="text" name="storyTitle" id="title" placeholder="Enter title." autofocus/><br>
<textarea rows="4" cols="50" name="storyDesc" id="description" placeholder="Enter description here."></textarea>
<div id="footer">
<input type="button" name="draftBtn" onclick="this.form.submit()" value="Save as Draft"/>
<input type="button" name="finalBtn" onclick="this.form.submit()" value="Finished!"/>
</div>
</form>
</div>
</div>
<?php include_once 'footer.php'; ?>
</body>
When I click one of these two buttons, I'm brought to this php document here:
include_once 'open_connection.php';
include_once 'story_manager.php';
$mysqli = open_connection();
if($_SERVER['REQUEST_METHOD'] === 'POST'){
if(isset($_POST['draftBtn'])){
$title = $_POST['storyTitle'];
$desc = $_POST['storyDesc'];
$str_id = $_GET['id'];
update_story_title($str_id, $title);
//update_story_description($str_id, $desc);
header('Location: createStory.php');
}
elseif(isset($_POST['finalBtn'])){
$title = $_POST['storyTitle'];
$desc = $_POST['storyDesc'];
$str_id = $_POST['storyID'];
update_story_title($str_id, $title);
//update_story_description($str_id, $desc);
save_draft_as_completed($str_id);
header('Location: ../home.php');
}else{ echo "failed";}
}?>
And I always get "failed" printed out on my page. I've been Googling this for hours and I don't understand where I'm going wrong here. If anybody could help that would be appreciated. Also, if anyone could shed some light on what the equivalent to
<input type="textarea">
would be that would be great. Thanks!
Use
<input type="submit" name="draftBtn" value="Save as Draft"/>
instead of the button types with their onclick events.
try to use submit tag instead of button.
and if you want to use button tag then you can pass value through hidden field. set value of hidden field on click event.
I have a basic HTML form like this. When submitted, it checks for empty fields and alert the user that both fields are required. After alert, user is redirected to the same page but text input sizes decrease. How can I retain the same width, height after PHP "echo"?
Screenhot 1:
Screenshot 2:
Screenshot 3:
Controller:
function validate_user_pass() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username','Email', 'required|max_length[30]');
$this->form_validation->set_rules('password','Password', 'required|max_length[32]');
if ($this->form_validation->run() == FALSE ) {
//$this->load->view('login_error');
echo '<script type="text/javascript">alert("Required fields are empty!");</script>';
$this->index();
}
else {
$this->verify();
}
}
View:
<?php echo form_open('login/validate_user_pass'); ?>
<div class="login">
<div class="login-screen">
<div class="login-icon">
<img src="<?php echo base_url(); ?>assets/images/1374606542_newsstand_ios7_ios_7.png" alt="logo">
<h4>Hotel<small>Management System</small></h4>
</div>
<div class="login-form">
<img src="<?php echo base_url(); ?>assets/ico/logo_small.png" alt="logo">
<div class="control-group">
<input type="text" class="login-field" name="username" value="<?php echo set_value('username'); ?>" placeholder="Enter username" id="login-name">
</div>
<div class="control-group">
<input type="password" class="login-field" name="password" value="" placeholder="Password" id="login-pass">
</div>
<input type="submit" class="btn btn-primary" value="Login">
</div>
</div>
</div>
<?php echo form_close(); ?>
it may be late to answer but same problem was with me. I overcome this by putting the error popup box out of the header. I searched everywhere and ended up here.
My code was something like this before:
<?php
//some code for verification and echo out/show error message
?>
<html>
<!--HTML code that has form-->
</html>
What I did was something like this:
<html>
<head>
<title></title>
</head>
<body>
<?php
//some code for verification and echo out/show error message
?>
<div>
<!--HTML code that has form-->
</div>
</body
</html>
This problem mostly exist because of echo present in header part. You can check your views for this and that may solve the problem.
This typically happens when a php error or var_dump() is on the page, but you may not see it because it is hidden under a fixed header div, for example.
Look at your View source & check if there aren't php errors on the page