php5:通过表单传递对象?可以吗?
我在测试应用程序时遇到了这个小问题。
目前的外观:
<form>
<span>1:Question goes here </span>
<input type=hidden name=true_1 value=10>
<input type=hidden name=false_1 value=20>
<input type=hidden name=target_1 value=color>
<select name=answer_1[]><option>blah</option><option>etc</option></select>
<span>2:Question goes here </span>
<input type=hidden name=true_2 value=40>
<input type=hidden name=false_2 value=20>
<input type=hidden name=target_2 value=size>
<select name=answer_2[]><option>blah</option><option>etc</option></select>
</form>
目前我使用一个可爆炸的脚本(“_”,$ get);这样我就可以用它自己的属性来处理每个问题。
当前方法
1.从数据库中获取问题。 (问题、目标、正确、错误、答案)
2.循环遍历它们并创建一个类似于我上面为所有问题编写的表单 3.发布后我爆炸性地单独获取每个问题的属性 4.将它们传递给评估答案是否正确并给出分数的函数
我想做的是将其转换为oop
1.为每个问题创建一个对象并分配其自己的属性(question,true,false,target,answer) foreach($question)$q[1]=new Question();
2.echo $q[] 供用户回答。
3.在用户回答后接收$q[]并$_POST它们。
希望我的描述是正确的。但是对我来说保持每件事都是动态的非常重要,所以我需要包含隐藏字段。 可行吗?
i have this littel problem with a test app.
Current look:
<form>
<span>1:Question goes here </span>
<input type=hidden name=true_1 value=10>
<input type=hidden name=false_1 value=20>
<input type=hidden name=target_1 value=color>
<select name=answer_1[]><option>blah</option><option>etc</option></select>
<span>2:Question goes here </span>
<input type=hidden name=true_2 value=40>
<input type=hidden name=false_2 value=20>
<input type=hidden name=target_2 value=size>
<select name=answer_2[]><option>blah</option><option>etc</option></select>
</form>
currently i use a script that explode("_",$get); so i can handle each question with its own properties.
Current approach
1.Get questions from database. (question,target,true,false,answers)
2.loop throught them and creat a form similar to one i wrote above for all questions
3.When posted i explode to get each question properties alone
4.pass them to function that evaluate if answer correct or false and give score
what im trying to do is to turn this into oop
1.creating an object for each question and assign its own properties(question,true,false,target,answer) foreach($question)$q[1]=new question();
2.echo the $q[] for user to answer.
3.receive the $q[] after user answer and $_POST them.
hope i described this right.but its very important for me to keep every thing dynamically so i need to include hidden fields.
is it doable ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 json 数据类型来完成此类任务(假设您的对象仅存储数据,而不是方法,也不需要 __sleep、__wakeup 等)。
在表单 POST 之后,您可以执行类似的操作
并开始迭代结果数组。
注意。由于 json_encode 会生成包含 " 等字符的字符串,因此您还应该使用 base64_encode、url_encode 或类似的东西。
You could use json datatype for this kind of task (assuming your objects just store data, not methods nor require __sleep, __wakeup, etc).
After form POST you can do something like
and start iterating resulting array.
NB. since json_encode results in string containing chars like " you should also use base64_encode, url_encode or something like that.