0

Tried asking this, it was marked duplicate with a suggestion. I employed the suggestions which still do not seem to be performing the desired result. Could someone actually read the following and advise where I am going wrong?

I am attempting to use sessions to pass the user selected data from this script (allotment.php) to a subsequent script (allotmentreport.php) wherein it is used in a query qualifier (e.g. ...WHERE tablecolumndata=session variable...). I am not getting an error from allotment.php upon selecting the option and clicking SUBMIT but data fails to pass to allotmentreport.php and returns an error for an undefined variable.

Is this and the line after it correct? Is there something I am missing otherwise? $tourselect=(isset($_POST['submit']));

UPDATE The final and corrected code is displayed below for future users seeking a working example and easy to read solution:

    <?php 
    session_start();
    $host="localhost";
    $username="HIDDEN";
    $password="HIDDEN";
    $dbname="bookings2015";
    $con = mysql_connect($host, $username, $password, $dbname);
    if (!$con)
    {
    die ('damn thing wont connect to the MYSQL server: Maybe it is retarded '. mysql_error());
    }
mysql_select_db($dbname, $con);
?>
<!Doctype html>
<html>
<?php include 'C:\xampp\htdocs\phpproject1\head.php'; 
include 'config\menu.php';
?> 
<div id="dataentry">
<div id="submit">
<?php
echo "Which Tour to check availability?&nbsp;&nbsp;&nbsp;";?>
<br />
<br />
</br>
</br>
<form method="post" action="allotment_report.php">
<select name='TourCode'>
<?php
$tourselection = "SELECT DISTINCT TourCode FROM toursanddates ORDER BY TourCode";
$result = mysql_query($tourselection);
while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['TourCode'] . "'>" . $row['TourCode'] . "</option>";
}
?>
</select>
<input type="submit" name="tourselected" value="submit">
</form>
<?php
?>
</div>
</div>
<div id="demographicborder">
<?php 
include 'footer.php';?>     
        </div>
</div>
    </body>
</html>
</form>
</form>
</div>
</div>
</div>
</body>
</html>

and here is the allotment_report.php code

        <?php 
        session_start();
        $host="localhost";
        $username="STILLHIDDEN";
        $password="STILLHIDDEN";
        $dbname="bookings2015";
        $con = mysql_connect($host, $username, $password, $dbname);
        if (!$con)
        {
        die ('damn thing wont connect to the MYSQL server: Maybe it is retarded '. mysql_error());
        }
    mysql_select_db($dbname, $con);
    include 'C:\xampp\htdocs\phpproject1\head.php'; 
    include 'config\menu.php';
    ?> 
    <br />
    <br />
    <?php

//Table Header:
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><u>Tour Availability</u>";
    echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
    <tr>
        <td align=:"left"><b>Tour:</b></td>
        <td align=:"left"><b>Start Date:</b></td>
        <td align=:"left"><b>Seats Avail:</b></td>
        <td align=:"left"><b>Rooms Avail:</b></td>
    </tr>
    ';
    if(isset($_POST['TourCode'])){
            $tour=$_POST['TourCode'];
        }
    $status="ok";
    $ar="SELECT TourCode, DATE_FORMAT (TourStart, '%m%d%y') AS TourStart, SeatsAvail, RoomsAvail FROM toursanddates WHERE TourCode='$tour' AND Status='$status' ORDER BY TourCode, TourStart ASC"; 
    $result=mysql_query($ar);
    $num_results = mysql_num_rows($result);
    while($row = mysql_fetch_assoc($result)){
    //Display the allotments fetched in above query
    echo '<tr>
            <td align=:"left">' . $row['TourCode'] . '</td>
            <td align=:"left">' . $row['TourStart'] . '</td>
            <td align=:"left">' . $row['SeatsAvail'] . '</td>
            <td align=:"left">' . $row['RoomsAvail'] . '</td> 
            </tr>
            ';
    }
    echo '</table>';
    //echo "</strong>Tour:&nbsp;&nbsp;".($row['TourCode']);
    //echo "</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Start Date: ".($row['TourStart']);
        ?>
    <br />
        <?php
    echo "<br />";
    echo "</p>";
    ?>
    </br>
    </br>
    </div>
    </form> 
    </div>
    <div id="demographicborder">
    <?php include 'footer.php';
    ?>      
            </div>
    </div>
        </body>
    </html>
    </form>
    </form>
    </div>
    </div>
    </div>
    </body>
    </div>
    </body>
