#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Writing Functions - Display results from drop down list


    I am very very new to PHP. I am creating a function which must accept 5 arguments on a form. I created a form that accepts 5 arguments, each via a drop-down menu, and I am finding it difficult to echo the results back to the user as it only displays the first letter of the item that I selected from the drop down list. Any help is highly appreciated.

    Here is my code:

    <?php
    $cinema = array ("The Pavillion","Suncoast Casino","The La Lucia Mall","Gateway Shopping Centre","The Wheel Shopping Centre") ;
    $type = array("Action","Comedy","Horror","Thriller","Animation");
    $movie = array("Scream","Wrong Turn","The Ice Age","Fast and Furious","No Pain No Gain");
    $times = array("13h00","15h00","17h00","19h00","21h00");
    $payment = array("Cash","Cheque","Voucher","Debit Card","Credit Card");


    function createDropdown($arr, $frm) {

    echo '<select id="'.$frm.'" name="'.$frm.'"><option value="">Select one…</option>';

    foreach ($arr as $key => $value) {

    echo '<option value="'.$value.'">'.$value.'</option>';

    }

    echo '</select>';

    }

    ?>


    <form id="form1" method="POST" name="form1" action="">
    <p align="center">
    <label for="frmcinema"><b>Please Select a Cinema:</b></label><br>
    <?php
    createDropdown($cinema, 'frmcinema')
    ?>
    <br><br>

    <label for="frmtype"><b>Please Select a Movie Type:</b></label><br>
    <?php
    createDropdown($type, 'frmtype');
    ?>
    <br><br>

    <label for="frmmovie"><b>Please Select a Movie:</b></label><br>
    <?php
    createDropdown($movie, 'frmmovie');
    ?>
    <br><br>

    <label for="frmtimes"><b>Please Select a Time:</b></label><br>
    <?php
    createDropdown($times, 'frmtimes');
    ?>
    <br><br>

    <label for="frmpayment"><b>Please Select a Payment Type:</b></label><br>
    <?php
    createDropdown($payment, 'frmpayment');
    ?>
    <br><br>
    <input type="submit" name = "submit" value="Submit Selection"> <br><br>

    <?php
    if (isset($_POST['submit'])) {
    $cinema = $_POST["frmcinema"];
    $type = $_POST["frmtype"];
    $movie = $_POST["frmmovie"];
    $times = $_POST["frmtimes"];
    $payment = $_POST["frmpayment"];
    $how_many = count(" ");


    for ($i=0; $i<$how_many; $i++) {
    echo "Your Cinema : " . $cinema[$i] . '<br>';
    echo "Your Movie Type : " . $type[$i] . '<br>';
    echo "Your Movie : " . $movie[$i] . '<br>';
    echo "Your Time : " . $times[$i] . '<br>';
    echo "Your Payment Method : " . $payment[$i] . '<br>';
    }
    echo "<br><br>";
    }
    ?>
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,921
    Rep Power
    1045
    Hi,

    there's a big notice right above your thread saying READ THIS BEFORE POSTING. Since you obviously overlooked it twice, please read it now (and then wrap your code in [ PHP ] tags to make it readable).

    The code itself doesn't really make sense to me. What is $how_many = count(" "); supposed to do? You cannot count a string (the result is always 1). Maybe you wanted the length of the string? But this still makes no sense.

    You also need to understand that $cinema[$i] is the $i-th character of the $cinema string. Probably not what you want. If you want the string itself, that's simply $cinema (no $i or anything).

    Last but not least, your code is wide open to JavaScript injection attacks. You need to read up on security.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo