1

I have a <select multiple="multiple"> to get multiple choices in php. After that I put it in a variable with the POST method and the I try to at least echo the array out to ensure that my variable$authors has the choices but it prints this error

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ex\addBook.php on line 45

Here is my code:

<?php
    include ("includes/connections.php");
    $authors = $_POST["authors"];
    foreach ($authors as $author) 
    {
        echo $author;
    }
?>

here is the 'select multiple' I have in html :

<?php multiple("author_ID", "author_firstname", "author_lastname", "author", "author_lastname", "authors"); ?> <span style="font-size: 12px; color: #666;">(Keep down Ctrl for selecting multiple authors)</span>

here is the multiple function:

<?php include ("includes/connections.php");
    function multiple($intIdField, $strfNameField, $strlNameField, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc") {
       echo "<select multiple=\"multiple\" name=\"$strNameOrdinal\">\n";

       $strQuery = "select $intIdField, $strfNameField, $strlNameField
                   from $strTableName
                   order by $strOrderField $strMethod";

       $rsrcResult = mysql_query($strQuery);

       while($arrayRow = mysql_fetch_assoc($rsrcResult)) {
          $strA = $arrayRow["$intIdField"];
          $strB = $arrayRow["$strlNameField"] . " " . $arrayRow["$strfNameField"];    
          echo "<option value=\"$strA \">$strB</option>\n";
       }

       echo "</select>";
    }
?>

So I would guess that the $authors variable would be an array and I would happily at least print, but as I already said this does not happen.

The Authors that are taken from the database are showing up correctly without any problem. the thing is I want the ones that are chosen from the 'select multiple' list to be echoed out.

9
  • The $authors array isn't being populated for some reason. You really should check against this condition to keep from getting that error. Commented Jun 7, 2012 at 18:16
  • how do I populate the '$authors' as an array from a '<select multiples = "multiples"' . That could be my main issue. Do i have to write something special in the select multiples? Commented Jun 7, 2012 at 18:19
  • Try the answer on this thread: stackoverflow.com/questions/2407284/… Commented Jun 7, 2012 at 18:21
  • I am trying several times but it is not working..maybe I'm declaring the variables in my function wrong or something, I just have no idea Commented Jun 7, 2012 at 18:29
  • 1
    Can you modify this part of the multiple() function? name=\"$strNameOrdinal\"> If so, make it name=\"$strNameOrdinal[]\"> Commented Jun 7, 2012 at 18:34

1 Answer 1

1

The name of your multiple select in HTML needs to end in '[]' to denote that it is an array, then when you run your foreach loop in php, make sure that it is receiving an array, so it needs to either be set, or you need to typecast a single value as an array. I'm pretty sure your problem is in how you have the name attribute set though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.