davierc
davierc

Reputation: 276

How do I find what value to pass to uncheck a checkbox?

How do I pass a url and uncheck a checkbox in a post request.

The HTML looks like this:

<input type="checkbox" name="encodeURL" ID="encodeURL" checked="checked">

I am using requests, a 3rd party python library, like so:

payload = {'u': 'http://www.google.com', 'encodeURL': '0'}
r = requests.post('http://www.website.com/', data=payload)

So far setting 'encodeURL' to '0', 'unchecked', '' and 0 is NOT posting the request with the checkbox unchecked. How do I find what value to pass to 'encodeURL' in order to uncheck the checkbox?

Upvotes: 2

Views: 3902

Answers (2)

bereal
bereal

Reputation: 34292

SUMMARY - TL/DR

To uncheck a box - exclude it in the payload

payload = {'u': 'http://www.google.com'}
r = requests.post('http://www.website.com/', data=payload)

To check a box - include it in the payload

payload = {'u': 'http://www.google.com', 'allowCookies': 'on'}
r = requests.post('http://www.website.com/', data=payload)

EXPLANATION

One needs to distinguish html markup (where the checkbox is set by default in your example) from the server-side behaviour, which is completely orthogonal. What you apparently want to do, is to mimic the behaviour of a browser when you submit the form with the unchecked checkbox. In this case, the server would not receive any encodeURL parameter whatsoever, just omit it from the request.

See also the HTML Spec

When a form is submitted, only "on" checkbox controls can become successful.

UPDATE

After investigating your particular case with TamperData plugin, I figured out that it might have to do with how speed-limit handles cookies, so that's not a requests library's bug, just a site peculiarity. This code seems to work:

s = requests.session()
s.get('http://speed-limit.info/index.php')  # getting the cookies
response = s.post('http://speed-limit.info/includes/process.php?action=update', 
                  data={'u': 'stackoverflow.com', 'allowCookies': 'on'},
                  allow_redirects=True)

Then the encodeURL in the response is unchecked (I hope, that's what you meant).

Upvotes: 3

Nilam Doctor
Nilam Doctor

Reputation: 491

// Check

document.getElementById("encodeURL").checked = true;

// Uncheck

document.getElementById("encodeURL").checked = false;

jQuery (1.6+):

// Check

$("#encodeURL").prop("checked", true);

// Uncheck

$("#encodeURL").prop("checked", false);

Upvotes: -1

Related Questions