0

I have a form submission with a couple dropdowns and a text input field. I have the values being uploaded to a database which works fine, however I'm attempting to carry the $_POST values over to a third success page after processing them. I can't seem to get it to work regardless of research. There's obviously something that I'm doing wrong and just need a little help to pinpoint. File examples are below. Thank you in advance for any help.

index.php

<?php
session_start();
$_SESSION["mynext"] = $_POST["mynext"];
$_SESSION["city"] = $_POST["city"];
$_SESSION["comment"] = $_POST["comment"];
?>
<form action="scripts/process.php" method="post">
            <div class="col-md-5 col-lg-3">
                <div class="form-group">
                    <label for="mynext">Choose your language</label>
                    <select class="form-control" id="mynext" name="mynext" autocomplete="off" required>
                        <option class="engl" value="My Next" selected="selected">My Next</option>
                        <option class="span" value="Mi próximo">Mi próximo</option>
                        <option class="chin-simp" value="我的未來的">我的未來的</option>
                        <option class="chin-trad" value="我的未來的">我的未來的</option>
                        <option class="kore" value="나의 다음">나의 다음</option>
                        <option class="arme" value="ԻՄ ՀԱՋՈՐԴ">ԻՄ ՀԱՋՈՐԴ</option>
                        <option class="russ" value="МОЙ БУДУЩЕГО">МОЙ БУДУЩЕГО</option>
                        <option class="viet" value="TRONG TƯƠNG LAI CỦA TÔI">TRONG TƯƠNG LAI CỦA TÔI</option>
                        <option class="japa" value="私の次の">私の次の</option>
                        <option class="thai" value="ในอนาคตของฉัน">ในอนาคตของฉัน</option>
                        <option class="khmr-camb" value="របស់ខ្ញុំបន្ទាប់">របស់ខ្ញុំបន្ទាប់</option>
                    </select>
                </div> 
            </div>
            <div class="col-md-5 col-lg-3">
                <div class="form-group">
                    <label for="city">Select where you live, work, or play</label>
                    <select class="form-control" id="city" name="city" autocomplete="off">
                        <option value="LA" selected="selected">LA*</option>
                        <option value="Agoura Hills">Agoura Hills</option>
                    </select>
                </div> 
            </div>
            <div class="col-md-2 dynamic-is">
                <p class="engl">is</p>
                <p class="hidden span">es</p>
                <p class="hidden chin-simp">是</p>
                <p class="hidden chin-trad">是</p>
                <p class="hidden kore">는 입니다</p>
                <p class="hidden arme">-ն է՝</p>
                <p class="hidden russ">— это</p>
                <p class="hidden viet">là</p>
                <p class="hidden japa">は</p>
                <p class="hidden thai">คือ</p>
                <p class="hidden khmr-camb">-ն է՝</p>
            </div>
            <div class="col-md-10 col-lg-3 comment-wrapper">
                <div class="form-group">
                    <label for="comment">Tell us what you’d like to see</label>
                    <input type="text" class="form-control" id="comment" name="comment" placeholder="Type here" autocomplete="off" required>
                </div>
            </div>
            <div class="col-md-2 col-lg-1">
                <button type="submit" name="mynext-submit" class="btn btn-lg btn-primary submit">Submit</button>
            </div>
        </form>

Once submitted the form values get uploaded via the process.php shown below

process.php

<?php 
$mynext = filter_input(INPUT_POST, 'mynext');
$city = filter_input(INPUT_POST, 'city');
$comments = filter_input(INPUT_POST, 'comment');

if(!empty($comments)){

        $host = 'localhost';
        $user = '';
        $password = '';
        $dbname = '';

        $conn = new mysqli ($host, $user, $password, $dbname);

        if(mysqli_connect_error()){
                die('Connect Error (' . mysqli_connect_error() . ') ' . mysqli_connect_error());
        }else{
            $sql = "INSERT INTO nextla (mynext, city, comment) VALUES ('$mynext', '$city', '$comments')";

            if ($conn->query($sql)){
                    echo "<br>Input data is successful";
            }
            else{
                    echo "Error: ". $sql ."<br>". $conn->error;
            }
            $conn->close();
        }

}else{

    echo 'Please add a comment';
    die();

}

header( 'Location: ../comment-success.php' );

?>

Once processed I then want to display the form field information on the comment-success.php page below.

comment-success.php

<?php include("includes/header.php"); ?>

        <div class="banner sub-banner sub-careers">
            <img src="<?php echo $siteroot; ?>img/banner/sub-contact.jpg" alt="">
            <div class="overlay"></div> 
            <h1><?php echo $page; ?></h1>       
        </div>

        <div class="email-sent-container container">
            <div class="row">
                <div class="col-12">
                    <h1 class="section-divider">Your Comments have been Submitted Successfully</h1>
                    <?php
                        echo $_SESSION["mynext"];
                        echo $_SESSION["city"];
                        echo $_SESSION["comment"];
                        session_unset();
                    ?>
                    <p>Thank you for your input. Click <a href="index.php">here</a> to return to the home page.</p>
                </div>
            </div>  
        </div>

<?php include ('includes/footer.php'); ?>

Can someone please help me find the solution. I'm sure it's something simple and I just seem to be overlooking it as I've been staring at this code for so long. Thank you for your help.

  • You can store them in one or more cookies, you can use hidden fields in the forms to carry them over, or you can use sessions to save the values. – Mr Glass Jul 05 '18 at 16:42
  • Thank you for your response. I am in fact attempting to use the $_SESSION function to achieve this, but what I have currently doesn't seem to be working. Are you able to see what I'm doing wrong? – Reuben Garcia Jul 05 '18 at 16:52

1 Answers1

0

You need to put this piece of code:

session_start();
$_SESSION["mynext"] = $_POST["mynext"];
$_SESSION["city"] = $_POST["city"];
$_SESSION["comment"] = $_POST["comment"];

On the top pf the file that actually receives the POST

process.php

<?php 
session_start();
$_SESSION["mynext"] = $_POST["mynext"];
$_SESSION["city"] = $_POST["city"];
$_SESSION["comment"] = $_POST["comment"];

$mynext = filter_input(INPUT_POST, 'mynext');
$city = filter_input(INPUT_POST, 'city');
$comments = filter_input(INPUT_POST, 'comment');
...

And add a session_start(); to the top of every file that use sessions.

PS: You should use prepared statements to avoid SQL Injection.

Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
  • So would I then be able to call in the values into comment-success.php as shown above? – Reuben Garcia Jul 05 '18 at 17:12
  • Yes. Try and see for yourself :) – Felippe Duarte Jul 05 '18 at 17:14
  • I've changed the comment-success.php to this `echo $_SESSION["mynext"]; echo $_SESSION["city"]; echo $_SESSION["comment"]; echo "Simply a TEST";`, but all I get is the "Simply a TEST" shown. I'm sorry if this seems like hand holding, it's a learning process on my end. – Reuben Garcia Jul 05 '18 at 17:38
  • add a `session_start();` to the top of comment-success.php – Felippe Duarte Jul 05 '18 at 17:44
  • Ah, that worked, always something simple but yet overlooked. I appreciate all your help. So just to clarify anywhere I'm going to be using $_SESSION variables I need to add the session_start()? – Reuben Garcia Jul 05 '18 at 17:49
  • Yes. You can include/require a file with `session_start();` instead, that will do the same. In the future, any other command needed to every file could be added to this file. – Felippe Duarte Jul 05 '18 at 17:53