Jump to content

Having trouble getting variables read from matrix


NightFalcon90909

Recommended Posts

Greetings,

 

I've unsuccessfully fought with this for a while, so I guess I'll see if anyone here can help me.

 

Thanks

 

 

Goal: User gets a list of questions. He can answer any number of them. When he starts typing in one text box, the checkbox next to that box is checked off.

 

Then, he submits the form after answering any number of the questions.

 

Then, the script writes his answers and what questions they are answers to to a mysql db.

 

Wow, I didn't explain that very well. Here it is in code.

 

 

 

First, the html (the quotes are escaped because this is being printed in php):

 

<p> <input type=\"checkbox\" id=\"1\" name=\"question[]\" /> What was your childhood nickname?  <br /> <input id=\"1\" onfocus=\"getElementByid(1).checked = true\" type=\"text\" name=\"1\" /> </p>

<p> <input type=\"checkbox\" id=\"2\" name=\"question[]\" /> In what city did you meet your spouse/significant other?  <br /> <input id=\"2\" onfocus=\"getElementByid(2).checked = true\" type=\"text\" name=\"2\" /></p>

<p> <input type=\"checkbox\" id=\"3\" name=\"question[]\" /> What is the name of your favorite childhood friend? <br /> <input id=\"3\" onfocus=\"getElementByid(3).checked = true\" type=\"text\" name=\"3\" /></p>

 

So I have each of the checkboxes identified by a number, and named question[], which is apparently supposed to make them into a matrix called question.

 

$query = "CREATE TABLE $dbName (
				password_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
				siteName TEXT,
				sitePass TEXT
				)";

				if (@mysql_query ($query)) {

					foreach ($_POST['question'] as $id) { //go through the question matrix, call each one $id
						print "<br />$id"; //this was me trying to debug... it printed 'on', even when more than one checked.

						$answer = ($_POST["$id"]); //call the answer to this question number $answer
						//$answer = strtolower($answer); //temporarily killed for debugging

						$db = $username.'Main';  //select proper database
						$query = "INSERT INTO $db  
							(question_number, question_ID, answer)
							idS (0, '$id', '$answer'
							)"; //make query to add info to it

							//then do it!
							@mysql_query ($query);	
					}

 

 

As I mentioned, when printing the $id, it prints 'on'.

 

Here is the result I get:

on
Notice: Undefined index: on in /Users/Home/Sites/BetterPass/register.php on line 135

on
Notice: Undefined index: on in /Users/Home/Sites/BetterPass/register.php on line 135

(line 135 is $answer = ($_POST["$id"]); )

 

As a side note, the javascript for automatically checking the checkbox by each textbox isn't working either, so if anyone nows why that might be, I'd be very grateful!

 

Thanks so much,

Joseph

Link to comment
Share on other sites

As I mentioned, when printing the $id, it prints 'on'.

Well you never gave your check box a value ... so yeah ...

 

<input type=\"checkbox\" id=\"1\" name=\"question[]\" value=\"MY VALUE\"/>

 

oohh i see you got mixtup with the id="" and value="" hehe

Well hopes this helps ya out ;-)

Link to comment
Share on other sites

Yes I was afraid I was a bit not very clear...

 

So here is a bit simpler model of it.

 

20090212-dsrh5u5hhmic5s4h7gg23csp6r.preview.jpg Click to enlarge...

 

Anyway. Any number of those three question can be answered.

 

Bottom line, I want to have some way of telling which ones were answered, and then store the answers to those questions to a mysql database. I also need to store which questions go with each answer.

 

Link to comment
Share on other sites

well ...

 

<p><input type="checkbox" value="<?=$id?>" name="question[]" value="ON"> QUESTION 1</p>
<p><input type="text" name="awnser[]" size="50"></p>
<p><input type="checkbox" value="<?=$id?>" name="question[]" value="ON">QUESTION 2</p>
<p><input type="text" name="awnser[]" size="50"></p>
<p><input type="checkbox" value="<?=$id?>" name="question[]" value="ON">QUESTION 3</p>
<p><input type="text" name="awnser[]" size="50"></p>

 

$i="0";

foreach ($_POST[question] as $question){
// Question number $question on loop $i goes with awnser $_POST[awnser][$i]

}

Link to comment
Share on other sites

Aha! missing ";

 

ok...

 

It's not happy. It went through, but nothing was saved to the database.

 

However I feel like I'm closer now...

 

My HTML now looks like this.

 

