Page 1 of 1

If - Else problem

Posted: Mon Oct 10, 2011 5:30 am
by Spalton
Hi guys, new to the board, and new to PHP, hope you can help :)

I'm trying to retain the values that a user has clicked on radio buttons and still display them when the form reloads.

I have this code to keep the variable :

Code: Select all

//Check which PolicyType is checked

$FirstOnlineRNWLStatus = 'unchecked';
$OnlineRNWLStatus = 'unchecked';
$FirstOfflineRNWLStatus = 'unchecked';
$OfflineRNWLStatus = 'unchecked';
$OnlineNBStatus = 'unchecked';
$OfflineNBStatus = 'unchecked';

if (isset($_POST['post'])){

$selectedPolicyType = $_REQUEST["PolicyType"];


if ($PolicyType == '1stOnlineRNWL') 
{
	$FirstOnlineRNWLStatus = 'checked';
}
elseif ($PolicyType == 'OnlineRNWL')
{
	$OnlineRNWLStatus = 'checked';
}
elseif ($PolicyType == '1stOfflineRNWL')
{
	$FirstOfflineRNWLStatus = 'checked';
}
elseif ($PolicyType == 'OfflineRNWL')
{
	$OfflineRNWLStatus = 'checked';
}
elseif ($PolicyType == 'OnlineNB')
{
	$OnlineNBStatus = 'checked';
}
elseif ($PolicyType == 'OfflineNB')
{
	$OfflineNBStatus = 'checked';
}
}
and then this to enter the value in the correct radio button on reload:

Code: Select all

label>
              <input name="PolicyType" type="radio" id="PolicyType_3" value="1stOnlineRNWL" checked="<?php print $FirstOnlineRNWLStatus; ?>" />
              1st Yr Online RNWL</label>
            <br />
            <label>
              <input type="radio" name="PolicyType" value="OnlineRNWL" id="PolicyType_4" checked="<?php print $OnlineRNWLStatus; ?>"/>
              Online Renewal</label>
            <br />
            <label>
              <input type="radio" name="PolicyType" value="1stOfflineRNWL" id="PolicyType_5" checked="<?php print $FirstOfflineRNWLStatus; ?>" />
              1st Yr Offline RNWL</label>
            <br />
            <label>
              <input type="radio" name="PolicyType" value="OfflineRNWL" id="PolicyType_6" checked="<?php  print $OfflineRNWLStatus; ?>" />
              Offline RNWL</label>
            <br />
            <label>
              <input type="radio" name="PolicyType" value="OnlineNB" id="PolicyType_7" checked="<?php print $OnlineNBStatus; ?>"/>
              Online NB</label>
            <br />
            <label>
              <input type="radio" name="PolicyType" value="OfflineNB" id="PolicyType_8" checked="<?php  print $OfflineNBStatus; ?>"/>
              Offline NB</label>
            <br />
            <br />
This isn't working and I can't see why, no matter which radio is selected, when the form is reloaded, they are all unchecked other than the bottom (Offline NB) which is checked. It's as if it's completely ignoring all the 'If' statements.

Can anyone shed any light on this for me? Thankyou!

Re: If - Else problem

Posted: Mon Oct 10, 2011 6:03 am
by Spalton
Hi there, yeah it's passing the variable correctly, can output to a textbox no problem. I did amend it slightly as your question just highlighted I omitted the 'selected' from $selectedPolicyType in the If statements, but am still getting the same issue despite changing it.

Re: If - Else problem

Posted: Mon Oct 10, 2011 6:23 am
by Spalton
Switch / Case does exactly the same thing. Bizarre... it would make more sense if it did nothing :)

Re: If - Else problem

Posted: Mon Oct 10, 2011 6:32 am
by Spalton
I first load a html file, with the form on it, user inputs and submits, it then loads a 'duplicate' PHP page, with the same layout etc just doing some calculations based on the user inputs. All the other variables work fine, I'm getting a text box to display what the user inputted into it when first doing the form etc, it's just these radio buttons that are causing the issue. very puzzling, like i saoid I'd kinda understand if it did nothing, it's just wierd how it 'accepts' the bottom value each time.

Re: If - Else problem

Posted: Mon Oct 10, 2011 6:45 am
by Celauran
You're using $PolicyType in your comparisons, but it hasn't been defined anywhere.

