Is thier a special way to get a check form to work? I have built a form that have checkboxes in it. Now everything in the form workins and submits to the DB properly except the actually checks. now I have a checkbox that says other and then a text box attached to it, if you type text in it it works, but the checkboxes don't. So I am not sure what I am doing wrong. Here is the php script for below and the link to it so you can see how the manner in how the boxes are being used.

http://www.texascampingforum.com/2006/index.php?page=submit

<?
include("dbinfo.inc.php");

$str_type = $_POST['type']; 
$str_type = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_type);

$str_name = $_POST['name']; 
$str_name = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_name);

$str_city = $_POST['city']; 
$str_city = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_city);

$str_activities = $_POST['activities'];  
$str_activities = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_activities); $str_URL = $_POST['URL']; $str_URL = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_URL); $str_description = $_POST['description']; $str_description = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_description); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = mysql_query("INSERT INTO parks (type, name, city, activities, URL, description) VALUES ('$str_type','$str_name','$str_city','$str_activities','$str_URL','$str_description')")or die (mysql_error()); mysql_close(); ?>

here are the checkboxes.

<FORM  METHOD="POST" ACTION="?page=includes/submit">
  <p><INPUT TYPE="checkbox" NAME="activities" 
VALUE="Camping"</font></span><font size="2"> Camping </font>
  <INPUT TYPE="checkbox" NAME="activities"
VALUE="RV's"</font></span><font size="2"> RVing </font>
  <INPUT TYPE="checkbox" NAME="activities"
VALUE="Hiking"</font></span><font size="2"> Hiking </font>
  <INPUT TYPE="checkbox" NAME="activities"
VALUE="Boating"</font></span><font size="2"> Boating </font>
  <INPUT TYPE="checkbox" NAME="activities"
VALUE="Fishing"</font></span><font size="2"> Fishing </font>
  <INPUT TYPE="checkbox" NAME="activities"
VALUE="Hunting"</font></span><font size="2"> Hunting </font>
  <INPUT TYPE="checkbox" NAME="activities"
VALUE="Wildlife"</font></span><font size="2"> Wildlife Watching </font>
  <INPUT TYPE="checkbox" NAME="activities"
VALUE="picnic/playground"</font></span><font size="2"> Picnic Areas/Playground </font><INPUT TYPE="checkbox" NAME="activities"
VALUE="geocaching"</font></span><font size="2"> Geocaching </font><INPUT TYPE="checkbox" NAME="activities"
VALUE="Other"</font></span><font size="2"> Other, please explain: </font><INPUT TYPE="text" NAME="activities" SIZE="19">

