Storing HTML Multiple Select values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Storing HTML Multiple Select values

Post by the9ulaire »

Here's my select:

Code: Select all

<select name="season" size="4" multiple>
      <option value="1">Spring</option>
      <option value="2">Summer</option>
      <option value="3">Fall</option>
      <option value="4">Winter</option>
</select>
For each season selected, I will store a new row (along with an id belonging to something else). How do I go about storing these values into a table?

Thanks!
lwr
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Storing HTML Multiple Select values

Post by RobertGonzalez »

You use them from the $_POST array and insert them.
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Storing HTML Multiple Select values

Post by the9ulaire »

How would I code that?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Storing HTML Multiple Select values

Post by RobertGonzalez »

Multiple value form elements send themselves as an array. So in effect you could do something like:

Code: Select all

<select name="season[]" size="4" multiple>
      <option value="1">Spring</option>
      <option value="2">Summer</option>
      <option value="3">Fall</option>
      <option value="4">Winter</option>
</select>
Use that, then var_dump($_POST) to see what is coming through. You should be able to derive what you need from that.
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Storing HTML Multiple Select values

Post by the9ulaire »

I did what you said. Looking at it, I'm not really sure how to use the array.

I did: var_dump($_POST['season']);
And got: array(2) { [0]=> string(1) "1" [1]=> string(1) "4" } (

I am not very good with arrays and don't know how to translate that information to dissect each value.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Storing HTML Multiple Select values

Post by RobertGonzalez »

That's OK. We are getting somewhere and you are learning. Let's keep this up.

Based on what you posted:

Code: Select all

array(2) 
{ 
  [0]=> string(1) "1" 
  [1]=> string(1) "4" 
}
You can surmise that both the option with value "1" was checked and the option with value "4" was checked. Now you can use the posted field name as an array and get at the values anyway you want.

Code: Select all

<?php
// Was the form posted?
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  if (! empty($_POST['season'])) {
    // Season had a value passed to it, lets get it.
    $season = $_POST['season'];
 
    foreach ($season as $key => $value) {
      // Now we can loop this array and use either the array keys
      // (which in your case are more than likely going to be useless)
      // or use the array values (which are more useful in this case)
      echo "A season of $value was submitted\n";
    }
  }
} else {
  echo 'The form was not submitted!';
}
?>
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Storing HTML Multiple Select values

Post by the9ulaire »

Here's my code (I do validate $_POST['season']):

Code: Select all

$season = $_POST['season'];
 
foreach ($season as $key => $value) {
    $sql3 = "INSERT INTO season (activity_id, season) VALUES (" . 
        $activity_id . ", " . $season . ")";
}
And I get this error:

Warning: Invalid argument supplied for foreach() in transact_activity.php on line 51
Could not redirect; Headers already sent (output).


I *think* that I'm getting the error because of $value? Is that correct? I have multiple values being posted into my trasaction_page.
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Storing HTML Multiple Select values

Post by the9ulaire »

Oh, and line 51 is where my foreach statement starts.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Storing HTML Multiple Select values

Post by RobertGonzalez »

No, you are getting that error because $season is not an array. Just before the foreach statement do a var_dump($season). The error is ok for now.
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Storing HTML Multiple Select values

Post by the9ulaire »

By the way, I really appreciate how you're helping me out. You're helping me actually learn this stuff, not just telling me what to type. Thanks.
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Storing HTML Multiple Select values

Post by the9ulaire »

It output: string(1) "4" and I had selected two things.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Storing HTML Multiple Select values

Post by RobertGonzalez »

Did you remember the "[]" in the markup?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Storing HTML Multiple Select values

Post by RobertGonzalez »

And by the way, you are welcome. I love helping people that want to learn, not just come here looking for an answer to the problem.
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Storing HTML Multiple Select values

Post by the9ulaire »

Yes, I have the [] in the HTML mark-up. There's something we're about what's going on. When I tried:

Code: Select all

<form action="test.php" method="post">
 
    <select name="season[]" size="4" multiple>
          <option value="1">Spring</option>
          <option value="2">Summer</option>
          <option value="3">Fall</option>
          <option value="4">Winter</option>
    </select>
 
    <input type="submit" name="action" value="Cancel" /> 
    <input type="submit" name="action" value="Create New Activity" />
</form>
 
</body>
</html>
 
<?php
if (!empty($_POST['season'])) {
    $season = $_POST['season'];
    
    foreach ($season as $key => $value) {
        echo "A season of $value was submitted\n";
    }
}
?>
That code works perfectly. However, when it is in the context of a much larger form:

Code: Select all

