I am new to PHP, I am making a Quiz page. The answers in FORM are sent to test-submit.php as follow:
question1-answer1; question2-answer2; question3-answer2...
http://localhost/tracy/test-submit.php?q1=a1&q2=a1&q3=a2&q4=a1
So I am confused how to get all the data separately to import to SQL database?
- q1 = a1
- q2 = a2
- q3 = a1
- ...
What if I have 1000 questions in URL? Iam looking for any help. Really appreciated!!!
I tried with $_GET['']
but I don't know what to get. is it:
$_GET['q1'];
$_GET['q2'];
$_GET['q3'];
$_GET['q4'];
$_GET['q5'];
- ...
I am new to PHP, I am making a Quiz page. The answers in FORM are sent to test-submit.php as follow:
question1-answer1; question2-answer2; question3-answer2...
http://localhost/tracy/test-submit.php?q1=a1&q2=a1&q3=a2&q4=a1
So I am confused how to get all the data separately to import to SQL database?
- q1 = a1
- q2 = a2
- q3 = a1
- ...
What if I have 1000 questions in URL? Iam looking for any help. Really appreciated!!!
I tried with $_GET['']
but I don't know what to get. is it:
$_GET['q1'];
$_GET['q2'];
$_GET['q3'];
$_GET['q4'];
$_GET['q5'];
- ...
1 Answer
Reset to default 0You can loop through all the query params
and check if the value corresponding to the key exists
foreach($_GET as $key => $val) {
if(!empty($val)) {
$save[$key] = $val;
}
}
Then you can process the final $save
array as you want.
More importantly, if you have 1000 questions, means large form value and doing database operations, you should use POST
method instead of GET
. In that case the loop will be similar:
foreach($_POST as $key => $val) {
if(!empty($val)) {
$save[$key] = $val;
}
}
for
loop, and string concatenation - no?for ($i=1; $i<1001; ++$i) { do_stuff_with($_GET['q'.$i]); }
– C3roe Commented Jan 30 at 7:46name="q[question-id-goes-here]"
-name="q[1]"
,name="q[2]"
, etc. Then PHP will provide$_GET['q']
as an array, that you can easily iterate over with aforeach
loop. And then you will not even need to know the exact number of questions beforehand, or you could even have "gaps" in the question IDs. – C3roe Commented Jan 30 at 7:49