Forum Moderators: coopster
What I am trying to do is post a form that has numerous projects and get the user to choose what Topic they belong to.
I want the values to go into my SQL db under either TopicA column or TopicB column in my db.
So the answer is always going to be Topic A or Topic B for each one so when the db is populated I can search the the answers by TopicA or TopicB
Here is my form source:
<form name="form1" method="post" action="calstat_fund_insert.php">
<fieldset><legend>Training</legend><br>
<input name="TopicA" type="checkbox" value="training" >
TOPIC A</label>
<input name="TopicB" type="checkbox" value="training" >
<label for="TOPIC B">TOPIC B</label>
</fieldset>
<fieldset><legend>State Leadership Institute</legend><br>
<input name="TopicA" type="checkbox" value="State Leadership Institute">
TOPIC B
<input name="TopicB" type="checkbox" value="State Leadership Institute"></fieldset>
<fieldset><legend>Learning Center</legend>
<br>
TopicA
<input name="TopicA" type="checkbox" value="Learning Center">
TOPIC B
<input name="TopicB" type="checkbox" value="Learning Center"></fieldset>
<fieldset><legend>Leadership Sites</legend>
<br>
TopicA
<input name="TopicA" type="checkbox" value="Leadership Sites">
TOPIC B
<input name="TopicB" type="checkbox" value="Leadership Sites"></fieldset>
<input name="submit" type="submit">
</form>
and HERE is my PHP code:
<?
$username="username";
$password="password";
$database="database";
$TopicA=$_POST['TopicA'];
$TopicB=$_POST['TopicB'];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO table VALUES ('','$TopicA', '$TopicB')";
if(mysql_connect) { echo"<p>Thank you</p>";
}else{
echo "There was a problem sending info";
}
mysql_close();
?>
I'm sure what I am doing is fundamentally wrong, so if anybody can point me in the correct directions I would greatly appreciate it!
The results I'm getting ;only one response per column is getting inputed.
So.. The form asks the question A? B? for each row and of course I only get the last response from each column.
The problem seems to be that for all the checkboxes, the input name is either Topic A (calstat) or Topic B (SIG) and the values are specific to the question.
So as an example
---------------------------------------------------------
State Leadership Institute
<input name="calstat" type="checkbox" value="State Leadership Institute">
<input name="sig" type="checkbox" value="State Leadership Institute">
Regional Institute
<input name="calstat" type="checkbox" value="Regional Institutes">
<input name="sig" type="checkbox" value="Regional Institutes">
----------------------------------------------------------------------
here is a link to the form
<snip>
I understand that my method is flawed. I know I should have a column for each question , but I need to be able to query the db by Topic A or Topic B. What programs fall under Topic A? What programs fall under Topic B?
again, thank you in advance. I hope I am making sense. This is proving difficult for me to explain.
-dottomm
[edited by: eelixduppy at 4:22 am (utc) on Oct. 23, 2007]
[edit reason] no urls, please [/edit]
It sounds like you have a form that has several "Yes/No" type questions and you want to have all of the answers entered into your database but you are only getting the last answer instead of all of the answers.
If that's what you are looking for you are close with your code but you are only set up to take the last answer. So you have two options, you can rename your checkboxes so that each set is different or you can set them up to create an array of data. (I'll suggest the second option)
Here's what you get (without making it pretty):
Training
<input name="TopicA[]" type="checkbox" value="training" >
TOPIC A
<input name="TopicB[]" type="checkbox" value="training" >
TOPIC B
Leadership
<input name="TopicA[]" type="checkbox" value="State Leadership Institute">TOPIC A
<input name="TopicB[]" type="checkbox" value="State Leadership Institute">TOPIC B
Learning Center
<input name="TopicA[]" type="checkbox" value="Learning Center">TOPIC A
<input name="TopicB[]" type="checkbox" value="Learning Center">TOPIC B
Leadership Sites
<input name="TopicA[]" type="checkbox" value="Leadership Sites">TOPIC A
<input name="TopicB[]" type="checkbox" value="Leadership Sites">TOPIC B
and so on... Notice the [] after each name, this tells your system to create an array out of the choices made.
Now to handle the results:
$TopicA=$_POST['TopicA'];
$TopicB=$_POST['TopicB'];
foreach ($TopicA as $data) {
insert into database (field1, field2, etc...) values ('$data', 'whatever', etc..)
};
foreach ($TopicB as $data) {
insert into database (field1, field2, etc...) values ('whatever', '$data', etc..)
};
or however you need to insert the data. If it has to be in the same order as the questions then change your field names to "Question1", "Question2", etc and capture the data for each question separately.
Did that help at all?