shane
  • 134
  • 1
  • 1
  • 10
  • Your ` – Sean Jul 20 '15 at 22:51
  • Note, you really should not ask a question twice. You can edit the original to make it more clear to bump it up. I recommend reading and absorbing this: http://stackoverflow.com/tour – nomistic Jul 20 '15 at 22:52
  • 1
    Hehe, I probably wouldn't go to production with that MySQL error handling. ;P – Hayden Schiff Jul 20 '15 at 22:53
  • Also isset returns a bool not the value passed to it. You can use `if(isset($thisVar)) { $otherVar = $thisVar; }` but setting equal to isset will be setting it to true/false just so you know. – CalebB Jul 20 '15 at 22:54
  • won't be held open to public; internal use only (otherwise I would use more stringent security,etc). – shane Jul 20 '15 at 23:02
  • thanks Sean. I will try that one. – shane Jul 20 '15 at 23:03
  • You are also going to have `header`/session` issues, as you output html before your session start -> ` – Sean Jul 20 '15 at 23:05
  • ok. As Sean suggested, I moved the form method ... action and the line with the form names to before the – shane Jul 20 '15 at 23:10
  • if(isset($thisVar)) { $otherVar = $thisVar; } ... returns an error: unexpected "if" ... this line. – shane Jul 20 '15 at 23:16
  • no header issues with the HTML. Will try moving HTML to after the session_start(); – shane Jul 20 '15 at 23:19
  • Not getting the desired result in this page code (`allotment.php`) or in `allotment_report.php`? – Sean Jul 20 '15 at 23:20
  • moving the HTML after Session_start(): yields no change. – shane Jul 20 '15 at 23:20
  • Not getting the desired result in page coded **allotment_report.php**. Allotment.php originally had errors, now corrected, but the **allotment.php** (above) is not passing the selected tour to **allotment_report.php** – shane Jul 20 '15 at 23:24
  • added allotment_report.php to the code above .... below original. – shane Jul 20 '15 at 23:28
  • 1
    Your issue is that you don't properly understand html form posting. Your `$tourselect=(isset($_POST['tourselected'])); $_SESSION['tourselected']=$tourselect;` in **allotment.php** will never work as your form posts to **allotment_report.php** and not itself. So `isset($_POST['tourselected'])` will **always** be `false`/`0`. Just delete/remove that code. Then in `allotment_report.php` change `$tour=$_SESSION['tourselected'];` to `$tour=$_POST['tourselected']` as you will want the value posted from the form. – Sean Jul 20 '15 at 23:35
  • Sean - correct. I am in the process of learning as I go. Made the suggested changes - still no result (should be returning two results or one result depending upon tour selected from the sets of sample data in the table). IF, on alltotment_report.php, I remove/delete the WHERE condition of Tourcode='$tour' and keep the status condition, it reports all data with a status of 'ok' as it should. But I just can't get it to use **allotment.php** to post the choice to **allotment_report.php**. While I can put it all into one script, I want to session it as I need to know how for a later part. – shane Jul 20 '15 at 23:54
  • ...a later part which has far more complexity. Felt it easier/logical to try it out on this simple process than one with three scripts of complexity. Trying to teach myself code (perhaps obvious). – shane Jul 20 '15 at 23:55
  • possible duplicate of [PHP MYSQL - error using Session](http://stackoverflow.com/questions/31526419/php-mysql-error-using-session) – kittykittybangbang Jul 21 '15 at 00:10
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Shehary Jul 21 '15 at 00:12
  • Stack Exchange - please ignore the comments regarding possible DUPLICATE .... the answer provided on the link to the previous answers fail to relate a solution in a manner a person just learning to code could comprehend. The answer provided by Hpierce is invaluable. – shane Jul 21 '15 at 22:21

1 Answers1

1
<?php
    //contained within allotment.php
    $tourselect=(isset($_POST['tourselected']));
    $_SESSION['tourselected']=$tourselect;
?>

It looks like you are expecting $_SESSION['tourselected'] to be set on allotment.php the when a user loads opens the page for the first time. However this is not the case. $_POST data is attached with an HTTP request. When you load alloment.php for the first time, the browser doesn't send any $_POST data to it. This would explain why $_SESSION['tourselected'] is unset when you get to your second script.

That said, if your only goal is to send data from the form built in alloment.php to alloment_report.php you shouldn't be using sessions at all. All of this can be done with only $_POST.

consider the following code:

 <!--alloment.php-->
    <form method="post" action="alloment_report.php">
        <select name='TourCode'>
            <?php 
            //Assume that $options contains stuff pulled from your database.
            foreach($options as $option) {
                echo "<option value='" . $option . "'>" . $option . "</option>";
            }
            ?>
        </select>
        <input type="submit" name="tourselected" value="submit">
    </form>

When a user completes the form, and clicks submit, alloment_report.php (specified by action="alloment_report") gets the data sent from the form over $_POST (specified by method="post").

<!--alloment_report.php-->
    <?php
    if(isset($_POST['tourcode'])){
        echo "yay! the tour has been selected!";
    }
    ?>
HPierce
  • 7,249
  • 7
  • 33
  • 49
  • Thank you for trying. However, now the entire data selection option is removed using the "foreach($options as $option) { (or really $result as $row) method. Thereby, there is nothing for the user to select. I also tried leaving the **allotment.php** as-is with just moving the HTML as you suggested and tested the **allotment_report.php** test code as you suggest and it doesn't work. So, it's definitely not posting. (I commented out the original PHP code before testing yours). – shane Jul 21 '15 at 03:15
  • /// HOWEVER, I realized I still had the *select* statement in the original PHP format **echo "";** instead of your suggested HTML .. ** – shane Jul 21 '15 at 03:23
  • Thanks again ... I left your suggested changes to HTML and removal of sessions; reinstated my tour coding and it is selecting the query correctly and displaying as intended. This is really nice of you all for taking the time to help and teach me something really interesting! Thank you HPierce and Sean and of course Stackoverlfow.com. – shane Jul 21 '15 at 03:33