Validation of form in another php script? - php

I'd like to know if it is possible to manage error if in the form, action is set to another script.
ajoutDevis.php
<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
<label for="client">Client</label>
<input type='text' id='client' name='client'>
<span class="error">* <?php echo $clientErr;?></span>
</FORM>
ajoutDevisScript.php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST['client'])) {
$clientErr = "ERROR";
} else $client = $_POST['client'];
So, is it possible to display my error in the span if the input is empty ? Or there is no way doing it with an other script as action ?
Thanks!

session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST['client'])) {
$clientErr = "ERROR";
$_SESSION['error] = $clentErr;
header('location:ajoutDevis.php');
} else $client = $_POST['client'];
In your ajoutDevis.php file
session_start();
$clientErr = ($_SESSION['error']!='')?$_SESSION['error']:'';
<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
<label for="client">Client</label>
<input type='text' id='client' name='client'>
<span class="error">* <?php echo $clientErr;?></span>
</FORM>

You need to do the following:
Add the form code as well on the ajoutDevisScript.php page at the bottom of page.
<?php if(!empty($clientErr)) { ?>
<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
<label for="client">Client</label>
<input type='text' id='client' name='client'>
<span class="error">* <?php echo $clientErr;?></span>
</FORM>
<?php } ?>
This will check if there is any error in error variable it will display the form on same page with error.

Yes, it is possibile with $_SESSION variables to store values during the session and header() function to redirect user from the second script back to the first.
Supposing both files are in the root directory, ajoutDevis.php:
<?php
session_start();
?>
<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
<label for="client">Client</label>
<input type='text' id='client' name='client'>
<?php if (isset($_SESSION['clientErr'])) : ?>
<span class="error">* <?php echo $_SESSION['clientErr'] ?></span>
<?php endif; ?>
</FORM>
and ajoutDevisScript.php:
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST['client'])) {
$_SESSION['clientErr'] = "ERROR";
header('Location: ajoutDevis.php');
} else {
$client = $_POST['client'];
}
}
?>
I would suggest you to print the span only if you need to. I would also suggest you to check your brackets to avoid PHP errors.

Related

issue on updating data in php mysql

I was trying to create an update system , but I don't know why my code is not working . My code are :
<?php
include "dbconnection.php";
if(isset($_POST['submit'])){
$residency_status = $_POST['residency_status'];
$query="UPDATE profile SET
residential_status='$residency_status'
WHERE id = '".user_id."'
";
}
header('location:profile.php');
?>
I am adding some Code from profile.php page :
<?php
session_start();
include_once ('dbconnection.php');
if(!isset($_SESSION['logged_in'])){
header("Location: login.php");
die();
}
$usrname=$_SESSION['username'];
$pasword=$_SESSION['password'];
$user_id= get_user_id($usrname, $pasword);
while($us_id= $user_id->fetch_assoc()) :
$collected_id=$us_id['id'];
$resi_status=$us_id['residential_status'];
endwhile;
?>
The code for the form is : I am trying to keep the code short to avoid
<form class="form-group row" action="get_update.php" method="POST"
enctype="multipart/form-data">
<h4>Residential Status </h4>
<div class="form-group">
<input type="radio" name="residency_status" value="yes" class=""
id=""
<?php
if ($resi_status == "yes") { ?> checked <?php }
?>
>yes
<input type="radio" name="residency_status" value="No" class="" id=""
<?php
if ($resi_status == "no" || $resi_status == "") { ?> checked
<?php } ?> >no
</div>
<br>
Thank you .
You are missing out on mysqli_query.Your code should be as
<?php
include "dbconnection.php";
if(isset($_POST['submit'])){
$residency_status = $_POST['residency_status'];
$query="UPDATE profile SET
residential_status='$residency_status'
WHERE id = '".user_id."'";
mysqli_query($conn,$query); // $conn must be as per your `mysqli_connect` variable
}
header('location:profile.php');
?>

If statement in a form PHP

I'm working on a code which has a form. The answer on the form needs to decide to which page it should go, but it doesn't work.
<html>
<body>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
<?php
if($_GET["graden"]>=28){
$action = "Koelbox.php";
}
else{
$action = "scrabble.php";
};
?>
</form>
</body>
</html> ```
You have to declare and initialize the variable before using it.
<?php
if($_GET["graden"]>=28)
{
$action = "Koelbox.php";
}
else
{
$action = "scrabble.php";
}
?>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
</form>
First check the GET parameter, then set your $action variable accordingly and then use it in your echo.
I just initialized the variable before the form, but as far as I see the IF statement doesn't see the input in the form.
<?php
if($_GET["graden"]>=28)
{
$action = "Koelbox.php";
}
else
{
$action = "scrabble.php";
}
?>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
</form>

PHP - POST is not catching any value, where GET works well

SORRY FRIENDS FOR THIS QUESTION, I TESTED THE SAME CODE IN REAL TIME SERVER. IT WORKED FINE, BUT WHEN THE CODE RETURN IN PHPSTORM IT NOT WORKED.
(don't know exactly what the reason )
First.php
<form action="second.php" method="post" >
Name: <input type="text" name="name" >
<input type="submit">
</form>
second.php
<?php
if(isset($_POST["name"])){
$username = $_POST["name"];
echo $username;
}else
{
echo "null value";
}
?>
Here i am getting null value.
dont know what the reson.
but i simple testing this will GET (where method="get")
and
second.php is
<?php
echo $_GET["name"];
?>
it worked well.
Try this:
HTML
<form action="second.php" method="post" >
Name: <input type="text" name="name" >
<input type="submit">
PHP
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$username = $_POST["name"];
echo $username;
}else
{
echo "null value";
}
?>

Keep text in text field after submit

I'm building a form, and I want that all the inserted values will be kept, in case of form submit failure. This is my code:
<?php
$error = "";
$name = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
// Verify $_POST['name'] greater than 4 chars
if ( strlen($name) < 4 ){
$error= 'Name too short!';
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
<input type="text" placeholder="Name" id="name" name="name" value=""/>
<input type="submit" value="submit"/>
</form>
<?php
echo "<h2>Input:</h2>";
echo $name;
if($error) {
// No Update AND refresh page with $name in text field
echo "<br/>" . $error;
} else {
// Update name in DB
}
?>
</body>
</html>
I would like that name field keeps the inserted input text, after submit. I tried to do with php code in input value but doesn't work. Any ideas?
Solved. This is the solution that I was looking for.
I added in value tag of input the following:
<?php if (isset($_POST['name'])) echo $_POST['name']; ?>
Therefore input field would look like:
<input type="text" placeholder="Name" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>"/>
Thanks for your responses, helped me.
<?php
$error = "";
$name = isset($_POST["name"])?$_POST["name"]:""; //Added condition
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
// Verify $_POST['name'] greater than 4 chars
if ( strlen($name) < 4 ){
$error= 'Name too short!';
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
<input type="text" placeholder="Name" id="name" name="name" value="<?php echo $name; ?>"/>
<input type="submit" value="submit"/>
</form>
<?php
echo "<h2>Input:</h2>";
echo $name;
if($error) {
// No Update AND refresh page with $name in text field
echo "<br/>" . $error;
} else {
// Update name in DB
}
?>
</body>
</html>
You can just echo $_POST['name'] in the value attribute of the input.
Make sure you check POST values to avoid XSS.
I also put up the update DB function, as you don't need to show the form to the user if the name in longer the 4 chars!
<?php
$error = "";
$name = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['name'])){ //change name content only if the post value is set!
$name = filter_input (INPUT_POST, 'name', FILTER_SANITIZE_STRING); //filter value
}
// Verify $_POST['name'] greater than 4 chars
if ( strlen($name) < 4 ){
$error= 'Name too short!';
} else {
// Update name in DB
// Redirect
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
<input type="text" placeholder="Name" id="name" name="name" value="<?php echo $name; ?>"/>
<input type="submit" value="submit"/>
</form>
<?php
echo "<h2>Input:</h2>";
echo $name;
if($error) {
// No Update AND refresh page with $name in text field
echo "<br/>" . $error;
};
?>
</body>
</html>
If you want to keep all values/inputs for further use you could achieve that with php session values.
Besides - you should use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF']
Mars

Simple Validator in PHP not working

I created a little form validator with PHP and having some problems with it.
MY VIEW FILE is here :
<form action="" method="post">
<?php if( isset($status) ) : ?>
<p class="notice"><?php echo $status; ?> </p>
<?php endif; ?>
<ul>
<li>
<label for="name">Your Name : </label>
<input type="text" name="name">
</li>
<li>
<label for="email">Your Email : </label>
<input type="text" name="email">
</li>
<li>
<input type="submit" value="Sign Up">
</li>
</ul>
</form>
and here's my little controller :
<?php
require 'index.tmpl.php';
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
if (empty($name) || empty($email)) {
$status = "Please provide a name and a valid email address";
}
echo $name;
}
?>
Now what happens is that , when I open up the page and leave the form fields blank and submit it ,it just reloads ,does not echo anything.
You want to echo $status, not $name.
How about moving the require line to below the if?
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
if (empty($name) || empty($email)) {
$status = "Please provide a name and a valid email address";
}
echo $name;
}
require 'index.tmpl.php';
?>
The <form action="" points to the location where the form will be submitted. If blank, it submits the form back to itself.
<form action="yourLittleController.php" method="POST">
Edited with more info:
in php, post is not POST. Example here: https://eval.in/89002
make sure you have
method="POST">
and not
method="POST">
you should mention your second file name in your view file's form's action attribute like action = "controller.php"

Categories