开发者

Auto-select a checkbox depending on referring page value?

I'm working on an email series signup form that needs to have a checkbox pre-checked depending on the page the user is coming from.

It looks like the form is already set up to track which page the user is coming from, as it contains this:

<input type="hidden" name="referring_page" value="<?php if (isset($_POST['referring_page'])) { echo $_POST['referring_page']; } else { echo $_SERVER['HTTP_REFERER']; }?>" />

The checkboxes all look like this:

<div class="emailOption"><input type="checkbox" name="seo" id="seo" value="seo" /> SEO Series</div>
<d开发者_JAVA百科iv class="emailOption"><input type="checkbox" name="ppc" id="ppc" value="ppc" /> PPC Series</div>

I would like to add the attribute checked="yes" to a single checkbox depending on the referring page url. I'm new to both PHP and jQuery, but the site is using both so either would work for a solution.

Thanks a lot for any help!


I'd recommend PHP since you are pre-checking. That is to say there's no reason to do this with JavaScript after page load if you already have the logic and access to server-side code.

Similar to the hidden input:

<div class="emailOption">
  <input type="checkbox" name="seo" id="seo" value="seo"<?php echo (/*your logic goes here */) ? 'checked="checked"' : ''; ?>  /> SEO Series
</div>


You can use jQuery on this case.

$('input').filter(function(){
  return $(this).attr('name') == "<?php echo $valueGoesHere; ?>";
}).attr('checked,' true);

Try and tell me about your result. :)


Assuming by "from the URL" you mean that seo and ppc are post parameters, this is what you need:

<div class="emailOption"><input type="checkbox" name="seo" id="seo" value="seo" <?php echo (isset($_POST['seo']) ? 'checked' : '');?>/> SEO Series</div>
<div class="emailOption"><input type="checkbox" name="ppc" id="ppc" value="ppc" <?php echo (isset($_POST['ppc']) ? 'checked' : '');?>/> PPC Series</div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