Simple PHP form not working - php

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;
}
}
}

Related

Clear form fields after a successful submit

well im working on a small html form.
<form class="contact" action="" method="POST">
<label>Name : </label><input type="text" name="name" value="<? echo $name; ?>"/>
<p class="middle"><label>Comment : </label><textarea name="message"></textarea><? echo $message; ?></p>
<label class="captcha"><img src="captcha.php" style="line-height: 30px;"></label><input type="text" name="code"/>
<input type="submit" class="csubmit" value="Now !" name="get"/>
</form>
and this is the php code:
<?php
if (isset($_POST['get'])) {
$error = "";
if (!empty($_POST['name'])) {
$name = $_POST['name'];
} else {
$error .= "no name. <br />";
}
if (!empty($_POST['message'])) {
$message = $_POST['message'];
} else {
$error .= "no message <br />";
}
if(($_POST['code']) == $_SESSION['code']) {
$code = $_POST['code'];
} else {
$error .= "wrong captcha <br />";
}
if (!empty($error)) {
echo '<p class="error">Error :<br/>' . $error . '</p>';
} elseif (!empty($success)) {
echo $success;
}
if (empty($error)) {
$message = mysql_real_escape_string($message);
$name = mysql_real_escape_string($name);
$id = mysql_real_escape_string($_GET['id']);
$date = date("Y-m-d H:i:s");
mysql_query("INSERT INTO comments(id, name, comment, time,approved)VALUES('$id', '$name', '$message', '$date', '0')");
echo "thank you";
}
}
?>
As you can see i user $message and $name to keep informations after a submit with wrong captcha code, but the problem is that i want to clear those fields after a submit with correct informations. Can you please tell me how can i clear form fields after a succesfull submit ?
You can use .reset() on your form.
$("#form")[0].reset();
You could follow that with Javascript too
document.getElementById('form').reset();
Or, if successful, redirect the user back to your contact page:
header("Location: contact.php"); // redirect back to your contact form
exit;
EDIT
<input type="submit" class="csubmit" value="Now !" name="get" onClick="clearform();" />
function clearform()
{
document.getElementById("name").value=""; //don't forget to set the textbox ID
document.getElementById("message").value=""; //don't forget to set the textbox ID
document.getElementById("code").value=""; //don't forget to set the textbox ID
}
Also use:
required="required"
so people will be required to fill out the input fields :)
Which by the way is the prefered method. If you keep the user in a page that was reached through a POST method, if he refreshes the page the form will be submitted again.

Php Input Validation not responding

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.

How to show error messages in HTML page in PHP?

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>

Error appears before submit in php form

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!=="")).

PHP form error issue

There is probably a simple solution for this but i'm not very proficient in php! Basically I want to submit a form and the user be returned with a thank you overlay image without refresh. I've managed to get this to work BUT now the form validating isn't working properly...
I need to make my overlay only appear after the form validating is successful, if it isn't successful I need to display the error instead of the thank you overlay...
I know I could use ajax for this form but I don't want to rely on javascript!
At the minute the validating is working, but the image is being overlayed on top of it...
This is php code:
<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";
if(empty($_POST['formName']))
{
$errorMessage .= "<li>You forgot to enter your name</li>";
}
if(empty($_POST['formTown']))
{
$errorMessage .= "<li>You forgot to enter your town</li>";
}
$varName = $_POST['formName'];
$varTown = $_POST['formTown'];
$varAge = $_POST['formAge'];
$varEmail = $_POST['formEmail'];
$varOne = $_POST['hidden-one'];
$varTwo = $_POST['hidden-two'];
$varThree = $_POST['hidden-three'];
$varFour = $_POST['hidden-four'];
$varFive = $_POST['hidden-five'];
if(empty($errorMessage))
{
$fs = fopen("mydata.csv","a");
fwrite($fs,"\n" . $varName . ", " . $varTown . ", " . $varAge . ", " . $varEmail . ", " . $varOne . $varTwo . $varThree . $varFour . $varFive);
fclose($fs);
}
}
?>
This is my html (with the php code):
<?php
if (isset($_POST['formSubmit'])) {
print "<div class=\"thank-you\"><a href='enter.php'><img src='images/thankyou-overlay.png'/></a></div>\n";
}
?>
<div id="mainContainer">
<p>Just complete your entry details below.</p>
<?php
if(!empty($errorMessage)) {
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" target="_self">
<div class="inputContainer">
<label class="text" name="name">Full Name:</label>
<input type="text" class="box" name="formName" value="<?=$varName;?>">
</div>
... more html inputs...
</form>
Whatever you are going to do, you have to use Javascript. You can choose AJAX either using an iframe where you direct your post to, and reading it in a javascript to check status of posting.
Edit:
Like this you can post it:
<form action="do_stuff.aspx" method="post" target="my_iframe">
<input type="submit" value="Do Stuff!" />
</form>
<!-- when the form is submitted, the server response will appear in this iframe -->
<iframe name="my_iframe" src="not_submitted_yet.aspx"></iframe>
So after the post, you have to read status from this iframe, (in other words de HTML output from it).
First , i am having difficulty comprehending what you are trying to do : But still i can point out a few things that have better alternates ;
You should put this code
if($_POST['formSubmit'] == "Submit")
{
...
}
above the form for the functionality you want
and also the above if should have an else to show the form when there are errors.
like
else
{
---form---
}
try this and c if it helps

Categories