Hi, I’m making an attempt at creating a module (actually, I’m trying to move my old polling script over to MODx), and I’m having trouble passing the form values as an array during the actual poll creation process. Here’s what I have for the form where you type in the question, possible choices, set the poll status and choose what color the result bars will be:
<form name="module" method="post">
<input name="opcode" type="hidden" value="" />
<input type="hidden" name="act" value="createpoll" />
<table class="grid" cellpadding="1" cellspacing="1" width="450">
<tbody>
<tr>
<td>Question</th><td><input type="text" maxlength="255" size="40" name="question" value="' . $question . '" /></td>
</tr>';
for ($i = 1; $i < $numberOfChoices + 1; $i++){
$output .= '<tr>
<td>Choice ' . $i . '</td><td><input type="text" maxlength="255" size="40" name="answer[]" />
<td><select name="color[]">
<option value="blank">Bar Color:</option>
<option value="red">Red</option>
<option value="orange">Orange</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="purple">Purple</option>
</select></td>
</tr>';
}
$output .= '<tr>
<td colspan="3" align="center">Set Status As... <select name="status">
<option value="0">Open</option>
<option value="1">Closed</option>
<option value="2">Hidden</option>
</select> <input type="submit" value="Create New Poll!" /> <input type="reset" value="Reset Changes" /></td>
</tr>
</tbody>
</table>
</form>
</div>';
Basically it just loops through and populates the page with a text input ("answer[]") and a dropdown choice of colors ("color[]"). Then the submit button gets pressed and it should pass those values to the next part, which looks like this:
$timestamp = time();
$question = isset($_POST['question']) ? clean($_POST['question']) : '';
$status = isset($_POST['status']) ? clean($_POST['status']) : '';
$choices = clean($_POST['answer']);
$color = clean($_POST['color']);
$modx->db->query("INSERT INTO " . $infoTable . " (question, created, status)
VALUES ('" . $question . "','" . $timestamp . "','" . $status . "')");
$pollId = $modx->db->getInsertId();
for ($i = 1; $i <= sizeof($choices); $i++){
$modx->db->query("INSERT INTO " . $pollTable . " (poll_id, option_id, option_text, option_color)
VALUES ('" . $pollId . "','" . $i . "','" . $choices[$i] . "','" . $color[$i] . "')");
}
Now, everything works fine, except that it doesn’t seem like the arrays are being passed. No matter how many answers I put in, the "option_text" and "option_color" fields in the db are populated with "r", and only one row is added (so if I put in 3 answers, only 1 row will be added). I’ve been trying to get this working for hours and I was hoping that someone here could help. Thanks!