kemosabe kemosabe - 1 year ago 75
jQuery Question

Change form selection based on referrer

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.

Answer Source
<?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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download