retaining input value after refresh in php


retaining input value after refresh in php



I am trying to retain the value of the input if the form refresh with the earlier input the user filled in. But every time i make error and the page refresh still comes back empty.


if ( isset($_POST['make']) && isset($_POST['model']) && isset($_POST['year']) && isset($_POST['mileage'])){
if(strlen($_POST['make']) < 1 || strlen($_POST['model']) < 1 || strlen($_POST['year']) < 1 || strlen($_POST['mileage']) < 1){
$_SESSION["error"] = "All values are required";
header('Location: add.php');
return;
}



from here i tried to pass the post make value to the session as a parameter but, it didn't work either!


elseif (strlen($_POST['make']) < 1 ){
$_SESSION["error"] = "Make is required.";
$_SESSION['make'] = $_POST['make'];
header("Location: add.php?make=".$_POST['make']);
return;
}



Then i pass it as part of the url on refresh as and used a global get value still didn't work either!


else if (strlen($_POST['model']) < 1 ){
$_SESSION["error"] = "Model is required.";
header("Location: add.php?model=".$_POST['model']);
return;
}
else if(strlen($_POST['year']) < 1 ){
$_SESSION["error"] = "Year is required.";
header("Location: add.php?year=".$_POST['year']);
return;
}
else if( strlen($_POST['mileage']) < 1){
$_SESSION["error"] = "Mileage is required.";
header("Location: add.php?mileage=".$_POST['mileage']);
return;
}
else if (!is_numeric($_POST['year']) || !is_numeric($_POST['mileage'])){
$_SESSION["error"] = "Mileage and year must be numeric";
header('Location: add.php');
return;
}else {
$sql = "INSERT INTO autos (make, model, year, mileage)
VALUES (:make, :model, :year, :mileage)";
// // echo("<pre>n".$sql."n</pre>n");
// $opt = "Record Inserted";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':make' => $_POST['make'],
':model' => $_POST['model'],
':year' => $_POST['year'],
':mileage' => $_POST['mileage']));
}
$_SESSION["success"] = "added";
header('Location: view.php');
return;
}



Then i changed it here to listen for the submit and when the submit is set all value of post should be passed as session value but, still come empty.


if (isset($_POST['submit'])) {
$_SESSION['make'] = $_POST['make'];
$_SESSION['model'] = $_POST['model'];
$_SESSION['year'] = $_POST['year'];
$_SESSION['mileage'] = $_POST['mileage'];
}
?>



<?php
if ( isset($_SESSION["error"]) ) {
echo('<p style="color:red">'.$_SESSION["error"]."</p>n");
unset($_SESSION["error"]);
}
?>
<br>
<p>Adding A New Auto</p>
<form id="autoAdd" method="post">
<p>Make:



I then check if the get is set and try to pass it to the value and if not it should output the input without value.


<?php
if(isset($_GET['make'])) {
// $make = $_GET['make'];
$make = htmlentities($_SESSION['make']);
echo '<input type="text" name="make" size="40" value="'.$make.'">';
}
else{
echo '<input type="text" name="make" size="40">';
}
?>
</p>
<p>Model:



I then check if the get is set and try to pass it to the value and if not it should output the input without value.


<?php
if(isset($_GET['model'])) {
$model = $_GET['model'];
echo '<input type="text" name="model" size="40" value="'.$model.'">';
}
else{
echo '<input type="text" name="model" size="40">';
}
?>
</p>
<p>Year:



I then check if the get is set and try to pass it to the value and if not it should output the input without value.


<?php
if(isset($_GET['year'])) {
$year = $_GET['year'];
echo '<input type="text" name="year" size="40" value="'.$year.'">';
}
else{
echo '<input type="text" name="year" size="40">';
}
?>
</p>
<p>Mileage:



I then check if the get is set and try to pass it to the value and if not it should output the input without value.


<?php
if(isset($_GET['mileage'])) {
$mileage = $_GET['mileage'];
echo '<input type="text" name="mileage" size="40" value="'.$mileage.'">';
}
else{
echo '<input type="text" name="mileage" size="40">';
}
?>
</p>
<button type="submit" value="Add">Add</button>





research $_SESSION
– ThisGuyHasTwoThumbs
Jun 28 at 12:33


$_SESSION





that guy has no subject related to what i am looking for! ??
– R.Ovis
Jun 28 at 14:16





retaining input value after refresh in php -> page title, did you read what a $_SESSION does?
– ThisGuyHasTwoThumbs
Jun 28 at 14:16



retaining input value after refresh in php


$_SESSION





haven't you seen how i pass the error back tot the same page after refresh? i tried that it didn't work
– R.Ovis
Jun 28 at 14:18





but if you change from $_GET to $_SESSION you no longer need $_GET. You just use $_POST (as is standard nowadays) and then set the data with $_SESSION and if isset $_SESSION['error'], echo?
– ThisGuyHasTwoThumbs
Jun 28 at 14:20


$_GET


$_SESSION


$_GET


$_POST


$_SESSION


isset $_SESSION['error']


echo




1 Answer
1



I got it!!
I created variables and pass the Global POST as their values like this:


$make = $_POST['make'];



And then changed the header and the condition test cases as follows:


if(strlen($make) < 1 ){
$_SESSION["error"] = "Make is required.";
header("Location: add.php?autosInput=mileage&make=$make&model=$model&year=$year&mileage=$mileage");

return;
}



And left the input side with the $GET as usual that does the magic


$GET






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift