Reputation: 1
I'm currently putting together a small form project for a client. Since the client's web host does not support PHP, I've had to iframe the form from another host. It's not ideal but it's what I've got to work with at the moment.
Usually I wouldn't have much of an issue with forms except that this form is multi-paged. It also requires PHP because it contains API keys.
Here's the main part of the code.
<?php
echo file_get_contents("make.html");
if (isset($_POST['devicemake']))
{
$deviceMake = $_POST['devicemake'];
echo "<script type='text/javascript'>\n";
echo "document.body.innerHTML = ''";
echo "</script>";
echo $deviceMake;
echo file_get_contents("model.html");
}
if (isset($_POST['devicemodel']))
{
$deviceModel = $_POST['devicemodel'];
echo "<script type='text/javascript'>\n";
echo "document.body.innerHTML = ''";
echo "</script>";
echo $deviceMake . $deviceModel;
echo file_get_contents("description.html");
}
?>
It does is loads the first page of the form and then once the user has chosen an option, it puts their answer into a variable, clears the page then loads the next one.
I've tested each page individually and it seems to be able to put each value into a variable after being submitted but one issue I can't seem to figure out is why it will only echo back the last variable saved. For example, if I submit the second form page, I should now have two variables. But upon echoing both, only the deviceModel variable is echoed.
Why is this? I've searched and searched over this and simply can't seem to find why it won't hold variables across pages. The PHP file itself isn't reloading so it shouldn't be an issue, right?
Upvotes: 0
Views: 220
Reputation: 1075
PHP is server side language, so even if you are not reloading entire page each time, when you sent new POST request to PHP script, it will treat it as standalone request only and won't remember any values from your previous request.
What you will need to do is, you should save the earlier value in a php session.
For example,
If you want to save $_POST['devicemake']
value in first request and $_POST['devicemodel']
on second one. You can store first value using following code.
session_start();
if (isset($_POST['devicemake']))
{
$_SESSION['deviceMake'] = $_POST['devicemake'];
echo "<script type='text/javascript'>\n";
echo "document.body.innerHTML = ''";
echo "</script>";
echo $_SESSION['deviceMake'];
echo file_get_contents("model.html");
}
On next request you can get that using $_SESSION['deviceMake']
.
Upvotes: 1
Reputation: 160
Your second POST resets the variables passed. You can either switch to a GET and concatenate the variables or you can post $deviceMake
as a hidden variable the second time.
Upvotes: 1