I'm new to PHP and I'm trying to handle a form so I added a if condition to check if the submit button was pressed and if the other elements in the form are set. For some reason it causes the page to not load (it loads, but shows a blank page) so I can't actually submit anything..
When I tried removing the php code, it loaded fine. What am I doing wrong here?
<body>
<?php
if(isset(filter_input(INPUT_POST, 'aglogin')) && isset(filter_input(INPUT_POST, 'agname')) && isset(filter_input(INPUT_POST, 'agpass')))
{
echo 'Submitted..';
}
?>
<form method="post">
Username: <input type="text" id="agname" name="agname"/>
<br>
Password: <input type="password" id="agpass" name="agpass"/>
<br>
<input type="submit" id="aglogin" name="aglogin" value="Login">
</form>
</body>
Do not use isset(); because filter_input() returns true if variable is set and return false if it is not set.
<body>
<?php
if(filter_input(INPUT_POST, 'aglogin') && filter_input(INPUT_POST, 'agname') && filter_input(INPUT_POST, 'agpass'))
{
echo 'Submitted..';
}
?>
<form method="post">
Username: <input type="text" id="agname" name="agname"/>
<br>
Password: <input type="password" id="agpass" name="agpass"/>
<br>
<input type="submit" id="aglogin" name="aglogin" value="Login">
</form>
You could try this
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$valid=( array_key_exists('aglogin',$_POST ) && array_key_exists('agname',$_POST ) && array_key_exists('agpass',$_POST ) ) ? true : false;
if( $valid ) echo "Form submitted successfully";
}
?>
Related
I want to allow the user to input some text, then use that text as their session username and then echo a welcome "username" at the top of each page, and then a logout button which will end the session and allow for the user to input a new username
Currently the code will allow the user to input a name, and then a Welcome will appear for that name, but it will be destroyed if the user changes pages, and the textfield to input a name never disappears either - This is the master page in codeigniter
<form action="" method="POST">
<input type="text" name="input_value">
<input type="submit" name ='in' value="LogIn">
</form>
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
$_SESSION['userses'] = '';
}
if(session_status() == PHP_SESSION_ACTIVE && $_SESSION['userses']=='' ){
$_SESSION['userses'] = $_POST['input_value'];
echo "Welcome, ".$_SESSION['userses']."!";
}
else{
echo "Welcome, ".$_SESSION['userses']."!";
}
?>
<form action="<?php session_destroy(); ?>" method="POST">
<input type="submit" name='out' value="LogOff">
</form>
You have to change
if(session_status() == PHP_SESSION_ACTIVE && $_SESSION['userses']=='' )
to
if(isset($_POST['in']) ){
$_SESSION['userses'] = $_POST['input_value'];
}
so when he submits username, this if will catch the post request and then add input to your username
you don't need to check session_status
edit:
and as the comments said, put session_start(); at the beginning of every page
example of code :
<?php session_start();
if(isset($_SESSION['userses'])) echo "Welcome".$_SESSION['userses'];
?>
<form action="" method="POST">
<input type="text" name="input_value">
<input type="submit" name ='in' value="LogIn">
</form>
<?php
if(isset($_POST['in']) ){
$_SESSION['userses'] = $_POST['input_value'];
}
?>
<form action="<?php session_destroy(); ?>" method="POST">
<input type="submit" name='out' value="LogOff">
</form>
You are using codeigniter, why not use session library :
https://www.codeigniter.com/user_guide/libraries/sessions.html
Im trying to validate a single textbox on the same page using PHP but am unable to get the errors to show. When i click submit and there is nothing in the textbox it works by not letting it go to the next page but it does not show the errors. Here is my code.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['IGN'] == ""){
$errors="Please enter a IGN";
}
if (isset($errors)){
echo $errors;
}
}
?>
<form method="post" action="Queston1.php">
<label for="IGN" class="questionText">IGN (In Game Name)</label><br />
<input type="text" name="IGN" /><br />
<input type="submit" name="start" value="start" />
</form>
I have tried it with and without the if (isset($errors)) to see if that was the problem but both times i get the same result.
Can anyone see or know how to fix this?
You should use jQuery or Javascript to validate before submitting the form.
But instead of
if($_SERVER['REQUEST_METHOD'] == 'POST'){
Try
$errors = '';
if(isset($_POST['IGN'])){
if($_POST['IGN'] == ''){
$errors .= 'Please Enter a IGN';
}
}
if($errors!=''){
echo $errors;
}
Try something like this:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['IGN'] == ""){
echo "Please enter a IGN";
}
else {
header('Location: question.php');
}
}
?>
<form method="post" action="">
<label for="IGN" class="questionText">IGN (In Game Name)</label><br />
<input type="text" name="IGN" /><br />
<input type="submit" name="start" value="start" />
</form>
I have this very basic form in my html page.
<form action="post.php" method="post">
Message: <input type="text" name="message" />
<input type="submit" name="submit" value="send">
</form>
and then stores the data onto my database backend.
id also want to submit data via URL bar, such as this.
http://localhost/test.php?message=test&submit=send
but when i try to do above, nothing happens.
how can i achieve such method?
[EDIT]
my post.php
<?php
include_once("connect.php");
if (isset($_GET['submit'])) {
if ($_GET['message'] == "") {
echo " no input, return";
exit();
}
else {
$message = $_GET['message'];
mysql_query("insert into data (message) values ('$message')");
header ('location:index.php');
exit ();
}
}
else {
echo "invalid";
}
?>
use GET method instead of POST
so your code should be like follow:
<form action="post.php" method="GET">
Message: <input type="text" name="message" />
<input type="submit" name="submit" value="send">
</form>
and in the post.php you can get those Query string by using $_GET['message'] or $_REQUEST['message']
use a form GET method. to submit data of a form as a query string.
<form action="test.php" method="GET">
I want to have a php contact form, that when submitted (if no errors), will refresh the same page, and remove the contact form, and replace it with some text e.g. "thank you for contacting us".
Is there a best way to do this?
You could always do something like this:
<?php
$posted = false;
if( isset($_POST['submit']) ) {
/* Process $_POST */
/* Do your things */
/* Set a variable hinting if a post has been done */
$posted = true;
}
?>
<!DOCTYPE html>
<html>
<body>
<?php if( $posted ): ?>
<form method="post">
<input name="foo" />
<input name="bar" />
<input name="car" />
<input name="submit" type="submit" value="Submit" />
</form>
<?php else: ?>
<h1>Thank you for contacting us!</h1>
<?php endif; ?>
</body>
</html>
Do it this way.
<?php
$showform=true; //dispaying the form for the first time
if(isset($_POST['submit'])){
//check for errors
if($errors){
$msg="errors";
$showform=true; // if found errors, setting error msg and displaying the form
}else{
//process the form
$showform=false; //if no errors, setting success msg and hiding the form
$msg="SUccess";
}
}
if(isset($msg)){
//display the success/error msg
echo $msg;
}
if($showform==true){
//your form code
<form method="post">
<input name="foo" />
<input name="bar" />
<input name="car" />
<input name="submit" type="submit" value="Submit" />
</form>
}
?>
I try to login with one PHP file, without any HTML files. So when I'm successfully logged in I want to hide the HTML post button and the textboxes.
Here's my code..:
<?php
$showHTML = true;
if ($showHTML) { ?>
<h1> Bitte einloggen! </h1>
<form action="test2.php" method="POST">
<input name="user" type="test"><br>
<input name="pass" type="password"><br>
<input type="submit" value"Login">
</form>
<?php
}
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if ( $_POST['user'] == "test" && $_POST['pass'] == "a123" ) {
echo "user = test";
$showHTML = false;
}
}
Of course that doesn't hide the HTML code again, because its already executed I think.
Is there any way to hide the HTML output again?
Of course that doens´t hide the HTMLCode again, because its already executed I think.
Correct
Is there any way to hide the HTML output again?
Move the test so it appears before the HTML you (don't) want to output.
Short: put the HTML under the PHP.
Long: Use a decent login system. Use sessions.
You can't use a server side variable to control the already rendered html content.
<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if ( $_POST['user'] == "test" && $_POST['pass'] == "a123" ) {
echo "user = test";
}
} else { ?>
<h1> Bitte einloggen! </h1>
<form action="test2.php" method="POST">
<input name="user" type="test"><br>
<input name="pass" type="password"><br>
<input type="submit" value"Login">
</form>
<?php } ?>