-Thanks in advance for any help or suggestions.

    Ok, you got some errors in the code with no </input>, check them up first.

    IF you fix them up,you have got two choices to make the checkbox work.

    first, using different names for each checkbox and checking for thier value in the submit page. which will increase the code u need to write as the no of checkboxes increase.

    second, u can use an array of checkbox i.e name="activities[]", and use an for loop to get each value of the selected activities in the submit form. Its easy one and doesnt increase with the increase in the no of checkboxes.

    for u r idea check this one out:
    http://www.plus2net.com/php_tutorial/array_checkbox.php

    Hope it solves u r problem.

      Ok thank you, that makes sense and I was wondering if the naming was an issue but wasn't sure. Would I be correct to say it is the reason why everything before the checkboxes are nothing being entered because thier are errors within the checkboxes?

      -Thanks Again

        Ok I am still having some troubles with this. I have made the suggested change but still nothing is working. I would greatly appreicate the help.

        Here is the form, yes I am aware it's missing the ending </form? tag. It's further down on the page the form resides.

        <FORM  METHOD="POST" ACTION="?page=includes/submit">
          <p><INPUT TYPE="checkbox" NAME="activities[Camping]" 
        VALUE="Camping"</font></span><font size="2"> Camping </font>
          <INPUT TYPE="checkbox" NAME="activities[RVing]"
        VALUE="RV's"</font></span><font size="2"> RVing </font>
          <INPUT TYPE="checkbox" NAME="activities[Hiking]"
        VALUE="Hiking"</font></span><font size="2"> Hiking </font>
          <INPUT TYPE="checkbox" NAME="activities[Boating]"
        VALUE="Boating"</font></span><font size="2"> Boating </font>
          <INPUT TYPE="checkbox" NAME="activities[Fishing]"
        VALUE="Fishing"</font></span><font size="2"> Fishing </font>
          <INPUT TYPE="checkbox" NAME="activities[Hunting]"
        VALUE="Hunting"</font></span><font size="2"> Hunting </font>
          <INPUT TYPE="checkbox" NAME="activities[Wildlife]"
        VALUE="Wildlife"</font></span><font size="2"> Wildlife Watching </font>
          <INPUT TYPE="checkbox" NAME="activities[Picnic Areas/Playground]"
        VALUE="picnic/playground"</font></span><font size="2"> Picnic Areas/Playground </font><INPUT TYPE="checkbox" NAME="activities[Geocaching]"
        VALUE="geocaching"</font></span><font size="2"> Geocaching </font><INPUT TYPE="checkbox" NAME="activities[Other]"
        VALUE="Other"</font></span><font size="2"> Other, please explain: </font><INPUT TYPE="text" NAME="activities" SIZE="19">
                          </font>

        Here is the php

        <?
        include("dbinfo.inc.php");
        
        $str_type = $_POST['type'];
        $str_type = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_type);
        
        $str_name = $_POST['name'];
        $str_name = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_name);
        
        $str_city = $_POST['city'];
        $str_city = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_city);
        
        $str_activities = $_POST['activities'];  
        $str_activities = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_activities); $str_URL = $_POST['URL']; $str_URL = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_URL); $str_description = $_POST['description']; $str_description = preg_replace("#\<.+?\>(.+?)\</.+?\>#is", "\\1", $str_description); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = mysql_query("INSERT INTO parks (type, name, city, activities, URL, description) VALUES ('$str_type','$str_name','$str_city','$str_activities','$str_URL','$str_description')")or die (mysql_error()); mysql_close(); ?>

        Now everything is getting entered into the DB correctly with the exception of the vaules of each checkbox. I don't know if this is a PHP error or form error.

        -Thanks in advance.

          ok, you need to use this:

          p><INPUT TYPE="checkbox" NAME="activities[ ]"
          VALUE="Camping"</font></span><font size="2"> Camping </font>

          because u r already adding all the values into the array that could be causing the error u r getting there.

          try this. hope it solves u r prob

            Thanks for the help, it did has you suggested. All It says in the DB is "Array"

            here is the form with the changes

            <FORM  METHOD="POST" ACTION="?page=includes/submit">
              <p><INPUT TYPE="checkbox" NAME="activities[ ]" 
            VALUE="Camping"</font></span><font size="2"> Camping </font>
              <INPUT TYPE="checkbox" NAME="activities[ ]"
            VALUE="RV's"</font></span><font size="2"> RVing </font>
              <INPUT TYPE="checkbox" NAME="activities[ ]"
            VALUE="Hiking"</font></span><font size="2"> Hiking </font>
              <INPUT TYPE="checkbox" NAME="activities[ ]"
            VALUE="Boating"</font></span><font size="2"> Boating </font>
              <INPUT TYPE="checkbox" NAME="activities[ ]"
            VALUE="Fishing"</font></span><font size="2"> Fishing </font>
              <INPUT TYPE="checkbox" NAME="activities[ ]"
            VALUE="Hunting"</font></span><font size="2"> Hunting </font>
              <INPUT TYPE="checkbox" NAME="activities[ ]"
            VALUE="Wildlife"</font></span><font size="2"> Wildlife Watching </font>
              <INPUT TYPE="checkbox" NAME="activities[ ]"
            VALUE="picnic/playground"</font></span><font size="2"> Picnic Areas/Playground </font><INPUT TYPE="checkbox" NAME="activities[ ]"
            VALUE="geocaching"</font></span><font size="2"> Geocaching </font><INPUT TYPE="checkbox" NAME="activities[ ]"
            VALUE="Other"</font></span><font size="2"> Other, please explain: </font><INPUT TYPE="text" NAME="activities[ ]" SIZE="19">
                              </font>

            -Thanks

              That's because it is an array. You'll need to extract the values from it before adding them to the database.

              And take out all those stupid and unneccessary <font> and <span> tags. You must have used some crap code generater like FontPage to end up with all of them in there.

                I am sorry, I don't know what you mean by "extract the values from it"?

                  Same way you extract the values from the POST array, by index or key. Look up arrays in the manual and the functions for manipulating them.

                    Are you referring to this?

                    <?php
                    $arr = array("activities" => array(1 => Camping, 2 => RVing, 3 => Hiking, 4 => Boating, 5 => Fishing, 6 => Hunting, 7 => WildlifeWatching, 8 => PicnicAreasPlayground, 9 => Geocaching));
                    ?>

                    Is so where do I insert this? With the form or in the php code that inserts to the DB?

                    -Thanks

                      OK :rolleyes:

                      You have a group of checkboxes named 'activities[]' (and there should not be a gap between [ and ] ).
                      If the user clicks at least 1 checkbox then the form will post the array called 'activities'. This will contain the values for all of the checkboxes clicked.
                      How you access the contents depends on what you want to do with the data.

                      // check to see if any activities were clicked
                      if (isset($_POST['activities'])) {
                      
                      // check to see if it really is an array
                      if (is_array($_POST['activities'])) {
                      
                      // want a comma seperated list then use implode()
                      $str_activities = implode(',' , $_POST['activities']);
                      
                      // want to process each element one at a time
                      foreach($_POST['activities'] as $val) {
                         echo $val;
                      }
                      
                      // want to test if a specific box was clicked
                      if (in_array('camping', $_POST['activities'])) {
                      
                      

                        ok Thanks I understand that, but where does this need to be placed. I have two files, one that contains the HTML with the form. and another that the form calls and inserts all the info into the DB. So while all of this is making sense I do not know where to put the suggested code and I have not found a tut that explains otherwise.

                        The form can been seen in the link of my first post. Just so were on the same page. I want the value of each checkbox entered into the DB should it be click. I.E. Camping and fishing is checked I want it to be displayed as Camping, Fishing. in the DB and so on.

                        -Thanks

                          In the form that is being called...ie. <<< I have two files, one that contains the HTML with the form. and "another that the form calls " and inserts all the info into the DB. So while all of this is making >>>>

                          $str_activities = $_POST['activities'];

                          $str_activities = preg_replace("#<.+?>(.+?)</.+?>#is", "\1", $str_activities);

                          the above code is the one that u need to change to an loop to look for the values in array of activities.

                          comming to the database part of it, in the database have u got one field set for the activites or more than one. because u need more than one field in the database for the activities because it gonna be more than one activities that get selected. If u want only one field to get into the database might be u need to combine all activities into one string and then insert it into the database.

                          "I might be wrong because i dont know how u r database is going to be and what u r gonna use the activities for".

                          hope that gives u an idea of what changes need to be done.

                            Ah, so you're another regular expression aficionado msk. 🆒

                            Don't understand why you think one is needed here though. 😕 The data in the array is the data we define in the html form code. If it's not what we need then redefine it there.

                            Dada, if you want to store the activities as a comma-seperated list in 1 column then just implode() the array into a string, see my example above.

                              sorry roger i think u got me wrong. I am just explaining him the both options he has got but didnt prefer one over the other.
                              As i told "I might be wrong because i dont know how u r database is going to be and what u r gonna use the activities for".
                              sorry if i passed the wrong impression.

                                Still don't see where preg-replace fits into this, whatever way you wanted to use or store the data. It's an array - and an array containing pre-defined values at that. !!!???

                                  No I understand how the array works now. I still don't know where extactly the code is suppose to be placed. I have just started learning PHP just a couple of weeks ago, but I am trying to learn and understand. So some of the things you explain are still greek to me. Yes I want whatever is selected to be entered into one column which I am able to do now.

                                  This is what I have done so far, I have replaced this line

                                  $str_activities = $_POST['activities'];  

                                  With this one.

                                  $str_activities = implode(',' , $_POST['activities']);

                                  Now everything is being entered, but only 3 values are going into the DB. It's like thier is a limit on how many can be entered. To test this I actually went and entered all of them in groups of three and they all go in, but It won't enter more then 3 into the DB if more then 3 are checked. Is thier a limit on how many you can have?

                                  -Thanks Again.

                                    Yes, it's the limit imposed by the size of the column that you have defined. So what data type have you set for the column? And what is the maximum storage capacity of that data type? Don't ask us about it, go and read the manual.

                                      Thank you, I have got it now. You could be a little more for forgiving to those just starting and leaning PHP though. Can't very well read the manual on something If I don't know exactly what I am looking for. That's like going on a road trip and not knowing where you're going. I mean you know how to drive, but that doesn't mean know how to go where you have never been before, right? You could have just said "Check the size of your column" I mean you are on a newbie support forum after all. Just remember you will need help on something some day and your actions of how you treat others could effect how your problem gets resolved. besides that everyone has to start somewhere right?

                                      -Thanks for you help, I really do appericate it.

                                        Write a Reply...