开发者

php, how to run a link when check box checked?

开发者 https://www.devze.com 2023-03-06 03:15 出处:网络
<input type=\"checkbox\" name=\"checket\" id=\"checket\" /> $url= \'test.com\'; and when i check it i w开发者_运维知识库ant it to go to a url: href=\'$url\'
<input type="checkbox" name="checket" id="checket" />
$url  = 'test.com';

and when i check it i w开发者_运维知识库ant 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. }


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 becomes 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.com/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.com/php/php_cookies.asp

3) Store the data in the session variable

http://www.w3schools.com/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.com'"/>
0

精彩评论

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