Patrioticcow
Patrioticcow

Reputation: 27048

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

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

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

Upvotes: 0

Views: 9198

Answers (5)

fanfavorite
fanfavorite

Reputation: 5199

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.

Upvotes: 0

Marc B
Marc B

Reputation: 360702

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.

Upvotes: 6

Ibu
Ibu

Reputation: 43820

you can only do it on the client side

<input type="checkbox" name="checket" id="checket" onclick="location.href='test.com'"/>

Upvotes: -2

Swift
Swift

Reputation: 13188

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

Upvotes: 0

Jeroen
Jeroen

Reputation: 13257

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
       });
    }
});

Upvotes: 1

Related Questions