I am new to the web developing world.Please bare w/ me for having some mistakes and insufficient knowledge.
I'm trying to study the validation process of a users input. Below is my Code:
<?php
if($_POST['formSubmit'] == "Search")
{
$errorMessage = "";
if(empty($_POST['formName']))
{
$errorMessage .= "<li>No Input</li>";
}
$varName = $_POST['formName'];
if(!empty($errorMessage))
{
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
}
?>
<form action="index.php" method="post">
<input type="text" name="formName" value="<?=$varName;?>">
<input type="Submit" name="formSubmit" value=" Search">
</form>
What I think should happen is when the user click the search button without inputting anything an error message will pop-up, but I don't understand why its not responding or echoing the error message, i've check the names and values but Alas, Appreciate all the help/suggestions you could give tnx.
Thanks for the reply everyone, got it into working ^_^
Your $_POST['formSubmit'] doesn't contain Search. It contains Search (with space in front of it).
Because of that PHP will never validate your form.
This works:
<?php
if($_POST['formSubmit'] == "Search")
{
$errorMessage = "";
if(empty($_POST['formName']))
{
$errorMessage .= "<li>No Input</li>";
}
$varName = $_POST['formName'];
if(!empty($errorMessage))
{
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
}
?>
<form action="index.php" method="post">
<input type="text" name="formName" value="<?=$varName;?>">
<input type="Submit" name="formSubmit" value="Search">
</form>
you need to use isset() function
<?php
if(isset($_POST['formSubmit']))
{
$errorMessage = "";
if(empty($_POST['formName']))
{
echo $errorMessage .= "No Input";
}
else
{
$varName = $_POST['formName'];
}
}
?>
<form action="index.php" method="post">
<input type="text" name="formName" value="<?=$varName;?>">
<input type="Submit" name="formSubmit" value="Search">
</form>
You giving wrong name to your submit button .
<input type="Submit" name="formSubmit" value=" Search">
it should be
<input type="Submit" name="formSubmit" value="Search">
Change this line
if($_POST['formSubmit'] == "Search")
to
if(isset($_POST['formSubmit']))
I am making some corrections. Please use this.
<?php
if($_POST['formSubmit'] == "Search") {
$errorMessage = "";
if(empty($_POST['formName'])) {
$errorMessage .= "<li>No Input</li>";
} else {
$varName = $_POST['formName'];
}
if(!empty($errorMessage)) {
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
}
?>
<form action="index.php" method="post">
<input type="text" name="formName" value="<?=$varName;?>">
<input type="Submit" name="formSubmit" value="Search">
</form>
Corrected the condition if($_POST['formSubmit'] == "Search")
IF $_POST['formName'] is empty then no need for assigning so put it inside else condition.
Related
I'm going to keep it short and simple. I'm writing a really basic code in php which adds content to mysql db and I've run into an issue with editing. This is my form:
if($idExists)
{
Print '
<form action="editIt.php" method="POST">
<div name="id"> '. $id . '</div>
Enter new detail: <input type="text" name="details"/><br/>
public post? <input type="checkbox" name="public[]" value="yes"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}
And this is my editIt.php
//session start and stuff
if(filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING) == "POST")
{
echo "<script type='text/javascript'>alert('EDITIT!');</script>";
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("WebSiteDB") or die ("Cannot connect to database");
$id = $_POST['id'];
$details = mysql_real_escape_string($_POST['details']);
$time = strftime("%X");
$date = strftime("%B %d, %Y");
$isPublic = 'no';
foreach($_POST['public'] as $eachCheck)
{
if($eachCheck != NULL)
$isPublic = "yes";
}
mysql_query("UPDATE list SET details='$details', dateEdited='$date', timeEdited= '$time', public='$isPublic' WHERE id='$id'");
header("location: home.php");
}
I can't really find an issue with this code (which is not really strange, I'm a newbie at web stuff) and yet it just goes to home.php and does not change data in DB. Please, help me jump this ledge, so I can go on with my life.
I think, the problem is in this line $id = $_POST['id'];. On form submit, the input field value will only be submitted, not the DIV value.
So, please change from :
if($idExists)
{
Print '
<form action="editIt.php" method="POST">
<div name="id"> '. $id . '</div>
Enter new detail: <input type="text" name="details"/><br/>
public post? <input type="checkbox" name="public[]" value="yes"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}
To :
if($idExists)
{
Print '
<form action="editIt.php" method="POST">
<input type="hidden" name="id" value="' . $id . '">
Enter new detail: <input type="text" name="details"/><br/>
public post? <input type="checkbox" name="public[]" value="yes"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}
How do i prevent the error-message from showing before the 'submit'-button is clicked?
For some reason the content of 'else'-statement is visible on pageload - the message "NO" should only appear after typing something else than 'a' in this.. what am i doing wrong?
<form method="POST">
<input type="text" class="textfield" id="cursor" name="pass">
<input type="submit" class="button" name="submit" value="OK">
</form>
<?php
$pass = $_POST['pass'];
if($pass == 'a') {
echo "YES";
}
else{
echo "NO";
}
?>
Add this PHP code:
<?php
if(isset($_POST['pass'])) {
// your check here
}
?>
Test to see if the submit button is in the submitted data.
if (isset( $_POST['submit'] )) {
<form method="POST">
<input type="text" class="textfield" id="cursor" name="pass">
<input type="submit" class="button" name="submit" value="OK">
</form>
<?php
$pass = $_POST['pass'];
//You need to check if the form is submitted
if($_POST['submit']){
if(isset($pass) && !empty($pass) && $pass == 'a') {
echo "YES";
}
else{
echo "NO";
}
?>
I have following login form (login.php) in which I am asking for username and password.
<form action="processlogin.php" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
Following is the code snippet from my processlogin.php file
if(!$_POST["username"] || !$_POST["password"])
{
$msg = "You left one or more of the required fields.";
echo $msg;
//header("Location:http://localhost/login.php");
}
This code checks whether all the mandatory fields are filled on not. If not, it shows the error message.
Till now everything is fine.
My problem is that, error message is shown in plain white page. I want to show it above the login form in login.php file. How should I change my code to get
my functionality.
I would prefer Jquery Validation or Ajax based Authentication. But still you can do it this way:
Put your Error Message in Session like this :
$_SESSION['Error'] = "You left one or more of the required fields.";
Than simple show it like this:
if( isset($_SESSION['Error']) )
{
echo $_SESSION['Error'];
unset($_SESSION['Error']);
}
In this case you can assign multiple messages in different Operations.
header("Location:http://localhost/login.php?x=1")
In the login.php
if(isset($_GET('x'))){
//your html for error message
}
Hope it helps you,
In processlogin.php,
if(!$_POST["username"] || !$_POST["password"])
{
$msg = "You left one or more of the required fields.";
$msgEncoded = base64_encode($msg);
header("location:login.php?msg=".$msgEncoded);
}
in login.php file,
$msg = base64_decode($_GET['msg']);
if(isset($_GET['msg'])){
if($msg!=""){
echo $msg;
}
}
You can display the message in table or span above the form.
<span>
<?php if(isset($_REQUEST[$msg]))
echo $msg;
?>
</span>
<form>
</form>
And also don't echo $msg in the form's action page.
Try this:
html:
<form action="processlogin.php" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
<span>
<?php if(isset($_GET['msg']))
echo $_GET['msg'];
?>
</span>
</form>
php:
if(!$_POST["username"] || !$_POST["password"])
{
$msg = "You left one or more of the required fields.";
header("Location:http://localhost/login.php?msg=$msg");
}
Use only one page (your login.php) to display the form and also to validate its data if sent. So you don't need any $_SESSION variables and you have all in one and the same file which belongs together.
<?php
$msg = null;
if(isset($_GET['send'])) {
if(!$_POST["username"] || !$_POST["password"]){
$msg = "You left one or more of the required fields.";
//header("Location:http://localhost/login.php");
}
}
?>
<?php echo ($msg !== null)?'<p>ERROR: ' . $msg . '</p>':null; ?>
<form action="?send" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
use these functions:
<?php
session_start();
define(FLASH_PREFIX,'Flash_')
function set_flash($key,$val){
$_SESSION[FLASH_PREFIX.$key]=$val;
}
function is_flash($key){
return array_key_exits(FLASH_PREFIX.$key,$_SESSION);
}
function get_flash($key){
return $_SESSION[FLASH_PREFIX.$key];
}
function pop_flash($key){
$ret=$_SESSION[FLASH_PREFIX.$key];
unset($_SESSION[FLASH_PREFIX.$key]);
return $ret;
}
?>
And when you want to send a message to another page use
set_flash('err_msg','one field is empty');
header('location: another.php');
exit();
another.php
<html>
.
.
.
<body>
<?php if(is_flash('err_msg')){?>
<span class="err_msg"><?php echo pop_flash('err_msg'); ?></span>
<?php } ?>
.
.
.
</body></html>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(!$_POST["username"] || !$_POST["password"])
{
$msg = "You left one or more of the required fields.";
echo $msg;
//header("Location:http://localhost/login.php");
}
}
?>
<form action="<?php echo $PHP_SELF;?>" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
I´m trying to make a form that works. I´m using codeigniter, the view has this form:
<form class="renuncia_form" action="/formulario/send_form" method="post">
<p>
<label for="nombre">Nombre y apellidos:</label>
<input name="nombre" type="text" id="renuncia_nombre">
<br>
<label for="participe">Nº Partícipe: </label>
<input name="participe" type="text" id="renuncia_participe">
<br>
<label for="nombre_fondo">Nombre del Fondo de Inversión o SICAV: </label>
<input name="nombre_fondo" type="text" id="renuncia_fondo">
<br>
<label for="email">Direccion de correo electrónico: </label>
<input name="email" type="text" id="renuncia_email">
<br>
<input type="submit" value="Enviar" class="renuncia_submit" name="enviar">
</p>
</form>
And the controller has this php:
public function send_form(){
if($_POST['submit'] == "Submit")
{
$errorMessage = "";
if(empty($_POST['nombre']))
{
$errorMessage .= "<li>You forgot to enter your name</li>";
}
if(empty($_POST['participe']))
{
$errorMessage .= "<li></li>";
}
$varMovie = $_POST['nombre'];
$varName = $_POST['participe'];
if(empty($errorMessage))
{
$fs = fopen("mydata.csv","a");
fwrite($fs,$varName . ", " . $varMovie . "\n");
fclose($fs);
header("Location: thankyou.html");
exit;
}
}
}
I don´t know if I´m doing correctly the form. I just want to work it as a normal form action, that you click on submit an it takes you to a new page saying "Thanks for your email", no AJAX, just that.
Can anybody help me out with this one?
Edit: Also where do I put the recipient e-mail?
if($_POST['submit'] == "Submit")
should be
if(isset($_POST['enviar']))
As submit button is not having name as submit, instead it is name='enviar'
try this
if(isset($_POST["enviar"]) && $_POST["enviar"] == "Enviar")
You've got
if($_POST['submit'] == "Submit")
and then everything else if the condition is satisfied. It is never as there isn't such field. Just remove it and it'll be fine.
As for mail, you should access it via $_POST['email']. If you want to send an email, take a look here or here.
I think this is because you are translating your form anyway you used
if($_POST['submit'] == "Submit")
and you used in form
<input type="submit" value="Enviar" class="renuncia_submit" name="enviar">
your actual submit button name is enviar and not submit. Since this is a button I would reccomend to check isset()
if(isset($_POST['enviar']))
I'm not sure but the problem is the action attribute.
why don't use From helper to create this form?. Anyway if you don't what or can't use it change the action to something like http://localhost/your_project/index.php/formulario/send_form.
read about base_url here http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
Besides if($_POST['submit'] == "Submit") is wrong. It must be
if(isset($_POST['submit']) && ($_POST['submit'] === "Enviar"))
if (isset($_POST['enviar']))
This should work
Use:
public function send_form(){
if(array_key_exists('submit',$_POST))
{
$errorMessage = "";
if(empty($_POST['nombre']))
{
$errorMessage .= "<li>You forgot to enter your name</li>";
}
if(empty($_POST['participe']))
{
$errorMessage .= "<li></li>";
}
$varMovie = $_POST['nombre'];
$varName = $_POST['participe'];
if(empty($errorMessage))
{
$fs = fopen("mydata.csv","a");
fwrite($fs,$varName . ", " . $varMovie . "\n");
fclose($fs);
header("Location: thankyou.html");
exit;
}
}
}
I have the following code but unfortunately $error appears as soon as the page loads, before clicking on submit. What am I doing wrong?
(filename= form.php)
<?php
$error="";
if (isset($_POST['submit']) && $_POST['submit'] == "Submit") {
$name = $_POST['name'];
$email = $_POST['email'];
if($name=="" || $email==""){
$error .= "Error: all fields are required";
}
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email)) {
$error .= "<br/> Error: Invalid email address";
}
if (isset($error) && ($error!=="")) {
echo $error;
}
elseif (empty($error)){
// this works no need to write here... send form.
}
}
echo<<<_END
<html><head><body>
<form method='post' action='form.php'>
<p>Name</p> <input type="text" name="name" value='$name'>
<p>Email</p> <input type="text" name="email" value='$email'>
<input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value="Clear">
</form>
</body>
</html>
_END;
?>
if (isset($error) && (!$error=="")) {
echo $error;
}
if $error is set and $error equals nothing echo $error?
$error is being set when you intialize it to "" in the first line and it is equal to ""
Just noticed the ! in the second part of the if. Do what the person below me said.
Replace (!$error=="")) with ($error!=="")).