I'm trying to create a landing page and there should be one download button. So when the button was clicked and the form has been completed correctly the user will be redirected to the thank you page but when the form has not been completed correctly I will redirect the user to an error page. How can I realize this?
My code for the form:
<form method="post" action="index.php">
<input type="email" name="iptEmail" placeholder="MaxMustermann#gmail.com" required />
<br /><br />
<img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" />
<img src="img/reloadCaptcha.png" alt="reloadCaptcha.png">
<br /><br />
<input type="text" name="captcha_code" id="iptCaptcha" placeholder="Code" size="10" minlength="6" maxlength="6" required />
<br /><br />
<button name="btnSubmit">DOWNLOAD</button>
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
$securimage = new Securimage();
if(array_key_exists('btnSubmit',$_POST)){
if ($securimage->check($_POST['captcha_code']) == false) {
$_SESSION['status'] = "error";
} else {
if(isset($_POST['btnSubmit'])){
$mailKunde = $_POST['iptEmail'];
$betreff = "";
$message = "";
$absName = "";
$absMail = "";
$replyTo = "";
$anhang = "./data/test.zip";
mail_att($mailKunde, $betreff, $message, $absName, $absMail, $replyTo, $anhang);
$_SESSION['status'] = "thanks";
}
}
}
?>
</form>
My code for the body:
<body>
<?php
if ($_SESSION['status'] == "home") {
include('php/home.php');
} elseif ($_SESSION['status'] == "error") {
include('php/error.php');
} elseif ($_SESSION['status'] == "thanks") {
include('php/thanks.php');
}
?>
</body>
Consider the following,
you may redirect the user to a different page using this line of code:
<?php
if(some_condition...){
header("Location: someotherplace.php");
}
also, if you have already sent html output before this line, you can simply emit a javascript redirect:
<?php
if(some_condition...){
echo("<script>;location.href='someotherplace.php';</script>");
}
Related
How to clear page content and show other content after I validate and submit a form?
<?php
if(isset($_POST['Confirm'])) {
$to = "email#email.com";
$error = 0;
$first_name = $_POST['first_name'];
//Validation things
if(trim($first_name) == '') {$error = 1; $first_namerr = 1;}
$msg ="
First Name:$first_name
-------------------------
";
$sub ="Contact";
$from = "From: Support form";
#mail($to, $sub, $msg, $from);
//How to clear actual content and echo other ?
}
}
?>
<form name="contact" id="contact" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<label for="first_name" class="inner_text"><?php if ($error != 0){ if ($first_namerr == 1) {print("<font style='color: Red;'>");} }?>First Name<?php if ($error != 0){if ($first_namerr == 1) {print("</font>");}} ?></label>
<input id="first_name" name="first_name" size="30" type="text" value="<? echo $first_name; ?>" /><?php if ($error != 0){ if ($first_namerr == 1) {print('<img src="images/error.gif">');} }?>
<input type="submit" id="Confirm" name="Confirm" value="Confirm" />
</form>
I'm beginner in PHP, so please be explicit if you want to give an answer!
You could use an if/else statement to control the content being displayed - e.g:
<?php
if (isset($_POST['Confirm'])) {
// Your mail code
?>
Thank you, your message has been sent! <!-- This content is shown after form submission -->
<?php } else { ?>
<!-- Display your email form -->
<?php }; ?>
Ok, I have a script called profile.php and I would like the user to interact with profile.php "form input" from a different html page or php script such as user_settings.php.
I would like to have the errors ONLY from profile.php to be displayed/echoed/printed on user_settings.php page. I have tried the following
----profile.php---
<?php ob_start(); require_once ("core/init.php"); ?>
<?php require ("includes/overall/header.php"); ?>
<?php
if(has_access($session_user_id, 1) === true){
$message[] = '<p class="admin"> **Admin**</p>';
}
?>
<div id="mainInfo">
<p class="profile"> Hello <?php echo $user_data['first_name'], '!' .output_message($message) ;?> </p><br /><br />
<?php
if(isset($_FILES['image']) === true)
{
if(empty($_FILES['image']['name']) === true)
{
$errors_image[] = 'Please choose a file';
}
else
{
$allowed = array('jpg', 'jpeg', 'gif', 'png');
$file_name = $_FILES['image']['name'];
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES['image']['tmp_name'];
if(in_array($file_extn, $allowed) === true)
{
change_profile_image($session_user_id, $file_temp, $file_extn);
header('Location: profile');
exit();
}
else
{
$errors_image[] = 'Incorrect file type. Allow: ';
echo implode(', ', $allowed);
}
}
}
if(empty($user_data['image']) === false)
{
echo '<p class="photo"><image src="', $user_data['image'],'" alt="', $user_data['first_name'], '\'s Profile Image"></p> <br>';
}
?>
<p class="usr_sttngs"> Update Info!</p><br />
</div>
<?php require ("includes/overall/footer.php"); ?>
--------usr_sttngs.php------------
<?php ob_start();
require_once ("core/init.php");
ini_set('display_errors', 'On'); error_reporting(E_ALL);
protect_page(); ?>
<?php
if(empty($_POST) === false)
{
$required_fields = array('current_passwd','passwd', 'passwd_again');
foreach($_POST as $key => $value)
{
if (empty($value) && in_array($key, $required_fields) === true)
{
$errors[] = '<p class="postad_msg">Fields marked with * are required.</p>';
break 1;
}
}
if (sha1($_POST['current_passwd']) === $user_data['passwd'])
{
if(trim($_POST['passwd']) !== trim($_POST['passwd_again']))
{
$errors[] = '<p class="postad_msg">New passwords DO NOT match!</p>';
}
else if (strlen($_POST['passwd']) < 6)
{
$errors[] = '<p class="postad_msg">New passwords must be at least 6 characters.</p>';
}
}
else
{
$errors[] = '<p class="postad_msg">Your current password is incorrect!</p>';
}
}
?>
<?php require ("includes/overall/header.php"); ?>
<div id="mainInfo">
<br /> <br /><p3>Change Password!</p3> <br /> <br />
<?php
if(isset($_GET['success']) && empty($_GET['success']))
{
$message[] = '<p class="postad_msg">Your password has been changed successfully.</p>';
}
if(empty($_POST) === false && empty($errors) === true)
{
change_passwd($session_user_id, $_POST['passwd'] );
//redirect
header('Location: usr_sttngs?success');
//exit
exit();
}
else if(empty($errors) === false)
{
output_errors($errors);
}
?>
<form method="post" action="usr_sttngs">
<fieldset>
<label for="currentpasswd">Current password* </label>
<input placeholder="Type your current password" type="password" name="current_passwd" size="30" maxlength="50" value="<?php echo htmlentities($current_passwd); ?>" /><br /><br />
<label for="last_name">New Password* </label>
<input placeholder="New Password" type="password" name="passwd" size="30" maxlength="50" value="<?php echo htmlentities($new_passwd); ?>" /><br /><br />
<label for="email">New Password Again* </label>
<input placeholder="Retype New Password" type="password" name="passwd_again" size="30" maxlength="50" value="<?php echo htmlentities($new_passwd_again); ?>" />
</fieldset>
<fieldset class="center">
<input class="submit" type="submit" name="submit" value="Change Password" />
<?php echo output_errors($errors); ?>
<?php echo output_message($message);?>
</fieldset>
</form>
<p4>Change profile photo!</p4> <br /> <br />
<form action="profile" method="post" enctype="multipart/form-data">
<fieldset class="center">
<input type="file" name="image"> <input type="submit">
<?php echo output_errors($errors_image);
require_once ('$_SERVER["DOCUMENT_ROOT"]\profile.php');
?>
</fieldset>
</form>
<br /><br /><br /><p4>Upload your resume!</p4> <br />
<form action="profile" method="post" enctype="multipart/form-data">
<fieldset class="center">
<input type="file" name="resume"> <input type="submit">
</fieldset>
</form>
</div>
<?php require ("includes/overall/footer.php"); ?>
I tried something I read on the following post Want to echo a php variable from another page but I did not get the desired results. So I tried to include the required_once 'profile.php' ; at the very top of the user_setting.php file and that just gives me a blank page. Any help would be greatly appreciated. Thank you!
Require or Require_once must be called before any HTML is displayed.
Try placing your require_once at the beginning of the file.
EDITED
Try the following test code to see if you can get it working.
The profile.php should contain the following:
<?php
echo "profile.php is displaying correctly";
?>
The user_settings.php should contain the following:
<?php
require_once("profile.php");
?>
See if you can get this basic code to work. Post your results
I get the bellow page when I try the code above:
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>
How do I make error show on top of form so that if $user->success == true, it wont show my form then. Removing that last else would help, but then form shows after success. One way is to redirect that. Maybe tehre
if (isset($_POST["submit"]))
{
if ($_POST["formid"] == $_SESSION["formid"])
{
$_SESSION["formid"] = '';
$User->signin($_POST['username'], $_POST['password']);
}
else
$User->CheckUser();
if ($User->success == true) {
include ('in.php');
}
if ($User->error)
echo "<p>" . $User->error . "</p>";
else
echo 'Don\'t process form';
$_SESSION["formid"] = md5(rand(0,10000000));
} else {
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
Username:
<input id="username" name="username" type="text" /><br />
Password:
<input id="password" name="password" type="password" /><br />
<input type="hidden" name="formid" value="<?php echo $_SESSION["formid"]; ?>" />
<input type="submit" name="submit" />
<br />
Register
</form>
<?php }?>
Perhaps the simplest approach is to just create a variable $show_form to use to determine whether form is to be shown,
$show_form = true;
if(isset($_POST['submit'])) {
// do your form processing here.
// If you decide everything is good and you don't want to show the form,
// just add this line:
$show_form = false;
} // don't use else here
if (true === $show_form) {
?>
<form>...</form>
<?
}
?>
add this code before your form tag
<?php if (isset($User->error) AND $User->error)?>
<p><?php echo $User->error?></p>
<?php?>
I am creating a simple form that a user submits and email. I am trying to pose an error if the form is blank or it's not a valid email and if successful, then reload the page with a success message.
When I submit blank it reloads the page without an error, and if I enter anything in ( valid or invalid email ) it reloads the page white, despite the form action being correct. I've been stuck on this and need help. Thanks.
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/system/init.php');
if(isset($_POST['submit'])) {
$email = $_POST['email'];
if(empty($_POST['email']) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Please enter a valid email";
}else{
$success = true;
mysql_query("INSERT INTO survey
(email) VALUES('".$_POST['email']."' ) ")
or die(mysql_error());
}
}
?>
<div class="email-survey">
<?php if(isset($success)) { ?>
<div class="success">Thank You!</div>
<?php } ?>
<?php if(isset($error)) { ?>
<div class="error">
<?php echo $error; ?>
</div>
<?php } ?>
<form name="settings" action="/survey-confirm.php" method="post">
<input type="text" name="email" /> <br />
<input type="submit" name="submit" value="submit" />
</form>
</div>
<?php
function control($type, $text)
{
echo '<div class="'.$type.'">'.$text.'</div>';
}
require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/system/init.php');
if(isset($_POST['submit'])) {
$email = $_POST['email'];
if(empty($_POST['email']) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
control('error', 'Type valid mail!');
}else{
control('success', 'All done!');
mysql_query("INSERT INTO survey
(email) VALUES('".$_POST['email']."' ) ")
or die(mysql_error());
}
}
else
{echo 'echo '<form name="settings" action="/survey-confirm.php" method="post">
<input type="text" name="email" /> <br />
<input type="submit" name="submit" value="submit" />
</form>
</div>';}
?>
This is small function named control, you can call this and put your custom div name and text to show user.
control('THIS IS DIV NAME','THIS IS MESSAGE FOR USER')