Thanks. This a website where students can take online Math tests. Questions
could be of multiple choice or fill in the blank.
$ans is a local variable that gets declared at assignment. $_POST[ 'ans' ]
is an array which could be an input box, textarea, radio button or checkbox.
I'm using an array in all cases because in case it is a checkbox then
multiple checkboxes could be true and I would need an array to hold their
values. This code gets called when creating the question.
if ($qtype==3) // fill in the blank type question
{
$q = explode("<blank>",$question);
$question = "<br>" . addslashes($q[0]) . " <input type=text name='ans[]'
value='$ans' class='fillblank' /" . addslashes($q[1]) ;
}
elseif ($qtype==2) // multiple choice question. radio or checkbox
{
if (strpos($question, "<r>")) // if <rexists then radio buttons
{
$dlm = "<r>";
$boxtype = "radio";
}
else
{
$dlm = "<c>"; // if <cexists then checkboxes
$boxtype = "checkbox";
}
$choices = explode($dlm,$question);
$question = array_shift($choices) . "<br>" ;
foreach($choices as $ch )
{
if (strcmp(trim($ans),trim($ch))==0)
$chk = "checked" ;
else
$chk = "";
// generate html for mulitple choice
$question .= "<input type=$boxtype name='ans[]' $chk value=$ch$ch
<br>";
}
}
else
$question = addslashes($question);
The code I posted earlier gets called when student submits answer (by either
checking the correct option or typing the correct value). I've replaced the
for loop with implode function and it works fine.
To solve the 'Array' problem I've just chopped of the 'Array,' string
$ans = trim(str_replace("Array,", "", $ans));
It must be the PHP version 4.x used by client compared to 5.x that I'm using
for development.
Thanks
"J.O. Aho" <us**@example.netwrote in message
news:6d************@mid.individual.net...
mab464 wrote:
>I have this code on my WAMP server running on my XP machine
if ( isset( $_POST[ 'ans' ] ) )
You should ensure that the $_POST['ans'] is of the right type, in your
case an array, have you thought about what happens if someone manages to
post something else than your array?
> {
for($i=0; $i<count($_POST['ans']);$i++)
Here you recalculate the size of the array EVERY turn in the loop, better
to store the size of the array to a variable and compare that to the size
of $i, it's a lot faster.
> {
if ($ans != NULL )
you should test the $ans variable if it's set or not, as at this point it
may be undeclared.
> $ans .= ", " . $_POST['ans'][$i] ; // Not the
first element so append a comma
else
$ans = $_POST['ans'][$i] ; // first element so
comma not needed
This can be done a lot faster and easier with an implode().
http://www.php.net/manual/en/function.implode.php
You can then drop the loop too.
> }
echo 'answer is '. $ans . '<br>';
}
The purpose of the for loop is to concatenate all the elements into a
comma seperated string.
if(isset($_POST['ans'] && is_array($_POST['ans'])) {
$ans = implode(', ',$_POST['ans']);
echo 'answer is '. $ans .'<br>';
}
>It works fine so if the first element is 22/7 it prints 'answer is 22/7'
However when I upload the file to the actual Linux based server and run
the same I get 'answer is Array, 22/7'. Here the word 'Array' is given
as the first element. So what's wrong and how can I fix this.
The first cell in $_POST['ans'] is an array, do a var_dump($_POST['ans'])
on the top of your page and you will see what you really did post to the
page.
I would think there is something that ain't completely right in your
post-form, which with the version of PHP you are using at home don't
handle on the same way as on the version of PHP that the remote servers
has.
You can run phpinfo() on both and you will see that there will most likely
be a major version difference.
--
//Aho