0

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
5
  • What's your error message? Commented Mar 22, 2013 at 18:02
  • 1
    $activites = array (1=> $paintballing, $ice_skating, $horse_riding', $para_gliding', $water_rafting'); Commented Mar 22, 2013 at 18:03
  • 2
    Do you actually call this function somewhere? Commented Mar 22, 2013 at 18:04
  • @Allendar is saying that is not correct syntax. Need either $activites = array ('Paintballing', ... or $activites = array (1=> array('Paintballing', ... Also, You should be getting error messages no? Commented Mar 22, 2013 at 18:04
  • His array is fine the way he defined it. He is just setting the first numeric key and the keys after that will be 2, 3, 4. He is just initializing they array starting at 1 instead of 0. Commented Mar 22, 2013 at 18:09

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Also, activites is activities misspelled, you might want to fix that while your code's small.
@PedroCordeiro .. yeah i know ... i should have corrected it myself ... :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.