0

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 =)

iHamishio
  • 1
  • 3
  • 1
    You haven't actually asked a question. You've posted your assignment and some code, but you've not said what you want us to do with it. We won't mark your homework for you. If there's a problem, explain what it is. If not, what's it doing here? – Tangentially Perpendicular May 09 '22 at 02:57
  • Sorry I meant to send the problem with the question but forgot. Here it is: favsport = Warning: Array to string conversion in I:\twa\twa220\practicals\week9\exercise2.php on line 23 Array – iHamishio May 09 '22 at 03:00
  • Because you have `name="favsport[]"` that turns `$_POST["favsport"]` into an array. You have to handle this in your code. eg to print you could do `` – Scuzzy May 09 '22 at 03:02
  • @Scuzzy Thanks, that worked. Could you possibly explain a bit as to why that worked and what imlode means? – iHamishio May 09 '22 at 03:11
  • Your variable `$sportstr` is an array of string values, the [implode()](https://www.php.net/manual/en/function.implode.php) function joins the strings together with a string separator, this is the easiest way to print this data. ideally, however you need to process this array data should be done before printing, PHP doesn't handle echoing an array automatically. – Scuzzy May 09 '22 at 03:44

0 Answers0