admin管理员组文章数量:1315252
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;
}
}
本文标签: phpGET multiple variable separated from POST formStack Overflow
版权声明:本文标题:php - GET multiple variable separated from POST form? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741980048a2408343.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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