Timothy Coetzee Timothy Coetzee - 3 months ago 12
PHP Question

PHP function returning wrong value

Please note this is a beginners question - I'm Working on a very simple car booking system for school, user selects car group (a, b, c, d) from page 1, he then gets redirected to page 2 where he completes his booking

MY PROBLEM

Im trying to use a simple function to retrieve the value from page 1 and add it to image src. The value does get passed correctly however in my function the value is ALWAYS

a
regardless of which selection was made.

PAGE 1

`<select class="form-control col-xs-4" name="carType" id="sel1">
<option value="a">Group A (Hyundai I10)</option>
<option value="b">Group B (VW POLO)</option>
<option value="c">Group C (Corolla)</option>
<option value="d">Group D (TRUCK)</option>
</select>`


enter image description here

PAGE 2

When I do
echo $_POST['carType']
I get the correct value, thus the problem is with my function, since I always get the value
a
returned.

enter image description here

FUNCTION

function getCarGroup($group){
//extension must end in PNG
if($group == 'a'){
$src = 'a';
}
else if($group == 'b'){
$src = 'b';
}
else if($group == 'c'){
$src = 'c';
}
else if($group =='d'){
$src = 'd';
}
return $src;
}


FUNCTION CALL

<img src="img/cars/<?php echo getCarGroup($carType) ?>.png" class="img-responsive" />


Result

Should display
b
NOT
a


enter image description here

Any advice / help much appreciated

UPDATE:

function getCarGroup($group){
//extension must end in PNG
if($group = 'a'){
$src = 'a';
}
else if($group = 'b'){
$src = 'b';
}
else if($group = 'c'){
$src = 'c';
}
else if($group ='d'){
$src = 'd';
}
return $src;
}
if($_SERVER['REQUEST_METHOD']=="POST"){
echo $branch = $_POST['branch'];
echo'<br />';
echo $pickup = $_POST['pickupDate'];
echo'<br />';
echo $return = $_POST['returnDate'];
echo'<br />';
echo $carType = $_POST['carType'];
echo'<br />';
echo $name = $_POST['uName'];
var_dump(getCarGroup($group));
var_dump($carType);
?>
<div class="row">
<div class="col-md-4">
<img src="img/cars/<?php echo getCarGroup($carType) ?>.png" class="img-responsive" />

Answer

but you can use direct $_POST['carType'] for image name

<img src="img/cars/<?php echo $_POST['carType'] ?>.png" class="img-responsive" />
Comments