0

I have a form in which a user is asked to select one of two items. I want to make it so that if they come from a specific page on my site one of the options is already selected for them. I can't seem to do this without breaking my form.

Here's what the form element looks like:

<div class="container-fluid selectLoanPurpose">
    <div>
        <div class="col-xs-6">
            <label class="choose"><input type="radio" name="reason1" value="one" required><span>reason1</span></label>
        </div>
        <div class="col-xs-6">
            <label class="choose"><input type="radio" name="reason2" value="two" required><span>reason2</span></label>
        </div>
    </div>
</div>

I have tried:

<div class="container-fluid selectLoanPurpose">
    <div>
        <div class="col-xs-6">
        <?php if ($_SERVER['PATH_INFO'] == '/reasons') { ?>
            <label class="choose"><input type="radio" name="reason1" value="one" required checked><span>reason1</span></label>
        <?php } else { ?>
            <label class="choose"><input type="radio" name="reason1" value="one" required><span>reason1</span></label>
        <?php } ?>
        </div>
        <div class="col-xs-6">
            <label class="choose"><input type="radio" name="reason2" value="two" required><span>reason2</span></label>
        </div>
    </div>
</div>

I have also tried using $_SERVER['HTTP_REFERRER'] and $_SERVER['REQUEST_URI']. None of them have worked and my form no longer loads, I just get a white screen.

kemosabe
  • 144
  • 13
  • Please explain what you mean by "breaks". What happens? Also, I don't know if you tried that exact code, but you're missing a { on `` this line – ishegg Aug 17 '17 at 04:46
  • Thanks for the feedback, I have edited my question to add the bracket and explain that the form just doesn't load when I run that code. Yes, I've tried the code. I'll try it again to make sure the missing bracket wasn't the issue. – kemosabe Aug 17 '17 at 04:50
  • if user want check one of them means you need to keep both radio name attribute to same name . and differentiate by the value – JYoThI Aug 17 '17 at 04:51
  • The missing curly brace was the issue as far as the form breaking, but it still doesn't select the option I want it to select. – kemosabe Aug 17 '17 at 04:51
  • If the page doesn't load at all its likely you have PHP errors turned off in your development environment, firstly view source and see if anything is coming through at all - then turn errors on to see what's going wrong https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Jye Lewis Aug 17 '17 at 04:52
  • Blank page probably means PHP error with display_errors set to Off. Add this to the top of the page: `ini_set('display_errors', 1); error_reporting(E_ALL);` and you'll get more information – ishegg Aug 17 '17 at 04:52
  • 1
    Can you see exactly what $_SERVER['PATH_INFO'] is outputting, put at the top of your script – Jye Lewis Aug 17 '17 at 04:53
  • When I place at the top of my script I get nothing coming back. – kemosabe Aug 17 '17 at 04:55
  • 1
    Use $_SERVER["REQUEST_URI"]; – ishegg Aug 17 '17 at 04:55
  • Ok, that time I got the /page that my form lives at. I need to get the page that the user was one before they came to my form. – kemosabe Aug 17 '17 at 04:56
  • 1
    Use $_SERVER["HTTP_REFERER"] then. – ishegg Aug 17 '17 at 04:57
  • Thank you everybody for your help! – kemosabe Aug 17 '17 at 05:05

1 Answers1

1
<?php $referrer = $_SERVER['HTTP_REFERER'];?>
<div class="container-fluid selectLoanPurpose">
    <div>
        <div class="col-xs-6">
        <?php if (strpos($referrer, '/reasons') > -1) { // here we am checking that what ever the URL is but should contain /reason ?>
            <label class="choose"><input type="radio" name="reason1" value="one" required checked><span>reason1</span></label>
        <?php } else { ?>
            <label class="choose"><input type="radio" name="reason1" value="one" required><span>reason1</span></label>
        <?php } ?>
        </div>
        <div class="col-xs-6">
            <label class="choose"><input type="radio" name="reason2" value="two" required><span>reason2</span></label>
        </div>
    </div>
</div>

Try this one.

Naveed Ramzan
  • 3,348
  • 3
  • 22
  • 27