0

EDIT 3: Ahha... some progress. The problem was actually in some code below the divs that I left out (updated HTML below to reflect). The second div contains a "required" input, so simply changing the div to display=none via javascript doesn't actually make the page entirely ignore the hidden Div. So a slight change to the angle of my question - how would I adjust the code below to completely ignore the hidden div, so that the required field is not read?

EDIT 2:I have tried removing the 2nd block of JS code below and inserting the dynamic PHP code directly into the input and div tags to change the display - still no luck.

EDIT 1: Just to confirm, the solution to a similar question doesn't work in this case: Auto checked radio buttons and php form processing - How to avoid blank field? it seems that the Chrome team have made changes in later versions that make this solution redundant.

A user on the website can select either Points or Stamps - I get the value from the Database via PHP and the form should have the relevant radio button checked and only show the Div related to that radio button.

Everything works fine, however, unless I manually change the radio button it will not let me POST the form i.e. I cannot post with value selected automatically from the DB - it seems a similar problem to this (Chrome Browser Ignoring AutoComplete=Off) - but no matter what I do with Chrome autocomplete it doesn't work. Also the page is recognising the radio as checked because it shows my dot in the right place. (EDIT 3: still unsure why this works in Mozilla but not Chrome - but latest Edits make this less important)

Heres my JS that shows the right Div if a radio button is changed:

$(function () {
          var $divs = $('#option > div');
           $('input[type=radio]').change(function() {
                  $divs.hide();
                  $divs.eq( $('input[type=radio]').index( this ) ).show();
              });
    });

This JS sets the correct radio button based on DB and shows the correct div when the page loads:

$(function() {
        var $radios = $('input:radio[name=selection]');
        var $type = "<?php echo $type; ?>";


        if($type === 'points') {
            $radios.filter('[value=points]').prop('checked', true);
            document.getElementById('pointsdiv').style.display = 'block';
            document.getElementById('stampdiv').style.display = 'none';

        }
        else if($type === 'stamp') {
            $radios.filter('[value=stamp]').prop('checked', true);
            document.getElementById('stampdiv').style.display = 'block';
            document.getElementById('pointsdiv').style.display = 'none';

        }
    });

My HTML and PHP:

$type = $_SESSION['user']['type']; //Note: This is actually set at the very top of the page i.e. before the JS

<form autocomplete="off" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" role="form">

                <input type="radio" id="selection" name="selection" value="points"></input><label>Points</label>
            &nbsp;
            &nbsp;    
                <input type="radio" id="selection" name="selection" value="stamp"></input><label>Stamps</label><br></br>


                <div id="option"> 

                        <div id="pointsdiv"> 
                            //Points related information here
                           <input type="text" required="required"/>
                        </div>
                        <div id="stampdiv"> 
                            //Stamp related information here - below input is required so when pointsdiv is displayed, this entire div should not even load
                          <input type="text" required="required"/>
                        </div>


                   </div>
<button type="submit" class="button">Save</button>
</form>

Anyone have any ideas how I can load this page and post the form if I don't change the radio button - i.e. if it loads with "Points" checked and I click the Save button?

Community
  • 1
  • 1
contool
  • 941
  • 2
  • 15
  • 26

1 Answers1

0

Input radio groups must have the same name but not the same id, so you should change id="selection" for one of them.

If everything is in the same page and the page is in php, then you have to wait for dom ready before setting input values.

There is a dynamic way without using javascript :

<input type="radio" <?=$type =='points'?='checked="checked"':''?> id="selection" name="selection" value="points">

The same thing you can do with the other radiobutton and also with the divs pointsdiv and stampdivs

Hidran
  • 328
  • 1
  • 5
  • Thanks. But no joy. I removed my entire set of JS in the second block in the question. I changed the ID's of the radios as suggested, and I inserted the php into the inputs like this: /> etc... and in the divs as follows:
    > etc... Still no luck - the correct radio & div are being displayed, but unless I click on a different radio, and then back on the original, the form won't POST
    – contool May 15 '14 at 10:52