-2

So as of now, i can successfully get the results to move from page one to page two using post and get, but no matter what im doing it will not move the info to the 3rd page. Im trying to switch it over to sessions after reading its made exactly for this but for some reason im doing something wrong and after hours of searching i cant for the life of me figure out what it is. I've followed guides, followed videos, and other post related to the topic on this website. I have now come to the conclusion that it is just me and i need some assistance. Any help would be greatly appreciated.

Page 1 (Index Page | Input Your Variables):

<?php session_start();
$_GET['q'] = $q;
$_GET['s'] = $s;
?>

<form action="search.php" method="get">
<input name="q" maxlength="8" type="text" placeholder="License Plate" id="textbox" required />
<select name="s" id="s" required aria-required="true">
<option value="" disabled selected>CHOOSE STATE</option>
<option value="AL">ALABAMA</option>
<option value="AK">ALASKA</option>
<option value="AZ">ARIZONA</option>
<option value="AR">ARKANSAS</option>
<option value="CA">CALIFORNIA</option>
<option value="CO">COLORADO</option>
<option value="CT">CONNECTICUT</option>
etc...
</select>
<input type="submit" value="SEARCH" id="submitbtn"></form>

Page 2 (Search.php that will take you directly to page specified if its already been created):

<?php session_start();
$q = $_POST['q'];
$s = $_POST['s'];
?>
<?php
$dir = 'states';
$s = (isset($_GET['s']))? strtolower($_POST['s']) : '';
$q = (isset($_GET['q']))? strtoupper($_POST['q']) : ''; 
$res = opendir($dir); 

while(false!== ($file = readdir($res))) {
    if(strpos(strtoupper($file),$q)!== false &&!in_array($file)) {
        echo "<a href='$dir/$s/$q.htm'>$file</a>";
    }
}

closedir($res);
?>

<?php
echo $htmlHeader;
while($stuff){
echo $stuff;
}
echo "<script>window.location = 
'http://www.somesite.com/$dir/$s/$q.htm'</script>";
 ?>

Page 3 (404 page for catch all that are not in the system):

<?php session_start();
?>
<form action="" method="" name="FormChoice">
<input name="q" maxlength="8" type="text" value="<?php echo $_POST['q']; ?>" id="q" required>
<select name="s" id="s" required aria-required="true">
<option value="" disabled>CHOOSE STATE</option>
<option value="AL" <?php if($_POST['s'] == al) {echo ' selected="selected"';} ?>>ALABAMA</option>
<option value="AK" <?php if($_POST['s'] == ak) {echo ' selected="selected"';} ?>>ALASKA</option>
<option value="AZ" <?php if($_POST['s'] == az) {echo ' selected="selected"';} ?>>ARIZONA</option>
<option value="AR" <?php if($_POST['s'] == ar) {echo ' selected="selected"';} ?>>ARKANSAS</option>
<option value="CA" <?php if($_POST['s'] == ca) {echo ' selected="selected"';} ?>>CALIFORNIA</option>
<option value="CO" <?php if($_POST['s'] == co) {echo ' selected="selected"';} ?>>COLORADO</option>
<option value="CT" <?php if($_POST['s'] == ct) {echo ' selected="selected"';} ?>>CONNECTICUT</option>
</select>
<input type="submit" id="submitbtn2" value="SEARCH" name="submit" OnClick="search()" />
<span id="or">OR</span>
<input type="submit" id="addbtn" value="ADD" name="submit" OnClick="add()" /> 
</form>
  • Its a good idea to load a value into your variables BEFORE trying to store the value of that variable into a `$_SESSION['']` variable – RiggsFolly Apr 18 '18 at 15:27
  • 2
    Page1 and Page2) `$q` and `$s` HAVE NO VALUE when you try and store those variables into the SESSION !!!! – RiggsFolly Apr 18 '18 at 15:28
  • So following @RiggsFolly, what are $q and $s values? Are they set higher up in the pages? – Nic3500 Apr 18 '18 at 15:29
  • `$_GET['q']` and `s` – AbraCadaver Apr 18 '18 at 15:34
  • Sorry everyone, i fixed the error that i posted, it didnt reflect my current code. Now it does. The code i had posted before was one i was messing with on a test page. – Adam Tourgeman Apr 18 '18 at 15:44
  • @RiggsFolly I have edited the code to be what it should have been. Do you still see the same error? The variable is typed into the form below. – Adam Tourgeman Apr 18 '18 at 15:49
  • @Nic3500 No the variables are typed in by user below the GET statement. Should it be below the form? I was wondering that but i wasnt sure. – Adam Tourgeman Apr 18 '18 at 15:50
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Apr 18 '18 at 15:52
  • Still PAGE2 does not place anything in the SESSION – RiggsFolly Apr 18 '18 at 15:53
  • @RiggsFolly As much as i want to say i know what that is im still new to PHP. A little help in what you mean would go a long way. Do i have something backwards? Or is the variable backwards? – Adam Tourgeman Apr 18 '18 at 15:56
  • I actually want to thank you guys, i figured out what i was doing wrong thank you all so much! I simply had to google how to convert POST or GET data to SESSION data. Fixed all my problems. Its working right as far as i can see. – Adam Tourgeman Apr 18 '18 at 17:04

1 Answers1

1

page1

<?php 
session_start();
// next 2 lines do NOTHING remove them
// as you have not yet loaded any values into $q and $s
//$_GET['q'] = $q;
//$_GET['s'] = $s;
?>

<form action="search.php" method="get">
<input name="q" maxlength="8" type="text" placeholder="License Plate" id="textbox" required />
<select name="s" id="s" required aria-required="true">
<option value="" disabled selected>CHOOSE STATE</option>
<option value="AL">ALABAMA</option>
<option value="AK">ALASKA</option>
<option value="AZ">ARIZONA</option>
<option value="AR">ARKANSAS</option>
<option value="CA">CALIFORNIA</option>
<option value="CO">COLORADO</option>
<option value="CT">CONNECTICUT</option>
etc...
</select>
<input type="submit" value="SEARCH" id="submitbtn"></form>

Page 2 - Search - receives data from previous form - Contains lots of unecessary <?php...?> - Previous form uses method="get" so data will arrive in the $_GET array not the $_POST array

<?php 
session_start();
//$q = $_POST['q'];
//$s = $_POST['s'];

// But this is silly as you have not yet tested these values exist
// but you do that in the next lines
//$q = $_GET['q'];
//$s = $_GET['s'];

$dir = 'states';
$s = (isset($_GET['s']))? strtolower($_POST['s']) : '';
$q = (isset($_GET['q']))? strtoupper($_POST['q']) : ''; 
$res = opendir($dir); 

// Now if you want to pass the values of `q` and `s` on to the next form
// they now need to be added to the session
$_SESSION['q'] = $q;
$_SESSION['s'] = $s;

while(false!== ($file = readdir($res))) {
    if(strpos(strtoupper($file),$q)!== false &&!in_array($file)) {
        echo "<a href='$dir/$s/$q.htm'>$file</a>";
    }
}

closedir($res);

echo $htmlHeader;
while($stuff){
    echo $stuff;
}
echo "<script>
        window.location = 'http://www.somesite.com/$dir/$s/$q.htm';
      </script>";
// added missing semi colon                                       ^
?>

Page 3 (404 page for catch all that are not in the system):

Now the data will be available in the SESSION, when you get to this page.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149