I am battling with a php function.
I am trying to create a function that accepts 5 arguments. The user needs to select the activity of they choice, a message with the activity of their choice should be echoed back to the user.
I used an array and a foreach statement within the function but it doesn't work.
Please help
<?php
// Declare the function and variables
error_reporting (0);
$msg = "";
$activity = "";
function activites ($paintballing , $ice_skating, $horse_riding, $para_gliding, $water_rafting ){
// Make the activity array
$activites = array (1=> 'Paintballing', 'Ice_skating', 'Horse_riding', 'para_gliding', 'water_rafting');
// make the activity pull down menu
echo '<select name ="activity">';
// use a foreach loop to loop through the activity
foreach ($activites as $key =>$value) {
echo "<option value \"key\"$key\">$value</option>\n";
}
}
if ($activity == ""){
$msg = "<font color = 'red'> Please select an activity from the drop down menu</font>";
}
?>
<html>
<head>
<title>Function </title>
</head>
<body>
<center> <a href="index.php">Home </a> | <a href ="calc.php">Calc</a> | <a href="string.php">String</a> | <a href ="array.php"> Array </a> | <a href = "function.php">Function </a></center>
</center>
<form name = 'function' method = "POST" action = 'function.php' >
<p>Select one of the activity you would like to do today:</p>
<select name="Activity" size="5">
<option selected value = "">
<option> Paintballing </option>
<option> Ice-skating </option>
<option> Horse-riding </option>
<option> Para-gliding </option>
<option> Water-rafting </option>
</select>
</form>
<input type = "submit" name = "submit" value = "submit" />
</option>
<?php echo $msg;?>
<p align ="center"><a href ="function.txt" Onclick ="window.open ('function.txt', 'function', resizable =no, height=500, width=500, scrollercars=yes'); retrun false;">Function.txt</a></p>
asked Mar 22, 2013 at 17:59
Basi Manele
971 gold badge1 silver badge4 bronze badges
1 Answer 1
Try calling the function
if ($_POST['submit']) {
echo activities($_POST['Activity']);
}
Your function modified
function activities($activity) {
$activities = array(
'Paintballing',
'Ice_skating',
'Horse_riding',
'para_gliding',
'water_rafting'
);
$activity = trim($activity);
if (! in_array($activity, $activities))
return false;
return sprintf("You have selected : %s", $activity);
}
answered Mar 22, 2013 at 18:05
Baba
95.3k29 gold badges172 silver badges222 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Pedro Cordeiro
Also,
activites is activities misspelled, you might want to fix that while your code's small.Baba
@PedroCordeiro .. yeah i know ... i should have corrected it myself ... :)
lang-php
$activites = array (1=> $paintballing, $ice_skating, $horse_riding', $para_gliding', $water_rafting');$activites = array ('Paintballing', ...or$activites = array (1=> array('Paintballing', ...Also, You should be getting error messages no?