Hey, I am creating a website with two forms which contain checkboxes and displays and sends the values input by the user to display on the page as well as save onto a database. The languages I'm using are xhtml, php and sql. The problem for me is that only one (usually the first value) sends over to the database if say for instance I select 3 checkboxes, what I need is for all the values to send over

Here is the coding.

Note: I've done alot of trial and error and nothing seems to be working for me. I would be very thankful if someone gave me a little hand.

XHTML:

<html>

<head>
<title>Form 7A</title>
</head>

<body>
<form action="Form7B.php" method="POST">
<p>Client ID:<input name="clientid" type="text" /></p>
<p>Company Name:<input name="name" type="text" /></p>

<input type="checkbox" name="worktype[]" value="Construction" />Construction<br />
<input type="checkbox" name="worktype[]" value="Civil" />Civil<br />
<input type="checkbox" name="worktype[]" value="Demolition" />Demolition<br />
<input type="checkbox" name="worktype[]" value="Industrial Manufacturing" />Industrial Manufacturing<br />
<input type="checkbox" name="worktype[]" value="Trades" />Trades<br />

<input name="" type="submit" value="Submit" />
<input name="" type="reset" value="Reset" />
</p>
</form>
</body>

</html>

PHP:

<html>

<head>
<title>Form 7B</title>
</head>

<body>
<?php

$host="localhost";
$user="root";
$password="";
$database="db2";

$clientid=$_POST['clientid'];
$name=$_POST['name'];

$mysqli=mysqli_connect($host,$user,$password,$database);

echo "<strong>Client ID:</strong>".$clientid."<br />";
echo "<strong>Name:</strong>".$name."<br />";

if(!empty($_POST["worktype"]))
	{
	echo"<ul>";
	foreach($_POST["worktype"] as $value)
		{
		echo "<li>$value</li>";
		$sqli="INSERT into client VALUES('$clientid','$name','$value')";
		$result=mysqli_query($mysqli,$sqli);
		}
	echo"</ul>";
	}
	
?>
</body>

</html>

Thanks in advance.

Ax.

Recommended Answers

All 5 Replies

Clients and Work types form a "1 to many" relationship (often 1:n.) This means that for every client (stored in one record) there needs to be 1 or more related worktypes for that client.

Usually this is solved with three tables. Two tables will store the data and the third will tie them together.

Table 1: clients -- clientid, name
Table 2: workTypes -- worktypeid, description
Table 3: clients_workTypes -- clientid, worktypeid

Initialize clients_workTypes with the types of work that are part of your system (construction, civil, demolition, industrial manufacturing.) Each of these will be assigned a unique worktypeid.

In your html form, retrieve the work types with a query and display the checkboxes. The value of the checkbox should be the worktypeid and display the description in the checkbox labels.

<label><input type="checkbox" name="worktype[]" value="1" />Civil</label><br />
<label><input type="checkbox" name="worktype[]" value="2" />Construction</label><br />

Insert one entry for the client (before the foreach loop.)
Delete all the client_workType entries for this client (if it had an ID already.)
Then loop over the values in the $_POST array and store an entry with the client id and the worktypeid into clients_workTypes.

That's basically it. Let me know if you have further questions and I'll help you out.

Hi

I think the below coding will help you to solve the issue

<html>

<head>
<title>Form 7A</title>
</head>

<body>
<form action="Form7B.php" method="POST">
<p>Client ID:<input name="clientid" type="text" /></p>
<p>Company Name:<input name="name" type="text" /></p>

<input type="checkbox" name="worktype[]" value="Construction" />Construction<br />
<input type="checkbox" name="worktype[]" value="Civil" />Civil<br />
<input type="checkbox" name="worktype[]" value="Demolition" />Demolition<br />
<input type="checkbox" name="worktype[]" value="Industrial Manufacturing" />Industrial Manufacturing<br />
<input type="checkbox" name="worktype[]" value="Trades" />Trades<br />

<input name="submit" type="submit" value="Submit" id="submit" />
<input name="" type="reset" value="Reset" />
</p>
</form>
</body>

</html>
<html>

