Insert dynamic checkboxes and corresponding textboxes value.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sanka123
Forum Newbie
Posts: 1
Joined: Thu Oct 18, 2007 2:09 am

Insert dynamic checkboxes and corresponding textboxes value.

Post by sanka123 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello.

I have some[size=150] dynamic checkboxes and corresponding textboxes[/size].
[syntax="html"]
<tr>
                <td width="6%"><input name="service[]" type="checkbox"  value="primary"></td>
                <td width="32%" class="newsdate2">Primary</td>
                <td width="62%"><input name="txt[]" type="text" value=""  class="news_home" ></td>
              </tr>
              <tr>
                <td><input name="service[]" type="checkbox"  value="school"></td>
                <td class="newsdate2">School</td>
                <td><input name="txt[]" type="text"  class="news_home" ></td>
              </tr>
              <tr>
When a user ticks a checkbox and enters data into the corresponding textbox and submits, the value of the checkbox and textbox should be inserted into the database. I can insert the checkbox values perfectly, but the textbox values have issues.


That code will only input the ticked checkbox values perfectly, but if i've ticked checkbox 1, 3 and 5 (of the dynamic checkboxes) it loops 3 times and will only insert values from TEXTBOX 1,2,3 INSTEAD OF 1, 3, 5.

Can anyone point me in the right direction here? Thankyou gentlemen.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

You'll get better answers if you format code as normal rather than colourising it, using only appropriate capitization and generally formatting things as others do. Doing other than that makes people switch off rather than making people read and answer your post.

You don't mention how you process the form but shouldn't the txt value input field contain the same value as the checkboxes so you can tie the two together ?
Last edited by CoderGoblin on Thu Oct 18, 2007 4:47 am, edited 1 time in total.
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Post by N1gel »

I do something similar to track software versions on PCs.

I list all of the software in the db then allow people to enter revisions off each piece of software for a selected PC.

here is where i print out the form and the how i process the data

Code: Select all

//Outputs a list of all my software with a tick and text box
$query = "select Name,ID from software";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_row($result))
{
    echo "<tr><td><p>$row[0]</p></td><td><input type='checkbox' name='OnOff$row[1]' value='1'></td><td><input id='InstallNo' name='Rev$row[1]' type='text' size='10'></td>";
}
echo "</table>";

Code: Select all

//Process the data and put selected into the db
$query = "select Name,ID from software";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
    if($_POST["OnOff".$row[1]] == 1)
    {
        if($_POST["Rev".$row[1]] != "")
        {
            $queryUP = "INSERT INTO `softwareversions` (Software,PC,Revision) VALUES (";
            $queryUP .= $row[1].",";
            $queryUP .= $_GET["pc"].",";
	    $queryUP .= "'".$_POST["Rev".$row[1]]."'";
	    $queryUP .= ")";
	    mysql_query($queryUP);
        }
    }
}
Hope this helps you

Nigel
Post Reply