Jump to content

Archived

This topic is now archived and is closed to further replies.

mymilkshake

Checkboxes+Php/mysql

Recommended Posts

Hello,

I have many checkboxes in my form(php) for diffrent selections a user can make. I would like to make them into one array if possible and put it into a mysql table. I have experimented with serialize, unserialize, with no success. I was wondering if someone could point me in the right direction, thanks.

echo "<form action=\"post.php\" method=\"post\">\n";
echo "<fieldset>\n";
echo "<legend>\n";
echo "Lister Information\n";
echo "</legend>\n";
echo "<label for=\"listername\">Lister Name(s):</label><input type=\"text\" name=\"listername\"/><br /><br />\n";
echo "<label for=\"listerdayphone\">Lister Day Phone#:</label><input type=\"text\" name=\"listerdayphone\"/><br /><br />\n";
echo "<label for=\"listernightphone\">Lister Night Phone#:</label><input type=\"text\" name=\"listernightphone\"/><br /><br />\n";
echo "</fieldset>\n";
echo "<fieldset>\n";
echo "<legend>\n";
echo "House Information\n";
echo "</legend>\n";
echo "<label for=\"headline\">Headline:</label><input type=\"text\" name=\"headline\" /><br /><br />\n";
echo "<label for=\"price\">Price:</label><input type=\"text\" name=\"price\" /><br /><br />\n";
echo "<label for=\"address1\">Property Address1:</label><input type=\"text\" name=\"address1\" /><br /><br />\n";
echo "<label for=\"address2\">Property Address2:</label><input type=\"text\" name=\"address2\" /><br /><br />\n";
echo "<label for=\"town\">Town:</label><input type=\"text\" name=\"town\" /><br /><br />\n";
echo "<label for=\"province\">Province:</label><select name=\"province\" ><option selected>B.C<option>A.B</select><br /><br />\n";
echo "</fieldset>\n";
echo "<fieldset>\n";
echo "<legend>\n";
echo "Property Description\n";
echo "</legend>\n";
echo "<label for=\"highlight1\">High Light Bullet1:</label><input type=\"text\" name=\"highlight1\" /><br /><br />\n";
echo "<label for=\"highlight2\">High Light Bullet2:</label><input type=\"text\" name=\"highlight2\" /><br /><br />\n";
echo "<label for=\"highlight3\">High Light Bullet3:</label><input type=\"text\" name=\"highlight3\" /><br /><br />\n";
echo "<label for=\"longdescription\">Long Description:</label><input type=\"text\" name=\"longdescription\" /><br /><br />\n";
echo "</fieldset>\n";
echo "<fieldset>\n";
echo "<legend>\n";
echo "House Features\n";
echo "</legend>\n";
echo "<table>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Acerage\" />Acerage</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Farm\" />Farm</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Irrigation\" />Irrigation</td>\n";
echo "\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Landscaped\" /> Landscaped</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Established Fruit Trees\" />Established Fruit Trees</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Fenced\" />Fenced</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Barn\" />Barn</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"RV Parking\" />RV Parking</td>\n";
echo "\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Kennel\" />Kennel</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Storage Shed\" />Storage Shed</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Greenhouse\" />Greenhouse</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Lake View\" />Lake View</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"View\" /> View</td>\n";
echo "\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Waterfront\" /> Waterfront</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Deck\" />Deck</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Balcony\" />Balcony</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Sun Room\" />Sun Room</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Patio\" />Patio</td>\n";
echo "\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Sky Lights\" />Sky Lights</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Vaulted Ceilings\" />Vaulted Ceilings</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Walk-in Closet\" />Walk-in Closet</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"En Suit\" />En Suit</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Pantry\" />Pantry</td>\n";
echo "\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Island Kitchen\" />Island Kitchen</td>\n";
echo "</tr>\n";
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Bus Route\" />Bus Route</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Work Shop\" />Work Shop</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Cul de Sac\" />Cul de Sac</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"School Nearby\" />School Nearby</td>\n";
echo "\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"In-Law Suit or Rental Suit\" />In-Law Suit or Rental Suit</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Handicap Access\" /> Handicap Access</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Wood Fireplace\" />Wood Fireplace</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Gas Fireplace\" /> Gas Fireplace</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Built in Vac\" />Built in Vac</td>\n";
echo "\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Air Conditioning\" />Air Conditioning</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Network Wiring\" />Network Wiring</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Security System\" />Security System</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Window Convering Included\" />Window Convering Included</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Built in Speakers\" />Built in Speakers</td>\n";
echo "\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Jetted or Soaker Tub\" />Jetted or Soaker Tub</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Pool\" />Pool</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Hot tub\" />Hot tub</td>\n";
echo "<td><input type=\"checkbox\" name=\"features[]\" value=\"Sauna\" />Sauna</td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "</fieldset>\n";
echo "<input type=\"submit\" value=\"Send\">\n";
echo "</form>\n";

Share this post


Link to post
Share on other sites
<input type="checkbox" name="features[]" value="Sauna">

is correct how you process it into the database is where you are probably having the trouble.

[code]
//<?php blah blah would be before the first lines..

$String3a = implode(", ", $_POST['features']);
$String3b = implode(", ", $_POST['somethingelse']);
$String3c = implode(", ", $_POST['anotherone]);

//other stuff might be here

$sql = mysql_query("INSERT INTO yourtablename (features, somethingelse, anotherone)
                     VALUES('$String3a', '$String3b', '$String3c')")
                     or die (mysql_error());



  if(!$sql){
    echo 'A database error occured while updating your choices. Please contact the Web site owner.';
    } else {

      include 'nextformpage';
}
}
  ?>
[/code]

Hope that helps

Share this post


Link to post
Share on other sites
[!--quoteo(post=387324:date=Jun 23 2006, 08:17 PM:name=phpstuck)--][div class=\'quotetop\']QUOTE(phpstuck @ Jun 23 2006, 08:17 PM) [snapback]387324[/snapback][/div][div class=\'quotemain\'][!--quotec--]
<input type="checkbox" name="features[]" value="Sauna">

is correct how you process it into the database is where you are probably having the trouble.

[code]
//<?php blah blah would be before the first lines..

$String3a = implode(", ", $_POST['features']);
$String3b = implode(", ", $_POST['somethingelse']);
$String3c = implode(", ", $_POST['anotherone]);

//other stuff might be here

$sql = mysql_query("INSERT INTO yourtablename (features, somethingelse, anotherone)
                     VALUES('$String3a', '$String3b', '$String3c')")
                     or die (mysql_error());
  if(!$sql){
    echo 'A database error occured while updating your choices. Please contact the Web site owner.';
    } else {

      include 'nextformpage';
}
}
  ?>
[/code]

Hope that helps
[/quote]

Thank-you, this makes sense. How would I retrieve this information from the database after it is stored? Could I make this information into a table of sorts?

Share this post


Link to post
Share on other sites
$String3a = implode(", ", $_POST['features']);

Right after implode this line tells it to store it in the database with a comma in between each entry. If you want to pull it out and echo it as all the chosen features listed and shown with just a comma in between, just pull it out as you would any other data. If you want to reseperate into individual features all in thier own table cell in HTML you would have to EXPLODE the data first.

A simple example if you want it all laid out in one section with comma's between each choice:

[code]
$contlist=mysql_query(
        "SELECT * FROM yourtable WHERE username='$username'");

/*I added username incase you are breaking each down, you can take it out if you don't need it.*/

while ($all = mysql_fetch_array($contlist)) {
$features = $all['features'];
$somethingelse = $all['somethingelse'];
$anotherone = $all['anotherone'];
}

echo "Features Chosen: " . $features . "<i>END</i>";
[/code]

Hope it helps,

Rob

Share this post


Link to post
Share on other sites

Thank you for the reply. How would i explode it? Just take the implode(",",) and replace implode with explode?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.