9 Replies - 2426 Views - Last Post: 30 December 2011 - 01:42 PM Rate Topic: -----

#1 DeadScream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-November 11

How to learn which dynamic radio button is selected

Posted 29 December 2011 - 07:45 PM

Hello, I have created an html page which adds dynamically radio buttons and text boxes so that later on I can take the values of the text boxes and whether the corresponding radio button is selected or not and store the data into a table in my database.

The html code which the user whill see and add radio buttons and text boxes is:
<TABLE id="dataTable" width="350px" >
                            <TR>                            
                                <TD> <input type="radio" name="choice[]" value="1" /> </TD>
                                <TD> <INPUT type="text" name="answerTxt[]"/> </TD>
                            </TR>
                    	</TABLE>
                        <INPUT type="button" value="Add" onclick="addRow('dataTable')" />
     
                        <INPUT type="button" value="Delete" onclick="deleteRow('dataTable')" />



The java script which adds do the whole work is:
function addRow(tableID) 
{
	var table = document.getElementById(tableID);
 
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;
 
  	for(var i=0; i<colCount; i++) 
	{
  		var newcell = row.insertCell(i);
  		newcell.innerHTML = table.rows[0].cells[i].innerHTML;
		  		
        switch(newcell.childNodes[0].type) 
		{
        	case "text":
               	newcell.childNodes[0].value = "";
                break;
            case "radio":
                newcell.childNodes[0].checked = false;
                break;
            case "select-one":
                  newcell.childNodes[0].selectedIndex = 0;
                  break;
       	}     
	}
 
}



and my code trying to get the data to store them into a table in the database :

$answerTxt = UserInput::getPostVar('answerTxt'); //will get the values which were entered into all the textboxes
						$choice = UserInput::getPostVar('choice'); //the values of the radio buttons

						foreach($choice as $k =>$v)
						{
							foreach($answerTxt as $key => $value )
							{							
								DBQuestions::insertPossibleAnswer($connection);	//executes an sql query								DBQuestions::insertMCQAnswer($connection,$answerTxt[$key],$choice[$k]);//executes an sql query
															
							}
						}


Lets say that when the radio button is selected in the table should be stored 1 otherwise should be stored 0.
The table in my table should be like this (example):
PK1 , PK2, TextBox , RadioButton
1 1 hello 1
1 2 hi 0
1 3 salute 0
1 4 sagionara 0

and so on.

But with the above code I get:
PK1 , PK2, TextBox , RadioButton
1 1 hello 1
1 2 hi 1
1 3 salute 1
1 4 sagionara 1

I believe this is because I have set a static value for my radio button in my html code.
Any idea on how I can solve this?

Thank you in advance for your help :)


The problem is that every time that the code is executed in the database

Additional comment

I must have done some wrong move with the mouse cause a phrase was cut. What I was trying to say is that:

The problem is that every time that the code is executed in the database is stored 1 even if the corresponding button is not selected.

Is This A Good Question/Topic? 0
  • +

Replies To: How to learn which dynamic radio button is selected

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,085
  • Joined: 08-August 08

Re: How to learn which dynamic radio button is selected

Posted 30 December 2011 - 07:34 AM

When you send data to the server using get or post PHP receives it in using the name as the key. I don't see any names used for added items.
Was This Post Helpful? 0
  • +
  • -

#3 DeadScream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-November 11

Re: How to learn which dynamic radio button is selected

Posted 30 December 2011 - 08:57 AM

what do you mean by saying that i have not used any names? I have set as a name for my radio button an array list:
<input type="radio" name="choice[]" value="1" /> 

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,085
  • Joined: 08-August 08

Re: How to learn which dynamic radio button is selected

Posted 30 December 2011 - 09:18 AM

Sorry, I was looking for it in your Javascript. What do you get for values of $k?
Was This Post Helpful? 0
  • +
  • -

#5 DeadScream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-November 11

Re: How to learn which dynamic radio button is selected

Posted 30 December 2011 - 09:32 AM

I was thinking that maybe if I search in any value of the radio button that it will return me 1 only for the one that is selected but apparently not.

I also tryed doing:
foreach($answerTxt as $key => $value )
                            {                          

                                DBQuestions::insertPossibleAnswer($connection); //executes an sql query                             DBQuestions::insertMCQAnswer($connection,$answerTxt[$key],$choice[$key]);//executes an sql query

                            }



but it returns 1 for the first value that is entered in the table :/
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,085
  • Joined: 08-August 08

Re: How to learn which dynamic radio button is selected

Posted 30 December 2011 - 09:42 AM

But what are you getting for $k?
Was This Post Helpful? 0
  • +
  • -

#7 DeadScream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-November 11

Re: How to learn which dynamic radio button is selected

Posted 30 December 2011 - 11:23 AM

I get alway 1 from $k
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,085
  • Joined: 08-August 08

Re: How to learn which dynamic radio button is selected

Posted 30 December 2011 - 11:58 AM

$k should be the name of the choice, so that isn't getting set properly. What's $v?
Was This Post Helpful? 0
  • +
  • -

#9 DeadScream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-November 11

Re: How to learn which dynamic radio button is selected

Posted 30 December 2011 - 12:41 PM

it is supposed to be the value of the radio button
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,085
  • Joined: 08-August 08

Re: How to learn which dynamic radio button is selected

Posted 30 December 2011 - 01:42 PM

I know that. ;)
I'm asking what you're actually receiving.
By the way, I don't see where you actually use $k, $v, $key, or $value because the code you've posted has the line that uses them commented out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1