php5:pass objects through Form ? can it be done?
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 scorewhat 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 ver开发者_Python百科y important for me to keep every thing dynamically so i need to include hidden fields. is it doable ?
You could use json datatype for this kind of task (assuming your objects just store data, not methods nor require __sleep, __wakeup, etc).
<input name="fooBar" value="<?php echo json_encode(/*some_array_with_data*/);?>">
After form POST you can do something like
$_POST['fooBar'] = json_decode($_POST['fooBar']);
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.
精彩评论