0

我正在与一个 php 函数作斗争。

我正在尝试创建一个接受 5 个参数的函数。用户需要选择他们选择的活动,带有他们选择的活动的消息应该回显给用户。

我在函数中使用了一个数组和一个 foreach 语句,但它不起作用。

请帮忙

<?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>
4

1 回答 1

1

尝试调用函数

if ($_POST['submit']) {
    echo activities($_POST['Activity']);
}

您的功能已修改

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);
}
于 2013-03-22T18:05:49.567 回答