Computing.Net > Forums > Programming > PHP array questions

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP array questions

Reply to Message Icon

Name: igor12
Date: November 25, 2005 at 13:50:30 Pacific
OS: Gentoo Linux
CPU/Ram: 450/384
Comment:

I'm trying to post the array values from one form to another. Here is the code for the sender:


<?php

print "<h1>Checkbox Questions: </h1>

";

for($x = 0; $x < $checkbox; $x++)
{
print "Text: " . '<input type = "text" size = 60 name = "$checkbox_q_" . [$x]>' . " " . "Options: "
. '<input type = "text" size = 5 name = "$checkbox_o_" . [$x]>' . " " . "Points: " .
'<input type = "text" size = 5 name = "$checkbox_p_" . [$x]>' . "
";
}
?>
<body>

<form action= "finalize_quiz.php" method = "post">
<input type = submit value = "Finalize Quiz...">
</form>
</body></html>

So with the code above, a certain amount of text forms will be printed, and for each iteration the values will be written to $checkbox_q_[$x]. So I should be able to read the values as arrays (ie. $checkbox_q_[4]).

And here is the code for the reciever, just a very simple test that should print the very first element of whatever was entered in the previous form. This file is finalize_quiz.php:

<?php

$checkbox_text = $_POST[checkbox_q_[]];

print checkbox_q_[0];
?>

And it doesn't work. Any Clues?

Thanks,
Igor



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: November 26, 2005 at 09:02:05 Pacific
Reply:

1. You can't "print" an array in that manner.

2. I don't think the way you coded your input boxes will work correctly. What does the HTML look like when processed.

3. It looks like you are trying to make the names of the checkboxes as array indexes (i.e. $checkbox_q_[0], checkbox_q_[1], etc.). You can't do that. HTML doesn't recognize arrays - and I don't know that you can even use those character in a field name.

Here is my suggestion. have the field names go like this: checkbox0, checkbox1, checkbox2, etc. Then on the page that processes the form, put the values into an array if you need to. There is one problem however, I don't think that checkboxes and their values are passed if the checkbox is not checked. So you will need to put some logic into the processing page such as:

for($x = 0; $x < length; $x++)
{ // you need to define length somehow

if ($_POST["checkbox_"+$x]) {
checkbox[$x] = $_POST["checkbox_"+$x];
} else {
checkbox[$x] = "";
}

}

Michael J


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: PHP array questions

array question no one can answer www.computing.net/answers/programming/array-question-no-one-can-answer/3205.html

Perl array question www.computing.net/answers/programming/perl-array-question/4352.html

c++ arrays question www.computing.net/answers/programming/c-arrays-question/13122.html