I have only recently got my head around PHP and got stuck when duplicating a single piece of code multiple times. I have made everything have their unique variables and linked them to the right name attributes in the input element and select elements. I don't really know much to do with PHP but I have looked around a lot but haven't managed to find any solution.
When I select an option (Subaru) then click submit it comes up with this:
You have selected: Subaru
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/SelectDropDowns/home.php on line 10
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/SelectDropDowns/home.php on line 16
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/SelectDropDowns/home.php on line 22
Notice: Undefined index: modelSelectRight in /Applications/XAMPP/xamppfiles/htdocs/SelectDropDowns/home.php on line 28
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/SelectDropDowns/home.php on line 28
Notice: Undefined index: yearSelectRight in /Applications/XAMPP/xamppfiles/htdocs/SelectDropDowns/home.php on line 34
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/SelectDropDowns/home.php on line 34
I will provide the relevant code in CSS as well as HTML/PHP. It may be an obvious mistake but please let me know if any further information is required. Thanks.
Please note that the HTML file's extension is .php
body{
display:block;
text-align: right;
background-color: lightblue;
}
#wrapper{
margin: 0px auto;
width: 800px;
border: 1px solid black;
}
#top_header{
padding: 15px;
background-color:;
text-align: left;
}
#top_nav{
border: 1px solid black;
text-align: center;
}
#top_nav li{
display: inline-block;
list-style: none;
text-decoration: none;
padding: 5px;
}
.list_boxes{
border: 1px solid black;
padding-left: 15px;
padding-right: 15px;
margin: 0px auto;
}
#main_body{
text-align:center;
border: 1px solid black;
height: 100%;
}
#sec1{
padding: 10px;
margin: 10px;
float: left;
width: 40%;
background-color: #D3D3D;
}
.circle_image{
border-radius: 50%;
background-color:none;
width: 175px;
height: 175px;
margin: 0px auto;
border: 1px double black;
}
.button_wrap{
margin-top: 30px;
}
.ListItemsDecoration li{
text-decoration: none;
list-style: none;
}
.button_wrap select{
margin-bottom: 10px;
width: 200px;;
height: 30px;
text-align: center;
}
#symbol{
text-align: center;
margin-left: 27.25%;
width: 100px;
position: absolute;
}
#compare_button{
margin-top:100px;
width: 79px;
}
#sec2{
padding: 10px;
margin: 10px;
float: right;
width: 40%;
background-color: #D3D3D;
}
<!DOCTYPE>
<?php
if(isset($_POST['Compare'])){
foreach ($_POST['makeSelectLeft'] as $selectMake1)
{
echo "You have selected :" .$selectMake1;
}
}
if(isset($_POST['Compare'])){
foreach ($_POST['modelSelectLeft'] as $selectModel1)
{
echo "You have selected :" .$selectModel1;
}
}
if(isset($_POST['Compare'])){
foreach ($_POST['yearSelectLeft'] as $selectYear1)
{
echo "You have selected :" .$selectYear1;
}
}
if(isset($_POST['Compare'])){
foreach ($_POST['makeSelectRight'] as $selectMake2)
{
echo "You have selected :" .$selectMake2;
}
}
if(isset($_POST['Compare'])){
foreach ($_POST['modelSelectRight'] as $selectModel2)
{
echo "You have selected :" .$selectModel2;
}
}
if(isset($_POST['Compare'])){
foreach ($_POST['yearSelectRight'] as $selectYear2)
{
echo "You have selected :" .$selectYear2;
}
}
?>
<html>
<body>
<head>
<meta charset="UTF-8" /> <!--Specifies the character encoding of the document-->
<link rel="stylesheet" href="CSS.css">
<title>Home</title>
<script type="text/javascript" src="Javascript.js"></script>
</head>
<div id="wrapper">
<header id="top_header">
<h1>CarCompare</h1>
</header>
<nav id="top_nav">
<ol>
<a class="list_boxes"><li>Home</li></a> |
<a class="list_boxes"><li>Contact Us</li></a> |
<a class="list_boxes"><li>About Us</li></a> |
<a class="list_boxes"><li>Cars</li></a> |
<a class="list_boxes"><li>Trucks</li></a> |
<a class="list_boxes"><li>Bikes</li></a> |
<a class="list_boxes"><li>Reviews</li></a> |
</ol>
</nav>
<div id="main_body">
<h3>COMPARE</h3>
<aside id="sec1">
<form method="post" action="#" class="CompareWrapperListed">
<nav class="ListItemsDecoration">
<li><div class="circle_image"></div></li>
<div class="button_wrap">
<li><select name="makeSelectLeft[]" id="1MakeSelect" type="option"></li>
<option>Make</option>
<option value="Mitsubishi">Mitsubishi</option>
<option value="Subaru">Subaru</option>
<option value="Toyota">Toyota</option>
</select>
</li><select name="modelSelectLeft" id="1ModelSelect" type="option"></li>
<option>Model</option>
<option value="Lancer">Lancer</option>
<option value="Evolution">Evolution</option>
<option value="Camry">Camry</option>
</select>
<li><select name="yearSelectLeft" id="1YearSelect" type="option"></li>
<option>Year</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
</select>
</div>
</nav>
</aside>
<input type="submit" name="Compare" id="compare_button" />
<aside id="sec2">
<nav class="ListItemsDecoration">
<li><img class="circle_image"></img></li>
<div class="button_wrap">
<nav></nav>
<li><select name="makeSelectRight" id="1MakeSelect" type="option"></li>
<option>Make</option>
<option value="Mitsubishi">Mitsubishi</option>
<option value="Subaru">Subaru</option>
<option value="Toyota">Toyota</option>
</select>
<li><select name="makeModelRight" id="1ModelSelect" type="option"></li>
<option>Model</option>
<option value="Lancer">Lancer</option>
<option value="Evolution">Evolution</option>
<option value="Camry">Camry</option>
</select>
<li><select name="makeYearRight" id="1YearSelect" type="option"></li>
<option>Year</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
</select>
</div>
</nav>
</form>
</aside>
</div>
</div>
<script type="text/javascript" src="CarcompanyNewJavascript.js"></script>
</body>
</html>
</div>
you only need to check one time for a post then you check that the values not empty before doing your loop
<?php
//check once if is posted
if(isset($_POST['Compare'])){
//check if the post value empty or not
if(!empty($_POST['makeSelectLeft'])){
foreach ($_POST['makeSelectLeft'] as $selectMake1)
{
echo "You have selected :" .$selectMake1;
}
}
if(!empty($_POST['modelSelectLeft'])){
foreach ($_POST['modelSelectLeft'] as $selectModel1)
{
echo "You have selected :" .$selectModel1;
}
}
if(!empty($_POST['yearSelectLeft'])){
foreach ($_POST['yearSelectLeft'] as $selectYear1)
{
echo "You have selected :" .$selectYear1;
}
}
if(!empty($_POST['makeSelectRight'])){
foreach ($_POST['makeSelectRight'] as $selectMake2)
{
echo "You have selected :" .$selectMake2;
}
}
if(!empty($_POST['modelSelectRight'])){
foreach ($_POST['modelSelectRight'] as $selectModel2)
{
echo "You have selected :" .$selectModel2;
}
}
if(!empty($_POST['yearSelectRight'])){
foreach ($_POST['yearSelectRight'] as $selectYear2)
{
echo "You have selected :" .$selectYear2;
}
}
}
?>
then in your html do your select like this to choose multiple
<select name="makeSelectLeft[]" id="1MakeSelect" type="option" multiple="multiple">