根据推荐人更改表单选择

时间:2017-08-17 04:44:47

标签: javascript php jquery

我有一个表单,要求用户选择两个项目中的一个。我想这样做,如果它们来自我网站上的特定页面,则已经为它们选择了一个选项。如果不打破我的形式,我似乎无法做到这一点。

这是表单元素的样子:

<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>

我试过了:

<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>

我也尝试过使用$ _SERVER ['HTTP_REFERRER']和$ _SERVER ['REQUEST_URI']。他们都没有工作,我的形式不再负载,我只是得到一个白色的屏幕。

1 个答案:

答案 0 :(得分: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>

试试这个。