简体   繁体   中英

PHP using the select tag

I would like help with my homework.
I need to use a select menu and then put the selection of the select tag into a function. Now when I tried doing this I get an undefined error (Which is weird since I did define it) I have tried to do the full if system. But I soon gave up because it was just way to much work, while there is probbaly a better way of doing this. Here is my code:

<form action=<?php echo $_SERVER['PHP_SELF'];?>>
            Vertrek:
            <select name="vertrek">
                <option value="amsterdam">Amsterdam</option>
                <option value="utrecht">Utrecht</option>
                <option value="denhaag">Den Haag</option>
                <option value="rotterdam">Rotterdam</option>
            </select>
            Aankomst:
            <select name="aankomst">
                <option value="amsterdam2">Amsterdam</option>
                <option value="utrecht2">Utrecht</option>
                <option value="denhaag2">Den Haag</option>
                <option value="rotterdam2">Rotterdam</option>
            </select>
            <br/><br/>
            <input type="submit" value="Berekenen">
            <p>------</p>
            De berekende reiskosten zijn:
            <?php 
                reiskosten(isset($_POST['vertrek']), isset($_POST['aankomst']));            
            ?> Euros's

And the "reiskosten" function:

<?php
            function reiskosten($vertrek, $bestemming)
            {
                $reiskosten = array();
                $reiskosten[1] = array();
                $reiskosten[2] = array();
                $reiskosten[3] = array();
                $reiskosten[4] = array();

                //Amsterdam naar iets
                $reiskosten[1][1] = 0;
                $reiskosten[1][2] = 30;
                $reiskosten[1][3] = 60;
                $reiskosten[1][4] = 90;

                //Utrecht naar iets
                $reiskosten[2][1] = 30;
                $reiskosten[2][2] = 0;
                $reiskosten[2][3] = 40;
                $reiskosten[2][4] = 20;

                //Den Haag naar iets
                $reiskosten[3][1] = 60;
                $reiskosten[3][2] = 40;
                $reiskosten[3][3] = 0;
                $reiskosten[3][4] = 10;

                //Rotterdam naar iets
                $reiskosten[4][1] = 90;
                $reiskosten[4][2] = 20;
                $reiskosten[4][3] = 10;
                $reiskosten[4][4] = 0;

                echo($reiskosten[$vertrek][$bestemming] . " Euro's ");
            }
        ?>

Your values are strings no integers. $reiskosten[$vertrek][$bestemming] is not defined since its ie. $reiskosten['rotterdam']['rotterdam2'] is not defined. You might want to change your array keys to strings or the option value to integers.

your form is GET . You are checking for POST variables.

The first thing you'll want to fix is that you're calling your reiskosten() function with the wrong arguments. What you mean to do is pass form values to that function, but if you look carefully, you'll see that you're passing the return values of the isset() function, each of which is going to be a boolean (TRUE/FALSE) value, not the value from the form element itself. That said, once you fix that, you'll realize that even the value of the form element is not quite what you'll want either. At that point you'll either adjust what the value="" attributes contain, or re-evaluate the way you're indexing things on the back end. Good luck!

Your form should be:

<form action=<?php echo $_SERVER['PHP_SELF'];?>>
        Vertrek:
        <select name="vertrek">
            <option value="1">Amsterdam</option>
            <option value="2">Utrecht</option>
            <option value="3">Den Haag</option>
            <option value="4">Rotterdam</option>
        </select>
        Aankomst:
        <select name="aankomst">
            <option value="1">Amsterdam</option>
            <option value="2">Utrecht</option>
            <option value="3">Den Haag</option>
            <option value="4">Rotterdam</option>
        </select>
        <br/><br/>
        <input type="submit" value="Berekenen">
        <p>------</p>
        De berekende reiskosten zijn:
        <?php 
            $reiskosten = reiskosten($_POST['vertrek'], $_POST['aankomst']);
            echo $reiskosten . "Euros's";
        ?>
  1. option values should (in this instance) be integers.
  2. remove isset() as this only checks if they are set and does not actually return their values.
  3. Create a new variable ($reiskosten) which will be the returned result of the function, and echo it out.

Your PHP should be:

<?php
        function reiskosten($vertrek, $bestemming)
        {
            $reiskosten = array();
            $reiskosten[1] = array();
            $reiskosten[2] = array();
            $reiskosten[3] = array();
            $reiskosten[4] = array();

            //Amsterdam naar iets
            $reiskosten[1][1] = 0;
            $reiskosten[1][2] = 30;
            $reiskosten[1][3] = 60;
            $reiskosten[1][4] = 90;

            //Utrecht naar iets
            $reiskosten[2][1] = 30;
            $reiskosten[2][2] = 0;
            $reiskosten[2][3] = 40;
            $reiskosten[2][4] = 20;

            //Den Haag naar iets
            $reiskosten[3][1] = 60;
            $reiskosten[3][2] = 40;
            $reiskosten[3][3] = 0;
            $reiskosten[3][4] = 10;

            //Rotterdam naar iets
            $reiskosten[4][1] = 90;
            $reiskosten[4][2] = 20;
            $reiskosten[4][3] = 10;
            $reiskosten[4][4] = 0;

            $return = $reiskosten[$vertrek][$bestemming];

            return $return;
        }
    ?>

No need to echo out here, just return the value.

The isset() function returns true or false , so the following line is incorrect since you appear to want to send the values through:

reiskosten(isset($_POST['vertrek']), isset($_POST['aankomst']));

Change it to:

if( isset($_POST['vertrek']) && isset($_POST['aankomst']) ) {
  reiskosten($_POST['vertrek'], $_POST['aankomst']);
}

Also, the values for vertrek and aankomst will be whatever the value field in the HTML option is, so it will be a name, not a number.

You can address it by using associative arrays like:

$reiskosten = array();
$reiskosten['amsterdam'] = array();
$reiskosten['utrecht'] = array();
$reiskosten['denhaag'] = array();
$reiskosten['rotterdam'] = array();

And the same for the sub-array definitions where you set the prices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM