Jump to content

Checkbox array help


laurajohn89

Recommended Posts

Hi, I've searched everywhere and had no luck...

 

I have a form where users tick checkboxes to enter horse riding event types.  I need to store these types on my database and display them in a table.

 

However when I look at the results I get the word 'array' under the type column in my database.

 

Thank you very much for your help.

 

 

HTML code:

 

<form action="insert.php" method="post">

 

<table width="400"  border="0" cellspacing="0" cellpadding="2">

  <tr>

    <td width="200">Title: </td>

    <td><input type="text" name="title" /> </td>

  </tr>

<td width="200">Type of Event: </td>

<td><p class="contentclass"><input type="checkbox" name="type[combined training]" id="type[combined training]" value="1" />Combined Training</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[dressage]" id="type[dressage]" value="1" />Dressage</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[demos/trials]" id="type[demos/trials]" value="1" />Demos / Trials</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[driving]" id="type[driving]" value="1" />Driving</p></td>

</tr><tr>

    <td><p class="contentclass"><input type="checkbox" name="type[endurance]" id="type[endurance]" value="1" />Endurance</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[eventing]" id="type[eventing]" value="1" />Eventing</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[ex-racehorse classes]" id="type[ex-racehorse classes]" value="Ex-Racehorse Classes" />Ex-Racehorse lasses</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[fun/sponsored]" id="type[fun/sponsored]" value="Fun / Sponsored" />Fun / Sponsored</p></td>

</tr><tr>

    <td><p class="contentclass"><input type="checkbox" name="type[hunting]" id="type[hunting]" value="Hunting" />Hunting</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[jumpcross]" id="type[jumpcross]" value="Jumpcross" />Jumpcross</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[ride]" id="type[ride]" value="Ride" />Ride</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[sale/auction]" id="type[sale/auction]" value="Sale/Auction" />Sale/Auction</p></td>

    </tr><tr>

<td><p class="contentclass"><input type="checkbox" name="type[show jumping]" id="type[show jumping]" value="Show Jumping" />Show Jumping</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[shows/mixed discplins]" id="type[shows/mixed discplins]" value="Shows / Mixed Discplins" />Shows / Mixed Discplins</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[training clinic]" id="type[training clinic]" value="Training Clinic" />Training Clinic</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[trec]" id="type[trec]" value="Trec" />Trec</p></td>

    </tr><tr>

<td><p class="contentclass"><input type="checkbox" name="type[other]" id="type[other]" value="Other" />Other</p></td>

<tr>

</tr>

  </tr>

  <tr>

 

 

 

 

PHP code:

$sql="INSERT INTO events (title, type, place, address, date, time, website, contact, additional)

VALUES

('$_POST[title]','$_POST[type]','$_POST[place]','$_POST[address]','$_POST[date]','$_POST[time]','$_POST[website]','$_POST[contact]','$_POST[additional]')";

 

 

thanks in advanced :)

Link to comment
https://forums.phpfreaks.com/topic/192635-checkbox-array-help/
Share on other sites

and now I get this error

 

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'combinedtraining'] Array['demostrails'] Array['dressage'] Array['driving'] Array' at line 4

Link to comment
https://forums.phpfreaks.com/topic/192635-checkbox-array-help/#findComment-1015626
Share on other sites

Hi, I've searched everywhere and had no luck...

 

I have a form where users tick checkboxes to enter horse riding event types.  I need to store these types on my database and display them in a table.

 

However when I look at the results I get the word 'array' under the type column in my database.

 

Thank you very much for your help.

 

 

HTML code:

 

<form action="insert.php" method="post">

 

<table width="400"  border="0" cellspacing="0" cellpadding="2">

  <tr>

    <td width="200">Title: </td>

    <td><input type="text" name="title" /> </td>

  </tr>

<td width="200">Type of Event: </td>

<td><p class="contentclass"><input type="checkbox" name="type[combined training]" id="type[combined training]" value="1" />Combined Training</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[dressage]" id="type[dressage]" value="1" />Dressage</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[demos/trials]" id="type[demos/trials]" value="1" />Demos / Trials</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[driving]" id="type[driving]" value="1" />Driving</p></td>

</tr><tr>

    <td><p class="contentclass"><input type="checkbox" name="type[endurance]" id="type[endurance]" value="1" />Endurance</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[eventing]" id="type[eventing]" value="1" />Eventing</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[ex-racehorse classes]" id="type[ex-racehorse classes]" value="Ex-Racehorse Classes" />Ex-Racehorse lasses</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[fun/sponsored]" id="type[fun/sponsored]" value="Fun / Sponsored" />Fun / Sponsored</p></td>

</tr><tr>

    <td><p class="contentclass"><input type="checkbox" name="type[hunting]" id="type[hunting]" value="Hunting" />Hunting</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[jumpcross]" id="type[jumpcross]" value="Jumpcross" />Jumpcross</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[ride]" id="type[ride]" value="Ride" />Ride</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[sale/auction]" id="type[sale/auction]" value="Sale/Auction" />Sale/Auction</p></td>

    </tr><tr>

<td><p class="contentclass"><input type="checkbox" name="type[show jumping]" id="type[show jumping]" value="Show Jumping" />Show Jumping</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[shows/mixed discplins]" id="type[shows/mixed discplins]" value="Shows / Mixed Discplins" />Shows / Mixed Discplins</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[training clinic]" id="type[training clinic]" value="Training Clinic" />Training Clinic</p></td>

<td><p class="contentclass"><input type="checkbox" name="type[trec]" id="type[trec]" value="Trec" />Trec</p></td>

    </tr><tr>

<td><p class="contentclass"><input type="checkbox" name="type[other]" id="type[other]" value="Other" />Other</p></td>

<tr>

</tr>

  </tr>

  <tr>

 

 

 

 

PHP code:

$sql="INSERT INTO events (title, type, place, address, date, time, website, contact, additional)

VALUES

('$_POST[title]','$_POST[type]','$_POST[place]','$_POST[address]','$_POST[date]','$_POST[time]','$_POST[website]','$_POST[contact]','$_POST[additional]')";

 

 

thanks in advanced :)

 

Dear Laura,

It because $_POST, must be outside the string, otherwise will no work, and the title must be like $_POST['title'] . If this not work you can check all variables submitted with echo phpinfo() at the top of your file  . It will display all data you need.

Link to comment
https://forums.phpfreaks.com/topic/192635-checkbox-array-help/#findComment-1015639
Share on other sites

thanks but I've sorted it now.  Another forum helped.  I inserted

 

$type = implode(",",$_POST['type']);

   

 

      for($i = 0; $i < sizeof($type); $i++) {

 

      $query = "INSERT INTO events (type) VALUES ('".$type."')";

 

      $result = mysql_query($query) or die(mysql_error());

    }

 

      mysql_close($dbh);

 

 

      // Echo to the browser.

 

 

      if(count($type) == 0){echo "<p>No Checkbox selected !</p>";}

 

      if (count($type) > 0) {

 

      for ($i=0;$i<count($type);$i++) {

 

      echo "<li>$type[$i] \n";

 

      }

 

    }

 

and its fine.  thanks :)

Link to comment
https://forums.phpfreaks.com/topic/192635-checkbox-array-help/#findComment-1015730
Share on other sites

Archived

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

×
×
  • Create New...

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.