Re: If - Else problem

Posted: Mon Oct 10, 2011 6:50 am
by Spalton
It's defined further up in the code, and I've amended the code sample Above to look like this :

Code: Select all

if (isset($_POST['post'])){

$selectedPolicyType = $_REQUEST["PolicyType"];


if ($selectedPolicyType == '1stOnlineRNWL') 
{
	$FirstOnlineRNWLStatus = 'checked';
}
elseif ($selectedPolicyType == 'OnlineRNWL')
{
	$OnlineRNWLStatus = 'checked';
}
elseif ($selectedPolicyType == '1stOfflineRNWL')
{
	$FirstOfflineRNWLStatus = 'checked';
}
elseif ($selectedPolicyType == 'OfflineRNWL')
{
	$OfflineRNWLStatus = 'checked';
}
elseif ($selectedPolicyType == 'OnlineNB')
{
	$OnlineNBStatus = 'checked';
}
elseif ($selectedPolicyType == 'OfflineNB')
{
	$OfflineNBStatus = 'checked';
}
}
Which does the same, and tried the switch/case option :

Code: Select all

switch($selectedPolicyType){
	case "1stOnlineRNWL":
	$FirstOnlineRNWLStatus = 'checked';
	break;
	
	case "OnlineRNWL":
	$OnlineRNWLStatus = 'checked';
	break;

	case "1stOfflineRNWL":
	$FirstOfflineRNWLStatus = 'checked';
	break;
	
	case "OfflineRNWL":
	$OfflineRNWLStatus = 'checked';
	break;

	case "OnlineNB":
	$OnlineNBStatus = 'checked';
	break;

	case "OfflineNB":
	$OfflineNBStatus = 'checked';
	break;
	
}
Which also does the same... just takes the last option (OfflineNB) as clicked and ignores any of the others, however when I output the variable $selectedPolicyType to a text box it shows the correct option that has been selected.

Very confused.

Re: If - Else problem

Posted: Mon Oct 10, 2011 8:36 am
by Spalton
Okay - I got a bit of code

Code: Select all

  <?php if((isset($_POST['PaymentMethod'])) && ($_POST["PaymentMethod"]=="Full")){echo "checked=\"checked\""; ?><?php } ?> />
from another site and inserted this into the actual radio groups section :

Code: Select all

 <input name="PaymentMethod" type="radio" id="PaymentMethod_0" value="Full" <?php if((isset($_POST['PaymentMethod'])) && ($_POST["PaymentMethod"]=="Full")){echo "checked=\"checked\""; ?><?php } ?> />
And this works great.

Is there a similar of using a chunk of code like this for dropdown boxes and checkboxes? As I said I a total noob at PhP and don't really understand what that code is doing so can't replicate/amend it for the other input types.

Thankyou!

Re: If - Else problem

Posted: Mon Oct 10, 2011 8:49 am
by ouchiko

Code: Select all

<input type="checkbox" <?=checkTest($_POST["PaymentMethod"],"Full","checked")?> />


// Looking for.. 2012
<select>
  <? for ($i=2011;$i<=2015;$i++){ ?>
   <option value="<?=$i?>" <?=checkTest($_POST["PaymentMethod"],"Full","selected")?>><?=$i?></option>
 <? } ?>
</select>


function checkTest( $variable, $value, $selectorType ){
 return ( $variable==$value ) ? "selectorType =\"selectorType \"":"";
}

Re: If - Else problem

Posted: Mon Oct 10, 2011 11:53 am
by Spalton
cheers mate,

I ended up using

Code: Select all

 <input name="ConfirmSaleClick" type="checkbox" id="ConfirmSaleClick" onclick="showform()" value="ConfirmSale" <?php echo empty($_POST['ConfirmSaleClick']) ? '' : ' checked="checked" '; ?>/>
is there anyway I can call a javascript function if this checkbox is checked 'automatically' by the PHP in the above example?

I appreciate this is a PHP forum but hope you can help :)

Thanks guys, really appreciate all the help today.

Re: If - Else problem

Posted: Tue Oct 11, 2011 11:03 am
by ouchiko

Code: Select all

<script>
<? if ( !empty($_POST['ConfirmSaleClick']) ) ?>
doMyFunkyFunction();
<? } ?>
</script>