Multiple Select inputs in PHP

99 Views Asked by At

I am wanting to write HTML to allow the selection of multiple select inputs and then print them in php.

Here is the problem:

favsport = Warning: Array to string conversion in I:\twa\twa220\practicals\week9\exercise2.php on line 23 Array

Here are the steps for the Question:

The forms in exercise 1 and 2 are very similar but with some important differences. These include:

  1. the selection list size has been changed to "4"
  2. multiple selections are now enabled in the selection list
  3. the action of the form has been changed to exercise2.php
  4. the form method has been changed

And here is what is needed:

  • modify exercise2.php to correctly process the form from exercise2.html. Hint: Because of the changes made to the form (in particular, the change to the form method is critical) you will need to modify several aspects of the php script.

  • Ensure that your exercise2 script is capable of displaying all of the selections made from the selection list - it won’t without appropriate modifications to both the html form and the php.

And here is my HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Week 9 Exercise 2 Form</title>
    <link rel="stylesheet" href="../css/week9Styles.css">
  </head>
  <body>
    <h1>Week 9 Exercise 2 PHP form demo</h1>
    <form id="userinfo" action="exercise2.php" method="post">
      <p>Please fill in the following form. All fields are mandatory.</p>

      <p>
        <label for="fname">First Name:</label>
        <input type="text" id="fname" name="firstname">
      </p>

      <p>
        <label for="email">Email Address:</label>
        <input type="text" id="email" name="email">
      </p>

      <p>
        <label for="addr">Postal Address:</label>
        <textarea rows="5" cols="300" id="addr" name="postaddr"></textarea>
      </p>

      <p>
        <label for="sport">Favourite sport: </label>
        <select id="sport" name="favsport[]" size="4" multiple>
            <option value="soccer">Soccer</option>
            <option value="cricket">Cricket</option>
            <option value="squash">Squash</option>
            <option value="golf">Golf</option>
            <option value="tennis">Tennis</option>
            <option value="basketball">Basketball</option>
            <option value="baseball">Baseball</option>
        </select>
      </p>

      <p>
        <label for="list">Add me to the mailing list</label>
        <input type="checkbox" id="list" name="emaillist" value="Yes">
      </p>

      <p><input type="submit" value="submit"></p>
    </form>
  </body>
</html>

And my PHP:

<!DOCTYPE html>
<html lang="en">
 <head>
 <meta charset="utf-8">
 <title>Week 9 Exercise 1</title>
 </head>
 <body>
 <?php
 //obtain the firstname input from the $_POST array
 $namestr = $_POST["firstname"];
 //obtain the values for the other input devices here
 $emailstr = $_POST["email"];
 $poststr = $_POST["postaddr"];
 $sportstr = $_POST["favsport"];
 $eliststr = $_POST["emaillist"];

 ?>
 <p>The following information was received from the form:</p>
 <p><strong>name = </strong> <?php echo "$namestr"; ?></p>
 <!--output the other form inputs here -->
 <p><strong>email = </strong> <?php echo "$emailstr"; ?></p>
 <p><strong>postaddress = </strong> <?php echo "$poststr"; ?></p>
 <p><strong>favsport = </strong> <?php echo "$sportstr"; ?></p>
 <p><strong>emaillist = </strong> <?php echo "$eliststr"; ?></p>
 </body>
 </html>

Thanks for any help =)

0

There are 0 best solutions below