"Double Load" Issue with PHP isset() and $_POST data - php

I know I am doing something wrong by using a combination of isset(), $_POST and $_GET but I am wondering what would be the easiest and painless way to tackle my issue.
The issue arises when I submit a HTML form... It reloads the page with the post data. I capture the submit with a php isset() function, process the $_POST data and then run a window.location to refresh the page with some $_GET data.
For example...
//example.php
...
<form action="" method="post">
<input name="stage1name" type="text" class="textfield" size="22" value="<?php echo $ClaimRow['ClaimantName']; ?>">
<input name="VehicleEngine" type="text" class="textfield" size="20" value="<?php echo $vehiclerow['VehicleEngine']; ?>">
<input name="VehicleFuel" type="text" class="textfield" size="20" value="<?php echo $vehiclerow['VehicleFuel']; ?>">
<input name="submitInfo" type="submit" value="<?php echo $LANG_Claims_Change_Info; ?>" />
</form>
...
<?php
if (isset($_POST['submitInfo']))
{
$stage1name= mysqli_real_escape_string($db, $_POST['stage1name']);
$VehicleEngine= mysqli_real_escape_string($db, $_POST['VehicleEngine']);
$VehicleFuel= mysqli_real_escape_string($db, $_POST['VehicleFuel']);
mysqli_query($db, "DO SOME COOL SQL HERE");
//I've done what I need to do so lets reload the page with the updated data
echo "<script>window.location='example.php?vehicle=" . $vehicleID . "&claimTab=Personal'</script>";
}
?>
Like I said before, this method works fine however the user gets the effect of a "double load" and is very epiletic fit inducing.
Any ideas how best to combat this?
Thanks
EDIT - Additional Example
I realised that this one example might not make complete sense so I put together another example which hopefully will.
...
<form action="" method="post">
<select name="addresstype[]" id="multiselectfrom" onchange='this.form.submit();' size="9" style="width:110px; text-align:center;">
<option style="<?php if ($AddType == 'Claimant') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Claimant"><?php echo $LANG_Claims_Claimant; ?></option>
<option style="<?php if ($AddType == 'Vehicle') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Vehicle"><?php echo $LANG_Claims_Vehicle; ?></option>
<option style="<?php if ($AddType == 'Repairer') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Repairer"><?php echo $LANG_Claims_Repairer; ?></option>
<option style="<?php if ($AddType == 'Insurer') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Insurer"><?php echo $LANG_Claims_Insurer; ?></option>
<option style="<?php if ($AddType == 'Fleet') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Fleet"><?php echo $LANG_Claims_Fleet; ?></option>
</select>
</form>
...
<?php
if (isset($_POST['addresstype']))
{
foreach ($_POST['addresstype'] as $addresstype) {
$addresstype2 = $addresstype;
}
echo "<script>window.location='claims.php?claimID=" . $claim . "&claimTab=Addresses&AddressType=" . $addresstype2 . "'</script>";
}
?>
The above example is supposed to take the result of the form and change the window.location depending on the form result. It does work however it loads the page twice in doing so.

Repalace:
echo "<script>window.location='example.php?vehicle=" . $vehicleID . "&claimTab=Personal'</script>";
With:
echo "<META HTTP-EQUIV = 'Refresh' Content = '0; URL =example.php?vehicle=" . $vehicleID . "&claimTab=Personal'>";

After much testing. It turns out CMorriesy was correct. However for future reference you will need to turn output_buffering = on in php.ini and also add <?php ob_start(); ?> to the very first line of your code.
header('Location: example.php?vehicle=' . $vehicleID . '&claimTab=Personal'); die();

Related

Is it possible to store variable in input box in HTML or PHP