<form action="transact_activity.php" method="post">
    <table border="0">
        <tr>
            <td width="130" valign="top"><strong>Name:</strong></td>
            <td width="397"><input type="text" name="name" maxlength="255" /></td>
        </tr>
        <tr>
          <td colspan="2" valign="top" background="../images/table_line.jpg">&nbsp;</td>
        </tr>
        <tr>
            <td width="130" valign="top"><strong>Short Description:</strong></td>
            <td><input name="short_desc" type="text" size="50" maxlength="255" /></td>
        </tr>
        <tr>
            <td width="130" valign="top"><strong>Full Descriptoin:</strong></td>
            <td><textarea name="full_desc" cols="50" rows="5"></textarea></td>
        </tr>
        <tr>
            <td colspan="2" valign="top" background="../images/table_line.jpg">&nbsp;</td>
        </tr>
        <tr>
            <td width="130" valign="top"><strong>Header Picture:</strong></td>
            <td><input type="file" name="header" /></td>
        </tr>
        <tr>
            <td width="130" valign="top"><strong>Icon Picture</strong></td>
            <td><input type="file" name="icon" /></td>
        </tr>
        <tr>
            <td colspan="2" valign="top" background="../images/table_line.jpg">&nbsp;</td>
        </tr>
        <tr>
            <td width="130" valign="top"><strong>Price:</strong></td>
            <td>$
            <input name="price" type="text" maxlength="255" /></td>
        </tr>
        <tr>
            <td colspan="2" valign="top" background="../images/table_line.jpg">&nbsp;</td>
        </tr>
        <tr>
            <td width="130" valign="top"><strong>Season:</strong><br />
            (hold ctrl to select multiple values)</td>
            <td>
                <select name="season[]" size="4" multiple>
                      <option value="1">Spring</option>
                      <option value="2">Summer</option>
                      <option value="3">Fall</option>
                      <option value="4">Winter</option>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2" valign="top" background="../images/table_line.jpg">&nbsp;</td>
        </tr>
        <tr>
            <td width="130" valign="top"><strong>Location:</strong></td>
            <td>
                <select name="location"s>
                    <option value="north">North</option>
                    <option value="east">East</option>
                    <option value="south">South</option>
                    <option value="west">West</option>
                    <option value="downtown">Downtown</option>
                    </select>
            </td>
        </tr>
        <tr>
            <td width="130" valign="top"><strong>Address:</strong></td>
            <td><textarea name="address" cols="30" rows="3"></textarea></td>
        </tr>
        <tr>
            <td width="130" valign="top"><strong>Website:</strong></td>
            <td><input name="website" type="text" value="" size="30" maxlength="255" /></td>
        </tr>
        <tr>
            <td width="130" valign="top"><strong>Contact:</strong></td>
            <td><input type="text" name="contact" maxlength="255" /></td>
        </tr>
        <tr>
            <td colspan="2" valign="top" background="../images/table_line.jpg">&nbsp;</td>
        </tr>
        <tr>
            <td width="130" valign="top">&nbsp;</td>
            <td>
                <input type="submit" name="action" value="Cancel" /> 
                <input type="submit" name="action" value="Create New Activity" />
            </td>
        </tr>
  </table>
</form>
It doesn't seem to accept it as an Array.... Here is the code that processes the declaration "Create New Activity":

Code: Select all

if (isset($_POST['name']) and ($_POST['name']) != ''
    and isset($_POST['full_desc']) and ($_POST['short_desc']) != ''
    and isset($_POST['short_desc']) and ($_POST['full_desc']) != ''
    and isset($_POST['price']) and ($_POST['price']) != ''
    and isset($_POST['address']) and ($_POST['address']) != ''
    and isset($_POST['location']) and ($_POST['location']) != ''
    and isset($_POST['website']) and ($_POST['website']) != ''
    and (!empty($_POST['season']))
    and isset($_POST['contact']) and ($_POST['contact']) != '') {
  
    $price = strval($_POST['price']);
            
    //Insert values into activity table
    $sql = "INSERT INTO activity " .
            "(name, full_desc, short_desc, price, address, location, website, contact) " .
            "VALUES ('" 
            . htmlspecialchars($_POST['name']) . "','" 
            . htmlspecialchars($_POST['full_desc']) . "','"
            . htmlspecialchars($_POST['short_desc']) . "'," 
            . $price . ",'" 
            . htmlspecialchars($_POST['address']) . "','"
            . htmlspecialchars($_POST['location']) . "','" 
            . htmlspecialchars($_POST['website']) . "','" 
            . htmlspecialchars($_POST['contact']) . "')";
 
    mysql_query($sql, $conn) or die('Could not insert into ACTIVITY; ' . mysql_error());
    
    //retrieve id value from activity just entered
    $sql2 = "SELECT activity_id FROM activity WHERE name='" . htmlspecialchars($_POST['name']) .
            "' AND full_desc='" . htmlspecialchars($_POST['full_desc']) .
            "' AND short_desc='" . htmlspecialchars($_POST['short_desc']) . 
            "' AND address='" . htmlspecialchars($_POST['address']) . 
            "' AND location='" . htmlspecialchars($_POST['location']) . "'";
            
    $result = mysql_query($sql2, $conn) or die('Could not retrieve ACTIVITY_ID; ' . mysql_error());
    $row = mysql_fetch_array($result);
    $activity_id = $row['activity_id'];
 
    //Inserts into season table for each selected season
    $season = $_POST['season'];
    
    foreach ($season as $key => $value) {
        echo "A season of $value was submitted\n";
    }   
    
    redirect('panel.php?s=created&pn=' . $_POST['season']);
    } else if (($_POST['page_name']) == ''
        or ($_POST['page_desc']) == '')
    {
    redirect('panel.php?compose=yes&alert=fillallfields');
    } else {
    redirect('panel.php?s=no&pn=' . $_POST['name']);
}
 
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Storing HTML Multiple Select values

Post by the9ulaire »

Actually, in the foreach, what I want it to do is:

$sql3 = "INSERT INTO season (activity_id, season) VALUES (" . $activity_id . ", " . $season . ")";

I accidentally left in the example you'd given me when I posted that last code.
Post Reply