jeudi 21 janvier 2016

Simple PHP form with radio, textbox, with an array checkbox error

I believe my error is with my array, but what is the best way to make the checkbox array output a specific result into a data base?

'$checkboxitems[0]','$checkboxitems[1]','$checkboxitems[2]',

I just wanted to make an all purpose form to input some values into a mysql database for now.

Here is the Form.html

<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="output_to_sql.php">
Field input:<br /> 
First Name:<input type="text" size="12" maxlength="12" name="field1"><br />
Last Name:<input type="text" size="12" maxlength="36" name="field2"><br />

Radio Button<br />
Gender:<br />
Male:<input type="radio" value="Male" name="radio"><br />
Female:<input type="radio" value="Female" name="radio"><br />

Check box - Please choose type of residence:<br />
Steak:<input type="checkbox" value="Steak" name="checkbox[]"><br />
Pizza:<input type="checkbox" value="Pizza" name="checkbox[]"><br />
Chicken:<input type="checkbox" value="Chicken" name="checkbox[]"><br />

<textarea rows="5" cols="20" name="txtbox" wrap="physical">Enter your favorite    Text!</textarea><br />

Drop Down - Select a Level of Education:<br />
<select name="dropdown">
<option value="Jr.High">Jr.High</option>
<option value="HighSchool">HighSchool</option>
<option value="College">College</option></select><br />

Highlight different values = Select your favorite time of day:<br />
<select name="highlight" size="3">
<option value="Morning">Morning</option>
<option value="Day">Day</option>
<option value="Night">Night</option></select><br />
<input type="submit" value="submit" name="submit">
</form>

output_to_sql.php

<?php

$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "database";

// Create connection
$connection = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo "success";
}

//Fields of input
$field1 = $_POST["field1"];
$field2 = $_POST["field2"];
$radio = $_POST["radio"];
$checkbox = $_POST["checkbox"];
$txtbox = $_POST["txtbox"];
$dropdown = $_POST["dropdown"];
$highlight = $_POST["highlight"];

if (!isset($_POST['submit'])) { 
// if page is not submitted to itself echo the form
echo "error submitting form";
} 
else //show form data with answers
{
echo "Hello, ".$field1." ".$field2.".<br />";

echo "You are ".$radio.", and you like ";

//make an array with check boxes and show result
foreach ($checkbox as $checkboxitems) {
echo $checkboxitems."<br />";
}
echo "<i>".$txtbox."</i><br />";

echo "You're favorite time is ".$highlight.", and you passed ".$dropdown."!<br />";
}

$sql =  mysqli_query("INSERT INTO `table` (`field1`, `field2`, `radio`, `checkbox1`, `checkbox2`, `checkbox3`, `txtbox`, `dropdown`, `highlight`) VALUES ('$field1, '$field2', '$radio', '$checkboxitems[0]','$checkboxitems[1]','$checkboxitems[2]', '$txtbox', '$dropdown', '$highlight')";


if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?> 




Aucun commentaire:

Enregistrer un commentaire