The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
Please vote in the Forum Structure Poll. Polling will close at 2PM EST on January 21, 2025.

Stupid PHP question

Caliban42Caliban42 Registered User regular
edited June 2008 in Help / Advice Forum
In a nutshell, here's the problem. I've got a form with a set of radio buttons and a single checkbox. I want to set a variable based on whether the checkbox is checked or not and another variable depending on which radio button is checked. I looked online to get some help and here's the code I ended up with for the checkbox.
$closed = "<script language=javascript>document.write(document.MainForm.close_issue.checked);</script>";
if ($closed == true)
{
	$status = "Closed";
} 

That works just fine. The radio buttons, however, are not being so cooperative. I adjusted the same code so it now looks like this:
for ($i=0;$i<9;$i++)
{
	$checked = "<script language=javascript>document.write(document.MainForm.source[$i].checked);</script>";
	if ($checked == true)
	{
		break;
	}		
}

With this code, it stops after reading the first radio button. If I change it to "if ($test == false)" it will read the entire set of radio buttons. So I guess what I need to know is if there is how to I get this working. Or, if there is an easier way of doing it that I'm overlooking.

Caliban42 on

Posts

  • RoundBoyRoundBoy Registered User regular
    edited June 2008
    when do you need this variable set ? PHP is a server side language, and javascript is dealing with the browser end of things... and they generally do not talk to each other .... except in the method you described.

    However... since php is server side, you can't send the state of the browser unless you do some sort of action... the most common on forms would be a submit. You can check the state of the checkboxes in a $_POST variable... and set the variable at that time.

    otherwise, you will need to look into some AJAX processing to muck with your variables, or submit to the same page, and pass the state of the variables in a initial $_POST or $_GET.


    99% of the time, if you need to set a variable based on a checkbox, then you can submit the form, and the script defined in the action will handle setting the variable... as should be there anyway to handle validation.

    RoundBoy on
    sig_civwar.jpg
    Librarians harbor a terrible secret. Find it.
  • Caliban42Caliban42 Registered User regular
    edited June 2008
    I'm not trying to change anything on the screen based on the radio or checkbox selections. What I want to do is set a couple of variables that will be passed when the submit button is clicked. With the checkbox if it is checked, I want to set a variable to show that that particular issue is in a closed status. With the radio buttons, I want to pull the value of the selected button and eventually get it saved in a database.

    I'm sure there are better ways of doing this, but this is the first time I've had to do anything with PHP so I'm still trying to get my head wrapped around it.

    Caliban42 on
  • RoundBoyRoundBoy Registered User regular
    edited June 2008
    Caliban42 wrote: »
    I'm not trying to change anything on the screen based on the radio or checkbox selections. What I want to do is set a couple of variables that will be passed when the submit button is clicked. With the checkbox if it is checked, I want to set a variable to show that that particular issue is in a closed status. With the radio buttons, I want to pull the value of the selected button and eventually get it saved in a database.

    I'm sure there are better ways of doing this, but this is the first time I've had to do anything with PHP so I'm still trying to get my head wrapped around it.

    for checkboxes:
    <input type='checkbox' name='foo[]' value='something'>
    

    notice the 'foo[]' .. on submit, iterate through the array and look for the value :
    if($_POST){
    $checkbox_array = $_POST['foo']
    foreach($checkbox_array){
     .... check goes here
    }
    }
    

    The radio button is identical, only that it can generally only have one value, while there can be many in checkboxes. You should also check for NO value in radio buttons as well.


    a google search would be a very good start :

    a friend

    RoundBoy on
    sig_civwar.jpg
    Librarians harbor a terrible secret. Find it.
Sign In or Register to comment.