Reputation: 62
So I understand if this may take a while to process but I have a dropdown box and what it's meant to do is if certain selections are clicked then it will fetch info in the database depending on what they selected. My question is, why aren't my if
statements working when I have applied a session onto each selection and am fetching them based on the ones clicked.
Here is the HTML:
<div class="button_wrap">
<select name="makeSelectLeft" class="compareButtonSizing" required="required" multiple="multiple">
<option>Make</option>
<?php session_start(); $_SESSION['MitOne']="Mitsubishi1" ?><option value="Mitsubishi1">Mitsubishi</option>
<?php session_start(); $_SESSION['SubOne']="Subaru1" ?><option value="Subaru1">Subaru</option>
<?php session_start(); $_SESSION['ToyOne']="Toyota1" ?><option value="Toyota1">Toyota</option>
</select>
<select name="modelSelectLeft" required="required" multiple="multiple">
<option>Model</option>
<?php session_start(); $_SESSION['LancOne']="Lancer1" ?><option value="Lancer1">Lancer</option>
<?php session_start(); $_SESSION['EvoOne']="Evolution1" ?><option value="Evolution1">Evolution</option>
<?php session_start(); $_SESSION['CamOne']="Camry1" ?><option value="Camry1">Camry</option>
</select>
<select name="yearSelectLeft" required="required" multiple="multiple">
<option>Year</option>
<?php session_start(); $_SESSION['1982 - 1983']="1982 - 1983" ?><option value="1982 - 1983">1982 - 1983</option>
<?php session_start(); $_SESSION['1983 - 1991']="1983 - 1991" ?><option value="1983 - 1991">1983 - 1991</option>
<?php session_start(); $_SESSION['1988 - 1995']="1988 - 1995" ?><option value="1988 - 1995">1988 - 1995</option>
<?php session_start(); $_SESSION['1995 - 2000']="1995 - 2000" ?><option value="1995 - 2000">1995 - 2000</option>
<?php session_start(); $_SESSION['2000 - 2007']="2000 - 2007" ?><option value="2000 - 2007">2000 - 2007</option>
<?php session_start(); $_SESSION['2007 - 2017']="2007 - 2017" ?><option value="2007 - 2017">2007 - 2017</option>
</select>
</div>
Thats where I added in the starts for the sessions. There is a session for each selection. In the PHP I currently have three outputs no matter what options I select. It's supposed to only have one and show one depending on what Make, Model, and Year you have selected.
Here is the PHP:
<?php
session_start();
if (!empty($_SESSION['MitOne']) && ($_SESSION['LancOne']) && ($_SESSION['1982 - 1983'])) {
$MitFirstState = mysql_query("SELECT Max_Speed FROM CarSpecifications WHERE id=2");
while ($MitFirstFetch = mysql_fetch_array($MitFirstState)) {
print_r($MitFirstFetch[0]);
}
}
if (!empty($_SESSION['SubOne']) && ($_SESSION['LegOne']) && ($_SESSION['1982 - 1983'])) {
$SubFirstState = mysql_query("SELECT Max_Speed FROM CarSpecifications WHERE id=3");
while ($SubFirstFetch = mysql_fetch_array($SubFirstState)) {
print_r($SubFirstFetch[0]);
}
}
if (!empty($_SESSION['ToyOne']) && ($_SESSION['CamOne']) && ($_SESSION['1982 - 1983'])) {
$ToyFirstState = mysql_query("SELECT Max_Speed FROM CarSpecifications WHERE id=4");
while ($ToyFirstFetch = mysql_fetch_array($ToyFirstState)) {
print_r($ToyFirstFetch[0]);
}
}
?>
The main thing I don't understand is why the if
statements aren't working. I have tried putting else
statements on there and changing the isset
/empty
variables and all of it. It doesn't seem to work.
Again I apologise for the large amount of info. If someone could help that would be greatly appreciated.
P.S. If someone has a javascript solution they are welcome.
Thank You.
Upvotes: 0
Views: 64
Reputation: 12505
None of this is being executed properly. As mentioned, you need your session_start();
one time and at the top of the page. Second you need to take advantage of form submission values, not session values. Last, you have to submit the form or use AJAX to process the user's selections. Here is an example (submission of the form is required, you would need to research ajax to do dynamic selections):
<?php
# I'm not convinced you even need this in your example, but you only put it
# once at the top of the page
session_start();
# Process the submission
if(!empty($_POST['action']) && $_POST['action'] == 'do_options') {
# Here is where you do your if conditions based on the post, not session
print_r($_POST['selectleft']);
}
?>
<!--
....html on your page...etc.
-->
<form action="" method="post">
<input type="hidden" name="action" value="do_options" />
<div class="button_wrap">
<select name="selectleft[make]" class="compareButtonSizing" required="required" multiple="multiple">
<option value="">Make</option>
<option value="Mitsubishi1">Mitsubishi</option>
<option value="Subaru1">Subaru</option>
<option value="Toyota1">Toyota</option>
</select>
<select name="selectleft[model]" required="required" multiple="multiple">
<option value="">Model</option>
<option value="Lancer1">Lancer</option>
<option value="Evolution1">Evolution</option>
<option value="Camry1">Camry</option>
</select>
<select name="selectleft[year]" required="required" multiple="multiple">
<option>Year</option>
<option value="1982_1983">1982 - 1983</option>
<option value="1983_1991">1983 - 1991</option>
<option value="1988_1995">1988 - 1995</option>
<option value="1995_2000">1995 - 2000</option>
<option value="2000_2007">2000 - 2007</option>
<option value="2007_2017">2007 - 2017</option>
</select>
</div>
<input type="submit" value="SAVE" />
</form>
Upvotes: 1
Reputation: 979
Request-Response is actually simpler than you're trying to attempt.
Your request session in 2nd file will have values initialised for parameters "makeSelectLeft, modeSelectLeft and yearSelectLeft" in the $_REQUEST object. These values are data in "value" attribute of "option".
<option value="Mitsubishi1">
Hence this php snippet is redundant. Remove these from your HTML.
<?php session_start(); $_SESSION['MitOne']="Mitsubishi1" ?>
In your PHP code, $_REQUEST['makeSelectLeft'] will give all values of options selected by user. Similarly you can retrieve data for others. Following that, make your database queries etc.
Upvotes: 1