title: PHP form text field to mysql based on selected checkbox
body: <p>I am trying to create sign up form, where it has multiple text field (name, email, phone, ..) and have multiple checkbox options.</p>
  
  <p>I also created multiple tables in mysql database, one for each checkbox.<br>
     My goal is to store values in text field to selected checkbox data table.</p>
  
  <p>My question is how can I create if statement to look what checkbox is selected, so it will store input of text field to that table</p>
  
  <p><strong>input.php</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  
  &lt;/head&gt;
  &lt;script type="text/javascript"&gt;
  function toggle(source) {
    checkboxes = document.getElementsByName('chk1[]');
    for(var i=0, n=checkboxes.length;i&lt;n;i++) {
      checkboxes[i].checked = source.checked;
    }
  }
  &lt;/script&gt;
  &lt;body&gt;
  &lt;h2&gt;&lt;font color="white"&gt;Welcome to Cars and Coffee&lt;/h2&gt;
  &lt;p&gt;Sign up to receive if you are interested in 
      receiving information from Cars and Coffee.&lt;/p&gt;
  &lt;p&gt;Please select at least one option&lt;/p&gt;
  &lt;form name="newEntry" action="page.php" method="POST"&gt;
  name&lt;input type=text length=60 size=30 name="name"&gt;&lt;br&gt;
  email&lt;input type=text length=60 size=30 name="email"&gt;&lt;br&gt;
  car&lt;input type=text length=60 size=30 name="car"&gt;&lt;br&gt;
  phone&lt;input type=text length=60 size=30 name="phone"&gt;&lt;br&gt;
  &lt;input type="submit" name="submit" email="add" car="add" phone="add" &gt;
  
  &lt;/form&gt;
  
  &lt;form action="page.php" method="POST"&gt;
  
  &lt;input type="checkbox" name="chk[]" value="cars_coffee"&gt;Cars  &amp; Coffee&lt;br&gt;
  &lt;input type="checkbox" name="chk[]" value="kentucky"&gt;Kentucky Derby Party&lt;br&gt;
  &lt;input type="checkbox" name="chk[]" value="july"&gt;July 4th&lt;br&gt;
  &lt;input type="checkbox" name="chk[]" value="labor"&gt;Labor Day&lt;br&gt;
  &lt;input type="checkbox" name="chk[]" value="new_year"&gt;New Years Day&lt;br&gt;
  &lt;input type="checkbox" name="chk[]" value="all" onClick="toggle(this)"&gt;Select All&lt;br&gt;
  
  &lt;/form&gt;
  &lt;?php
  mysql_connect("localhost", "db", "pw");
  mysql_select_db("db");
  $qh = mysql_query("select * from  all_emails order by id desc");
  if (@mysql_num_rows($qh)) { /* the if(@...) syntax makes PHP supress any
  warnings that might come out of that function. */
          /* mysql_fetch_object(query handle);
           * returns an object whose contents are that of one rotw in the
  database. */
          while ($obj = mysql_fetch_object($qh)) {
                  echo "
  &lt;p&gt;
  $obj-&gt;email&lt;br&gt;
  $obj-&gt;name&lt;br&gt;
  $obj-&gt;car&lt;br&gt;
  $obj-&gt;phone&lt;br&gt;
  &lt;p&gt;
  &lt;hr&gt;
  ";
         }
  }
  ?&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>page.php</strong></p>
  
  <pre><code>&lt;?php
  mysql_connect("localhost", "db", "pw");
  mysql_select_db("db");
  mysql_query("insert into all_emails (name, email, car, phone) values ('".$_POST['name']."','".$_POST['email']."','".$_POST['car']."', '".$_POST['phone']."' )");
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv=refresh content="0; URL=./input.php"&gt;
  &lt;/head&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is the if statement I am trying.</p>
  
  <pre><code>&lt;?php
  mysql_connect("localhost", "db", "pw");
  mysql_select_db("db");
  
  if( isset($_POST["cars_coffee"] ) ) {
      $sql = "INSERT INTO cars_and_coffee (name, email, car, phone) values ('".$_POST['name']."','".$_POST['email']."','".$_POST['car']‌​."', '".$_POST['phone']."' )");
      mysql_query($sql) or die(mysql_error());
      }
      echo "Record  is inserted";
  }
  
  ?&gt;
  </code></pre>