lundi 31 juillet 2017

PHP form, input values and check box to mysql table

I am trying to create sign up form, where it has input values (name, email, phone, ..) and have multiple checkbox option.

I also create table in mysql database for each checkbox. My goal is to values entered in input will only go to selected checkbox database.

At first, I had perfectly working input values to go table in database, but when I added checkbox and try to direct by selected checkbox, I mess everything up.

input.php

<html>
<head>
  <title>post submit</title>    
</head>
<script type="text/javascript">
function toggle(source) {
  checkboxes = document.getElementsByName('chk1[]');
  for(var i=0, n=checkboxes.length;i<n;i++) {
    checkboxes[i].checked = source.checked;
  }
}
</script>
<body>
<h2><font color="white">Welcome to Cars and Coffee</h2>
<p>Sign up to receive if you are interested in 
    receiving information from Cars and Coffee.</p>
<p>Please select at least one option</p>
<form name="newEntry" action="page.php" method="POST">
name<input type=text length=60 size=30 name="name"><br>
email<input type=text length=60 size=30 name="email"><br>
car<input type=text length=60 size=30 name="car"><br>
phone<input type=text length=60 size=30 name="phone"><br>
<input type="submit" name="submit" email="add" car="add" phone="add" >

</form>

<form action="checkbox.php" method="POST">

<input type="checkbox" name="chk1[]" value="MT">Cars  & Coffee<br>
<input type="checkbox" name="chk1[]" value="MT">Kentucky Derby Party<br>
<input type="checkbox" name="chk1[]" value="MT">July 4th<br>
<input type="checkbox" name="chk1[]" value="MT">Labor Day<br>
<input type="checkbox" name="chk1[]" value="MT">New Years Day<br>
<input type="checkbox" name="chk1[]" value="MT" onClick="toggle(this)">Select All<br>

</form>
<?php
mysql_connect("localhost", "db", "pw");
mysql_select_db("hpfarmne_events");
$qh = mysql_query("select * from  new_email_list 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 "
<p>
$obj->email<br>
$obj->name<br>
$obj->car<br>
$obj->phone<br>
<p>
<hr>
";
       }
}
?>  
</body>
</html>

page.php

<?php
mysql_connect("localhost", "db", "pw");
mysql_select_db("hpfarmne_events");
mysql_query("insert into new_email_list (name, email, car, phone) values ('".$_POST['name']."','".$_POST['email']."','".$_POST['car']."', '".$_POST['phone']."' )");
?>
<html>
<head>
<meta http-equiv=refresh content="0; URL=./input.php">
</head>
</html>

checkbox.php

<?php
include("page.php");
$checkbox1 = $_POST["chk1"];

if($_POST["Submit"] == "Submit") {

    for (i=0; $<sizeof($checkbox1); $i++){
        $query = "INSERT INTO  (name) VALUES( '".$checkbox1[i].'")";
        mysql_query($query) or die(mysql_error());
    }
    echo "Record  is inserted";
}
?>




Aucun commentaire:

Enregistrer un commentaire