TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-15-2008, 08:21 PM   #1 (permalink)
The Wanderer
 
Join Date: Dec 2007
Location: united states
Posts: 16
Thanks: 7
Matt is an unknown quantity at this point
Default looping

I'm trying to make a feature for my poll system to create a poll with unlimited options.
First off, the poll adding is formed into two steps, the first step you chose the question of the poll, then you chose how many options you want. The second step displays the amount of options I entered, say I wanted 3 options, so 3 option input fields are created.

That is all I have it down to, I can't figure out how I would insert a row into the database for each option provided, say I have 3 options, after filling them out and submitting the data, I want each option recorded into the database.

Now, here is the code I have so far.

PHP Code:
<?
    
//-----------------------------------------------------------------
    //  Create Poll
    //-----------------------------------------------------------------

    
case 3:

    if (
$_POST['question'] && $_POST['options'])
    {
        
//-----------------------------------------------------------------
        //  Insert the poll
        //-----------------------------------------------------------------

        
$db->insertRow("poll",array('name' => ''.$_POST['question'].'','user' => ''.$userinfo['username'].'','date' => ''.time().''));

        
// not sure what the name of the post is supposed to be retreiving
        
if ($_POST['options'])
        {

        
//-----------------------------------------------------------------
        //  Add in the options
        //-----------------------------------------------------------------

            
startbox("Step Two");

            
$options $_POST[options];

            echo 
"<form action='$PHP_SELF?page=polls&a=3' method='post'><table class='main' cellspacing='1' cellpadding='5'>";
        
            for (
$a 1$a <= $options$a++) 
            {
                    echo 
'<tr class="con1"><td>Option '.$a.': <input type="text" name="option'.$a.'" /></td></tr>';
            }
        
                
// i know this isn't right.
                
if ($_POST[option])
                {
                    
// this loop definitely wont work.
                    
foreach($_POST['option'] as $key => $value
                    {
                        
$poll_option[$key] = $_POST['option'][$key];
                    }
                }
        
?>

        </table><br/>
        <table class="main" cellspacing="1" cellpadding="3">
        <tr class="con1">
            <td width="50%">Actions:</td>
            <td width="50%" align="right"><input type="submit" value="Next &raquo;" /></td>
        </tr>
        </table></form>

    <?php
    
}

    } else {

    
?>

    <div class="box">
    <div class="boxtitle"><?=$language['Poll add title']; ?></div>
    <div class="boxcontent">
    <form action="<?=$PHP_SELF;?>?page=polls&a=3" method="post">
    <table class="main" cellspacing="1" cellpadding="5">
        <tr class="head">
            <td width="70%"><?=$language['Poll add desc']; ?></td>
            <td width="10%" align="center"><?=$language['Poll add desc3']; ?></td>
        </tr>
        <tr class='con1'>
            <td>
               <input type="text" name="question" class="box" style="width:100%;"  size="65" value="" />
            </td>
            <td align="center">
               <input type="text" class="box" name="options" style="width:30px;" />
            </td>
        </tr>
    </table>
    </div></div><br/>
    <table class="main" cellspacing="1" cellpadding="3">
        <tr class="con1">
            <td width="50%">Actions:</td>
            <td width="50%" align="right"><input type="submit" value="Next &raquo;" /></td>
        </tr>
    </table></form>

    <?php

    
}

    break;
?>
This roadblock is definitely setting me back, if possible, it would be great if somebody can provide the loop for me an explain it as well, it would help me avoid coming across a problem like this ever again. Thanks.
Send a message via AIM to Matt Send a message via MSN to Matt Send a message via Yahoo to Matt
Matt is offline  
Reply With Quote
Old 06-16-2008, 07:03 AM   #2 (permalink)
The Contributor
 
Join Date: May 2008
Location: Denmark
Posts: 70
Thanks: 3
SpYkE112 is on a distinguished road
Default

You don't use the right name for the options POST array....
PHP Code:
                if ($_POST[option])
                {
                    
// this loop definitely wont work.
                    
foreach($_POST['option'] as $key => $value
                    {
                        
$poll_option[$key] = $_POST['option'][$key];
                    }
                } 
You have used "option" instead of "options"... Don't know if thats the problem...
</span></span>
SpYkE112 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:42 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design