line 1<p> <input type="checkbox" id="1" value="<?=$id?>" name="question[]" /> What was your childhood nickname?  <br /> <input id="1" onfocus="getElementByid(1).checked = true" type="text" name="answer[]" /> </p>
line 2<p> <input type="checkbox" id="2" value="<?=$id?>" name="question[]" /> In what city did you meet your spouse/significant other?  <br /> <input id="2" onfocus="getElementByid(2).checked = true" type="text" name="answer[]" /></p>
line 3<p> <input type="checkbox" id="3" value="<?=$id?>" name="question[]" /> What is the name of your favorite childhood friend? <br /> <input id="3" onfocus="getElementByid(3).checked = true" type="text" name="answer[]" /></p>

 

 

However, upon submitting the form, php tells me:

 

Notice: Undefined variable: id in ...register.php on line 1.

Notice: Undefined variable: id in ...register.php on line 3.

 

However, at least it is singling out those two lines, since those are the two questions I answered.

 

The PHP code now looks like this:

 

1if (@mysql_query ($query)) {
2						print "Successfully created table called $dbName";
3						
4						$i = 0;
5						foreach ($_POST[question] as $question){
6							// Question number $question on loop $i goes with answer $_POST[awnser][$i]
7							$answer = $_POST[answer][$i];
8							$i++;
9																	
10							$db = $username.'Main';  //select proper database
11							$query = "INSERT INTO $db  
12								(question_number, question_ID, answer)
13								idS (0, '$question', '$answer'
14								)"; //make query to add info to it
15								
16								//then do it!
17								@mysql_query ($query);	
18

However, when I do this, I get:

[code]Notice: Use of undefined constant question - assumed 'question' in /Users/Home/Sites/BetterPass/register.php on line 5

Notice: Use of undefined constant answer - assumed 'answer' in /Users/Home/Sites/BetterPass/register.php on line 7

Notice: Use of undefined constant answer - assumed 'answer' in /Users/Home/Sites/BetterPass/register.php on line 7

 

Syntax error?

 

Regardless, nothing is being saved to the database...

 

 

 

Link to comment
Share on other sites

In the HTML code theres a value="<?=$id?>" for the check box ... did you actualy give $id a value? like the question number?

 

<?php

if ($_POST){
// process the form here

}else{
// the form stuff
// <form [...]

$questions[] = "What was your childhood nickname?"; // Question 0
$questions[] = "In what city did you meet your spouse/significant other? "; // Question 1
$questions[] = "What is the name of your favorite childhood friend?"; // Question 2

$i=0; foreach($questions as $q){
echo 'line '.$q.'<p> <input type="checkbox" id="'.$i.'" value="'.$i.'" name="question[]" /> What was your childhood nickname?  <br /> <input id="'.$i.'" onfocus="getElementByid('.$i.').checked = true" type="text" name="answer[]" /> </p>';
$i++;
} 
} 

?>

Link to comment
Share on other sites

Victory!

 

Thanks drisate so much for your help. Here is the code that I finally wound up using.

 

 
$question[] = "What was your childhood nickname?"; // Question 0
$question[] = "In what city did you meet your spouse/significant other? "; // Question 1
$question[] = "What is the name of your favorite childhood friend?"; // Question 2

$i=0; 

foreach($question as $q){
echo '<p> <input type="checkbox" id="'.$i.'" value="'.$i.'" name="question[]" />'.$q.' <br /> <input id="'.$i.'" onfocus="getElementByid('.$i.').checked = true" type="text" name="answer[]" /> </p>';
$i++;
} 

 

 

there is the PHP for the form, and here is the PHP for the handler.

 

$i = 0;
					foreach ($_POST[question] as $question){
						// Question number $question on loop $i goes with answer $_POST[awnser][$i]
						$answer = $_POST[answer][$question];
						$i++;



						$db = $username.'Main';  //select proper database
						$query = "INSERT INTO $db  
							(question_number, question_ID, answer)
							VALUES (0, '$question', '$answer'
							)"; //make query to add info to it

							//then do it!
							if (@mysql_query ($query))
								print "<br />Wrote answer...<br />";
							else
								die (mysql_error());	

 

the most notable change here was to do

 

$answer = $_POST[answer][$question];

 

instead of

 

$answer = $_POST[answer][$i]; 

 

as suggested, since the user isn't required to answer all the questions in a row, and if they, for example, do questions 1 and 3, it messes everything up.

 

 

Anyway, thanks so much for your help drisate

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.