I would like to create an input box that is only readable and have the title of the book, is it possible that the input box can store a variable?
P.S The one I want to change is the id, now I successfully disabled the input box, but the output is $id instead of the variable that I use the $_GET method.
My code is as follow
<?php
include_once 'header.php';
$id = mysqli_real_escape_string($conn, $_GET['bookid']);
$title = mysqli_real_escape_string($conn, $_GET['title']);
?>
<section class="signup-form">
<h2>Pre-order</h2>
<div class="signup-form-form">
<form action="preorder.inc.php" method="post">
<input type="text" disabled="disabled" name="id" value= $id >
<input type="text" name="uid" placeholder="Username...">
<input type="text" name="BRO" placeholder="BookRegistrationCode...">
<button type="submit" name="submit">Pre-order</button>
</form>
</div>
<?php
// Error messages
if (isset($_GET["error"])) {
if ($_GET["error"] == "emptyinput") {
echo "<p>Fill in all fields!</p>";
}
else if ($_GET["error"] == "wronginfo") {
echo "<p>Wrong information!</p>";
}
else if ($_GET["error"] == "stmtfailed") {
echo "<p>Something went wrong!</p>";
}
else if ($_GET["error"] == "none") {
echo "<p>Success!</p>";
}
}
?>
</section>
Put <?php echo $var; ?> inside the value attribute.
<input type="text" value="<?php echo $var; ?>" />
EDIT
As per #arkascha's comment, you can use alternative php short tags:
<input type="text" value="<?= $var; ?>" />
As per the docs: https://www.php.net/manual/en/language.basic-syntax.phptags.php

Send PHP form data to a different page

I have just written this code in which I have a form. You have to write your name, surname and country. You also have to choose your favourite colour. After that, you push a submit button so that you can see the data afterwards. I'm using the GET method with 1 page, but I have to use a second one with the POST method so that each echo is on that second page.
How could I do that? My code is:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form</title>
<link rel="stylesheet" type="text/css" href="form.css">
</head>
<body>
<section>
<?php
if (isset($_GET["name"])){
$name = $_GET["name"];
if ($name != ""){
$surname = $_GET["surname"];
$country = $_GET["country"];
$colour = $_GET["colour"];
echo "<p>";
echo "<h2>Data</h2>";
echo $name . " " . $surname . "</br />";
echo $country . "<br />";
echo $colour;
echo "</p>";
}else
echo "<strong>Complete the blank spaces</strong>";
}else{
?>
<h1>Form</h1>
<form class="elegant" method="GET" action="?">
<fieldset>
<legend>Favourite colour</legend>
<div>
<label for="nombre">Name</label>
<input type="text" placeholder="Name" name="name"
id="name" />
</div>
<div>
<label for="surname">Surname</label>
<input type="text" placeholder="Surname" name="surname"
id="surname" size="50" />
</div>
<div>
<label for="country">Country</label>
<input type="text" placeholder="Country" name="country" id="country"
size="10" maxlength="9" />
</div>
<div>
<select name="colour" id="colour">
<option value="yellow" <?php if ($colour == "yellow" ) echo "selected" ?> >yellow</option>
<option value="red" <?php if ($colour == "red" ) echo "selected" ?> >red</option>
</div>
<input class="btn" type="submit" value="Save" />
</fieldset>
</form>
<?php
}
?>
</section>
</body>
</html>
I know i have to use a link to that second page, but that's all I know. Thanks in advance!
If I understand correctly, the code you show is for Page 1 where the user can:
Input data if data don't exist;
View data and confirm them if they exist. At this point store data in SESSION and send the user to an other page.
To do so, remember you have to add the session_start() command right at the beginning of every page in which you will be able to manipulate Session Data.
<?php
session_start();
if (isset($_GET["name"])){
$name = $_GET["name"];
if ($name != ""){
$surname = $_GET["surname"];
$country = $_GET["country"];
$colour = $_GET["colour"];
echo "<p>";
echo "<h2>Data</h2>";
echo $name . " " . $surname . "</br />";
echo $country . "<br />";
echo $colour;
echo "</p>";
$_SESSION["name"] = $name;
$_SESSION["surname"] = $surname;
$_SESSION["country"] = $country;
$_SESSION["colour"] = $colour;
Confirm
}else
echo "<strong>Complete the blank spaces</strong>";
}else{
?>
...
In "another_page.php" then you will find that you can access your data simply querying the $_SESSION array.
<?php
session_start();
...
// Echo session variables that were set on previous page
echo "Name is " . $_SESSION["name"] . ".<br>";
echo "Surname is " . $_SESSION["surname"] . ".";
// etc.
?>
Complete reference is the PHP Manual and simple reference is in W3C PHP Sessions page.
In the form tag you can specify where to form gets submitted to:
<form class="elegant" method="GET" action="YOUR_PAGE_URL_HERE">
On that second page you get the values via the $_GET Variable like
<?php
echo $_GET['name'].' '.$_GET['surname'];

Button inside echo

Here is my php codeigniter view to show Time(If it is null,it will shows ERROR).
<input type="text" class="form-control timepicker" id="checkInTime" name="checkInTime" value="<?php if ($result->checkInTime == '0'||'NULL'){ echo 'ERROR';} else{ echo date('H:i:s',strtotime($result->checkInTime));}?>" required parsley-maxlength="6" placeholder="checkInTime" disabled/>
How can i change the ERROR into a button or link(Which is inside the echo).
The simplest way is to use if condition outside the input:
<?php
if ($result->checkInTime == 0 || $result->checkInTime == NULL) {
// here echo your button or link
} else { ?>
<input type="text" class="form-control timepicker" id="checkInTime" name="checkInTime" value="<?php echo date('H:i:s',strtotime($result->checkInTime));}?>" required parsley-maxlength="6" placeholder="checkInTime" disabled/>
<?php } ?>
In the sawe way that you echo ERROR, echo the HTML you want to render, within quotes (balance the quotes and escape them appropriately).
You could use something like this -
<input type="text" class="form-control timepicker" id="checkInTime" name="checkInTime" value="<?php if ($result->checkInTime == '0'||'NULL'){ echo "<button class=\"button-class\" value=\"button\"></button>" ;} else{ echo date('H:i:s',strtotime($result->checkInTime));}?>" required parsley-maxlength="6" placeholder="checkInTime" disabled/>

How can I remove an empty space in php output?

<input type="hidden" name="status" class="status" value="
<?php if (!empty ($status)) {
echo $status;
} else {
echo "default";
} ?>"
/>
My result is:
<input type="hidden" name="status" class="status" value=" default">
My problem is: There is an empty space in value=" default"
And I cannot remove it. I cannot see, why it is there...
Change
<input type="hidden" name="status" class="status" value="
<?php if (!empty ($status)) {
echo $status;
} else {
echo "default";
} ?>"
/>
To:
<input type="hidden" name="status" class="status" value="<?php if (!empty ($status)) {
echo $status;
} else {
echo "default";
} ?>"
/>
Line breaks are validated as a space if in HTML scope. So you could, for example, place the opening PHP tag directly after value=". Anyway, let me make this suggestion to improve your code:
<input (...) value="<?php echo !empty($status) ? $status : 'default'; ?>">
Shorthand if statements are pretty useful for these cases because they are compact and are, if you get used to them, very readable.
Please note, that since HTML5 you don't need the self-escaping (/>) for tags that are marked as self-closing. the input tag is one of them.

form variables not passing

It seems my code is correct, however the posted variables in the form do not echo in the update user settings page in the form. I have echoed the posted ids from the input in the database but I cannot get the variables to show. I have done this in Codeigniter fine but am trying to do it in pure php with my own framework.
$users = new Users($db);
comes from my init.php file that is called at the beginning of the file below.
when I do
<?php var_dump($user['first_name'])?>
I get Null
<input type="text" name="first_name" value="<?php if (isset($_POST['first_name']) )
{echo htmlentities(strip_tags($_POST['first_name']));} else { echo
$user['first_name']; }?>">
Try $_REQUEST['first_name'] first before submitting it to array.
<?=$_REQUEST['first_name']?>
<input type="text" name="first_name" value="<?php if (isset($_POST['first_name']) )
{echo htmlentities(strip_tags($_POST['first_name']));} else { echo
$user['first_name']; }?>">
try this code
Is your form using get or post method? If it is using get method $_POST will be null. If you use $_REQUEST, it will show the result regardless of the http method used.
try it i think its work fine and solve your problam
<?php
if(isset('name of submit button ')) // example $_POST['submit'];
{
?>
<input type="text" name="first_name" value="<?php echo htmlentities(strip_tags($_POST['first_name'])) ?> " >
<?php
}
else
{
?>
<input type="text" name="first_name" value="<?php echo $user['first_name']; ?> " >
<?php
}
?>
try it
<?php
$first_name = $_POST['first_name'];
?>
<input type="text" name="first_name" value="<?php if (isset($first_name)){ echo htmlentities(strip_tags($first_name));} else { echo $users['first_name']; } ?> ">

Categories