<head>
<title>Form 7B</title>
</head>

<body>
<?php

$host="localhost";
$user="root";
$password="";
$database="db2";

$mysqli=mysqli_connect($host,$user,$password,$database);

echo "<strong>Client ID:</strong>".$clientid."<br />";
echo "<strong>Name:</strong>".$name."<br />";

if($_POST['submit']=='Submit')
{
$clientid=$_POST['clientid'];
$name=$_POST['name'];
$worktype = $_POST['worktype'];

$limit = count($worktype);
for($i=0;$i<$limit;$i++) 
{
$worktype[$i] = mysql_real_escape_string($worktype[$i]);

		echo "<li>$value</li>";
		$sqli="INSERT into client VALUES('$clientid','$name','".$worktype[$i]."')";
		$result=mysqli_query($mysqli,$sqli);
		}
	}
	
?>
</body>

</html>
Member Avatar for diafol
for($i=0;$i<$limit;$i++) 
{
$worktype[$i] = mysql_real_escape_string($worktype[$i]);
 
		echo "<li>$value</li>";
		$sqli="INSERT into client VALUES('$clientid','$name','".$worktype[$i]."')";
		$result=mysqli_query($mysqli,$sqli);
		}
	}

You could end up running many SQL queries with this, maybe better to add all chboxes to the same query.

Clients and Work types form a "1 to many" relationship (often 1:n.) This means that for every client (stored in one record) there needs to be 1 or more related worktypes for that client.

Usually this is solved with three tables. Two tables will store the data and the third will tie them together.

Table 1: clients -- clientid, name
Table 2: workTypes -- worktypeid, description
Table 3: clients_workTypes -- clientid, worktypeid

Initialize clients_workTypes with the types of work that are part of your system (construction, civil, demolition, industrial manufacturing.) Each of these will be assigned a unique worktypeid.

In your html form, retrieve the work types with a query and display the checkboxes. The value of the checkbox should be the worktypeid and display the description in the checkbox labels.

<label><input type="checkbox" name="worktype[]" value="1" />Civil</label><br />
<label><input type="checkbox" name="worktype[]" value="2" />Construction</label><br />

Insert one entry for the client (before the foreach loop.)
Delete all the client_workType entries for this client (if it had an ID already.)
Then loop over the values in the $_POST array and store an entry with the client id and the worktypeid into clients_workTypes.

That's basically it. Let me know if you have further questions and I'll help you out.

Hey madCoder,

Thanks for the advice.

I done a bit of tweaking according to your guides but I still have some more work to do. Theres still some mistakes but Im sure theres only minor tweaking, let me know what you think.

I was going to try the other methods presented, but I want to design my coding in the most logical & easy to read/modify manner.


Thanks alot!

HTML:

<html>

<head>
<title>Form 8A</title>
</head>

<body>
<form action="Form8B.php" method="POST">
<p>Company Name:<input name="name" type="text" /></p>

<input type="checkbox" name="worktype[]" value="0" />Construction<br />
<input type="checkbox" name="worktype[]" value="1" />Civil<br />
<input type="checkbox" name="worktype[]" value="2" />Demolition<br />
<input type="checkbox" name="worktype[]" value="3" />Industrial Manufacturing<br />
<input type="checkbox" name="worktype[]" value="4" />Trades<br />

<input name="" type="submit" value="Submit" />
<input name="" type="reset" value="Reset" />
</p>
</form>
</body>

</html>

PHP:

<html>

<head>
<title>Form 8B</title>
</head>

<body>
<?php

$host="localhost";
$user="root";
$password="";
$database="db2";

$name=$_POST['name'];

$mysqli=mysqli_connect($host,$user,$password,$database);

$sqli1="INSERT into client VALUES('NULL','$name')";

$res1=mysqli_query($mysqli,$sqli1);

if(!empty($_POST["worktype"]))
	{
	foreach($_POST["worktype"])
		{
		$sqli2="INSERT into client_worktype VALUES('NULL',$worktype)";
		}
	}

$res2=mysqli_query($mysqli,$sqli2);

?>
</body>

</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.