最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - php, how to run a link when check box checked? - Stack Overflow

programmeradmin0浏览0评论
<input type="checkbox" name="checket" id="checket" />
$url  = 'test';

and when i check it i want it to go to a url: href='$url' and remember that has been checked.

any ideas?

thanks

edit: maybe

if ($('#checket:checked').val() !== undefined) {

// Insert code here. }

<input type="checkbox" name="checket" id="checket" />
$url  = 'test.';

and when i check it i want it to go to a url: href='$url' and remember that has been checked.

any ideas?

thanks

edit: maybe

if ($('#checket:checked').val() !== undefined) {

// Insert code here. }

Share Improve this question edited May 16, 2011 at 21:33 user680786 asked May 16, 2011 at 21:22 PatrioticcowPatrioticcow 27.1k76 gold badges221 silver badges339 bronze badges 4
  • 2 PHP is a server side only language. It can generate your page, but can't make it dynamic. You'll have to use javascript for that – Riccardo Galli Commented May 16, 2011 at 21:26
  • Your question is too vague. If you want to to happen the moment you check the box, that has to be done through JavaScript. If you want it to simply be remembered when you submit the form, that's an easy enough thing to handle in the PHP, and I don't see the need to step you through that. – Andrew Commented May 16, 2011 at 21:26
  • Use javascript for client side scripting – Ibu Commented May 16, 2011 at 21:27
  • Your question is not clear enough. You want to remember which option has been checked? At what point? do you want the new page to know about the selection or do you want the previous page to remember if the user access it again? is the user submitting the page? or do you want the page to go to a new address once the checkbox has been clicked? please specify more.... – Hanan Commented May 16, 2011 at 21:28
Add a ment  | 

5 Answers 5

Reset to default 6

Without using a JS library like jquery or mootools, here's how to do it in barebones JS:

<input
    type="checkbox"
    value="<?php echo htmlspecialchars($url) ?>"
    name="checket"
    onClick="if (this.checked) { window.location = this.value; }"
/>

Note that I've split the tag across multiple lines so it's easier to read. basically, embed the desired url into the checkbox's value field, then put an onclick handler that'll read out that URL and feed it to window.location when the checkbox bees checked.

Load the jquery library and then use the following code:

$('input[type=checkbox]#checket').click(function() {
    if($(this).is(':checked')) {
       $.get('urlhere', function(data) {
          // Process return data here
       });
    }
});

The way I see it you have a couple options.

1) Open the link in a new window using javascript and an onclick

http://wpcult./open-external-links-in-a-new-window/

2) Set a cookie that stores the data and check for the existence of the cookie

http://www.w3schools./php/php_cookies.asp

3) Store the data in the session variable

http://www.w3schools./php/php_sessions.asp

If you have more than one checkbox, put the URL in the value of the checkbox and add a class to all the checkboxes you want to do this:

<input type="checkbox" name="checket" id="checket" class="checkedurl" value="<?=$url?>" />
<script type="text/javascript">
$(document).ready(function(){
   $('input.checkedurl').click(function() {
      if ($(this).is(':checked')) {
          var checkurl = $(this).val();
          var postvars = "url="+checkurl;
          $.ajax({        
             url: "scripttostore.php",
             type: 'post',
             data: postvars,
             cache: false,
             success: function () {
                location.href = checkurl;
             }
          });
      }
});
</script>

If it's all checkboxes on the page you don't have to use the class, you could just use $('input[type=checkbox]')

The scripttostore.php would be what you use to store the url information.

you can only do it on the client side

<input type="checkbox" name="checket" id="checket" onclick="location.href='test.'"/>
发布评论

评论列表(0)

  1. 暂无评论