We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18110
    • 13 Posts
    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! smiley
      Mr. Madison, what you’ve just said is one of the most insanely idiotic things I have ever heard. At no point in your rambling, incoherent response were you even close to anything that could be considered a rational thought. Everyone in this room is now dumber for having listened to it. I award you no points, and may God have mercy on your soul.
      • 22303 MODX Staff
      • 10,725 Posts
      I ran into the same problem when upgrading to 0.9.2.x -- some new code in index.php is cleaning the REQUEST vars, but it doesn’t work for values meant to be arrays in the $_POST. This needs to be fixed, and if you wouldn’t mind, a bug in our bug-tracker regarding this would be a great reminder for me to do so.

      In the meantime, you can easily work around the problem by changing these lines in index.php from

      foreach($_POST as $key => $value) {
        $_POST[$key] = preg_replace($modxtags,"", $value);
      }

      to
      foreach($_POST as $key => $value) {
        if (!is_array($value)) {
          $_POST[$key] = preg_replace($modxtags,"", $value);
        }
      }


      This is a cheap workaround, and does not properly scrub the POST array values, but it should get it working until a proper fix is committed.
        • 4041
        • 788 Posts
        holey moley, I also spent quite a few hours trying to get an array passed to a snippet... thanks for the quick fix, works fine so far.

        The way I eventually got the array passed was a bit of code form php.net, I’ll post it here maybe it can help in individual situations as opposed to altering the main index file.

        function explode2($delimeter, $string){
           for ($i = 0; $i < strlen($string); $i++){
               if ($string{$i} == '"'){
                   if ($insidequotes)
                       $insidequotes = false;
                   else
                       $insidequotes = true;
               }else if ($string{$i} == $delimeter){
                   if ($insidequotes){
                       $currentelement .= $string{$i};
                   }else{
                    $returnarray[$elementcount++] = $currentelement;
                    $currentelement = '';
                   }
               }else{
                   $currentelement .= $string{$i};
               }
          }
           $returnarray[$elementcount++] = $currentelement;
           return $returnarray;
        }


        use it just like explode() and see if it will help...
          xforum
          http://frsbuilders.net (under construction) forum for evolution
          • 18110
          • 13 Posts
          Wow, thanks a ton! smiley I thought it was something I was doing wrong when I was rewriting my code, and I was working at it for hours. tongue Ah well, it’s good to know that you MODx developers are hard at work on these things, I’m sure 0.9.5 will be awesome! wink I’ll file a bug report about this (if one hasn’t already been sent in laugh ).

          EDIT: I just posted a bug report for this. smiley The problem though, is that I can’t release this until the bug is fixed unless I include the above code that Open Geek posted with my installation instructions. lipsrsealed Ah well, it probably won’t be ready until after then anyways. tongue

          Thanks! smiley
            Mr. Madison, what you’ve just said is one of the most insanely idiotic things I have ever heard. At no point in your rambling, incoherent response were you even close to anything that could be considered a rational thought. Everyone in this room is now dumber for having listened to it. I award you no points, and may God have mercy on your soul.