Link to home
Start Free TrialLog in
Avatar of kuclu
kuclu

asked on

Disable a radio-button if another radio button is selected

I have a form that is spread over a number of pages. A field-forwarder is used to forward the selected entries from one page to the next, until the user submits the form. Now i would like to ask if is it possible to have a radio-button disabled, if another radio-button on a previous page is selected? Below is an example of what i have..


//==================================================
<FORM method="post" action="database.php" id="myform">

<INPUT TYPE="radio" NAME="material" VALUE="iron">Iron
<INPUT TYPE="radio" NAME="material" VALUE="wood">Wood
<INPUT TYPE="radio" NAME="material" VALUE="plastic">Plastic
<INPUT TYPE="radio" NAME="material" VALUE="glass">Glass

<INPUT TYPE="submit" id="submit" value="Proceed >>">
</FORM>


//Next page

<FORM method="post" action="database.php" id="myform">

<INPUT TYPE="radio" NAME="process" VALUE="injection_moulding">Injection Moulding
<INPUT TYPE="radio" NAME="process" VALUE="blow_moulding">Blow Moulding
<INPUT TYPE="radio" NAME="process" VALUE="drilling">Drilling
<INPUT TYPE="radio" NAME="process" VALUE="casting">Sand Casting

<INPUT TYPE="submit" id="submit" value="Proceed >>">
</FORM>

//===============================================================

Now what i want is this: If a user chooses "Plastic" from the first form, the next form would have only the first two values enabled (i.e. drilling and casting are DISABLED).

How can I do this?
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

1. do not have the same IDs

2. try this (forms[1] means the second form on the page):

<script>
function doIt(theChk) {
   var chk = theChk.name=="plastic")
   document.forms[1].process[0].disabled=chk;
   document.forms[1].process[1].disabled=chk;
}
</script>

<INPUT TYPE="radio" NAME="material" VALUE="iron" onClick="doIt(this)">Iron
<INPUT TYPE="radio" NAME="material" VALUE="wood" onClick="doIt(this)">Wood
<INPUT TYPE="radio" NAME="material" VALUE="plastic" onClick="doIt(this)">Plastic
<INPUT TYPE="radio" NAME="material" VALUE="glass" onClick="doIt(this)">Glass
You can do this by looking at the values forwarded by the 'field-forwarder'.  Like:

<script>
function disableProcess(){
if(plastic){
  document.form[0].Process[2].disabled=true;
  document.form[0].Process[3].disabled=true;
}
}
</script>

<body onLoad="disableProcess()">


I don't know how your forwarder works, so the above is just a guess as to your field name.
My previous solution would go on the SECOND page.
Ahh, sorry I misunderstood
Avatar of kuclu
kuclu

ASKER

thanks barry62. This is my field forwarder. actually it was downloadable and i customised it for my needs. Now can you please tell me exactly what i need to do to solve my problem?

======================================
<?php
function field_forwarder() {
    global $_POST, $rEM979, $FFoutputType;
    $fieldForwarder = '';
    /* get the arguments passed */
    $argList = func_get_args ();

    /* globalize any other set of instructions */
    if (count ($argList)) {
        eval ('global $' . $argList[count($argList)-1] . ';');
    }
     
    /* set the default set of values to convert */
    if(count($argList)==0) {
        /* if the function is initially passed without
           parameter we're looking in $_POST */
        $argList[0] = '_POST';
        $startValue = $_POST;  
        if (sizeof ($startValue) == 0) {
            return false;
        }
    } elseif (count ($argList) == 1) {
        eval ('$rEM979["' . $argList[0] . '"] = $'  
              . $argList[0] . ';');
        $argList[0] = 'rEM979';
        $startValue = $rEM979;
    } elseif (count ($argList) == 2) {
        eval ('$startValue = $' . $argList[1] . '["'  
              . $argList[0] . '"];');
    } else {
        for($e = count($argList) - 2; $e >= 0; $e--) {
            $intersperse .= '["' . $argList[$e] . '"]';
        }
        eval ('$startValue = $' . $argList[count($argList)-1]  
              . $intersperse . ';');
    }

    foreach($startValue as $n => $v) {
        if (is_array ($v)) {
            /* call the function again */
            $shiftArguments = '';
            for($w = 0; $w <= count ($argList) - 1; $w++) {
                $shiftArguments .= '"' . $argList[$w] . '", ';
            }
            $shiftArguments = substr ($shiftArguments, 0,  
                                     strlen ($shiftArguments) - 2);
             
            eval ('$fieldForwarder .= field_forwarder("' . $n . '"'  
                  . substr(',',0,strlen($shiftArguments)) . ' '  
                  . $shiftArguments . ');');
                         
        } else {
            /* we have an root value finally */
            if (count ($argList) == 1) {
                /* actual output */
                flush();
                if ($FFoutputType == 'print') {
                    $fieldForwarder .= "\$$n = '$v';\n";
                } else {
                    $fieldForwarder .= "<input type=\"hidden\" " 
                                    . "name=\"$n\" value=\""  
                                    . htmlentities(stripslashes($v))  
                                    . "\">\n";
                }
            } elseif (count ($argList) >1 ) {
                $indexString = '';
                for($g = count ($argList) - 3; $g >= 0; $g--) {
                    $indexString .= '['  
                                 . ((!is_numeric ($argList[$g])
                                 and $FFoutputType == 'print')
                                 ? "'" : '')
                                 . $argList[$g]
                                 . ((!is_numeric ($argList[$g])
                                 and $FFoutputType == 'print')
                                 ? "'" : '')
                                 . ']';
                }
                $indexString .= '['  
                             . ((!is_numeric ($n)  
                             and $FFoutputType == 'print')  
                             ? "'" : '') . $n  
                             . ((!is_numeric ($n)  
                             and $FFoutputType == 'print')  
                             ? "'" : '') . ']';
                /* actual output */
                flush();
                if ($FFoutputType == 'print') {
                    $fieldForwarder .= "\${$argList[count($argList)-2]}"
                                    . "$indexString = '$v';\n";
                } else {
                    $fieldForwarder .= "<input type=\"hidden\" name=\""
                                    . "{$argList[count($argList)-2]}"
                                    . "$indexString\" value=\""  
                                    . htmlentities(stripslashes($v))  
                                    . "\">\n";
                }
            }
        }        
    }
    return $fieldForwarder;
}
?>
i don't know anything about php.  does the forwarder create a javascript array?  Is that array named argList?


What is the array structured like when the forwarder finishes it?  If I could see some data, I could give you a precise solution in javascript.
Avatar of kuclu

ASKER

i got the php code from http://www.zend.com/zend/spotlight/code-gallery-wade7.php?print=1 , from where you can have an explanation of its functionality. The field forwarder does not create a javascript array. It determines how many arguments were called and responds to each one appropriately. Then, it will process each argument, sent to it and extract the values, and place the retreived values in hidden fields.

After the user submits the entries of the multi-page form, the results are stored in a database, once again using php:

<?php

$material = $_POST['material'];
$process= $_POST['process'];
...
...

$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db ("username", $con);  

$sql="UPDATE table SET
material = '$material',
process = '$process',
?>


I hope this has answered your questions Barry62.
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So I get a "B" in closing this?