0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll give it a try, cheers. I spent a while using GET but it was difficult to do without using sub-iframes or reloading the PHP file after every submission. Just realised I'd accidentally posted it here instead of SO.
1

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'].

1 Comment

To elaborate on a good answer. Make sure session_start() is at the very beginning of your php code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.