0

I am doing this :

function GETVALUE() 
{
 if(document.getElementById("requirenewpage").checked == true)
    {
        document.getElementById("requirenewpage").value;    
        var cval= parseInt(document.getElementById("requirenewpage").value);

    }
}

HTML-----------------------------

<input type="checkbox" id="requirenewpage" unchecked   value= "GETVALUE();" >

I need to insert into a mysql table , 0 or 1, which is taken from the VALUE attribute of the checkbox.....but am not able to do it...please help???

Its always inserting 0 into the database, albeit am setting the value as 1 in the function GETVALUE().....

1
  • 4
    Your HTML is invalid. Inputs (if they are checkboxes or radio buttons) have a checked attribute (which should be specified as just the unquoted value checked). If it is to be unchecked, then don't specify the attribute. Commented Jul 15, 2011 at 13:19

3 Answers 3

3

Actually you don't need any of this i think. You can use this html:

  <input type="checkbox" id="requirenewpage"  value= "1" >

and the checkbox will send a value of 1 to the server if checked, otherwise it won't send anything (and the corresponding $_POST['requirenewpage'] or $_GET['requirenewpage'] won't be set).

If the checkbox is checked it's value is sent to the server and a key in the $_POST array (if you use POST) is created with the name of the checkbox and the value of hte checkbox.

you can do, serverside:

$chkboxval = 0;

if (isset($_POST['requirenewpage'])){
     $chkboxval = $_POST['requirenewpage'];
}
Sign up to request clarification or add additional context in comments.

9 Comments

----what will this do sir, ----- isset($_POST['requirenewpage'] , does it checks that checkbox is checked or unchecked , and iS TRUE, if checkbox is checked??
how do i take the value=1 of checkbox in a variable......i did this ----- $val = $_POST('requirenewpage'); ----------will $val be 1 in this case?
Yes isset($_POST['requirenewpage']) returns true if the checkbox is checked because if the checkbox is checked it's value is sent to the server and a key in the $_POST array (if you use POST) is created with the name of the checkbox. and yes $val will be 1 (or whatever value you set as the value of the checkbox)
so , there is no need to take a $chkboxval variable, which you have taken in your code, i will just insert $val=$_POST('requirenewpage') in my mysql table
No you should always check if it's set because otherwise it's key is undefined and you will get a notice: i updated the answer you should do as written there.
|
1

I'm shocked that nobody has answered this correctly yet... Change the checkbox to the following:

<input type="checkbox" id="requirenewpage" name="requirenewpage" value= "1" />

The ID of an input element is used for script access and styling only, if you want to submit the element in a form it must have a name attached to it.

Comments

1

You are wrong with your html. Change your checkbox code from

<input type="checkbox" id="requirenewpage" "unchecked"   value= "GETVALUE();" >

to

<input type="checkbox" id="requirenewpage" onclick= "GETVALUE();" >

1 Comment

you are right with this, but i think that's a lot more complicated than using standard html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.