checkbox checked?

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 6567 Views - Last Post: 17 August 2012 - 04:33 AM Rate Topic: -----

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

checkbox checked?

Posted 08 August 2012 - 06:12 PM

I have an HTML page containing checkboxes:

<td><input type="checkbox" value="" id='jq_001'><span class="jq" id="4_s">1.90</span></td>
<td><input type="checkbox" value="" id='jq_002'><span class="jq" id="4_s">1.70</span></td>
<td><input type="checkbox" value="" id='jq_003'><span class="jq" id="4_s">3.20</span></td>
<td><input type="checkbox" value="" id='jq_004'><span class="jq" id="4_s">2.60</span></td>


Now I want to detect user's selections.
1, do I have to put all the checkboxes in a form like
<form method="post" action="check-selection.php">
? and add a submit button like
<input type="submit" name="formSubmit" value="Submit" />
?
2, how do I determine which is checked in the php?

if(isset($_POST['formSubmit'])
{
    if($_POST['jq_001']) echo '1.90';
    if($_POST['jq_002']) echo '1.70';
    if($_POST['jq_003']) echo '3.20';
    if($_POST['jq_004']) echo '2.60';
}

This post has been edited by Dormilich: 08 August 2012 - 10:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: checkbox checked?

#2 ZevhEr  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 38
  • Joined: 18-July 09

Re: checkbox checked?

Posted 08 August 2012 - 07:39 PM

Add Value and Name to your checkbox

<html>
    
    <form method="post" action="check-selection.php">
  <td><input type="checkbox" name="jq_001" value="1.90" id='jq_001'><span class="jq" id="4_s">1.90</span></td>
<td><input type="checkbox" name="jq_002" value="1.70" id='jq_002'><span class="jq" id="4_s">1.70</span></td>
<td><input type="checkbox"name="jq_003" value="3.20" id='jq_003'><span class="jq" id="4_s">3.20</span></td>
<td><input type="checkbox" name="jq_004"  value="2.60" id='jq_004'><span class="jq" id="4_s">2.60</span></td>      
    <td><input type="submit" name="formSubmit" value="Submit"/></td>    
    </form>


</html>



check-selection.php
<?php

if(isset($_POST['formSubmit']))
{
    if($_POST['jq_001']) echo '1.90';
    if($_POST['jq_002']) echo '1.70';
    if($_POST['jq_003']) echo '3.20';
    if($_POST['jq_004']) echo '2.60';
}

?>


Was This Post Helpful? 3
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: checkbox checked?

Posted 08 August 2012 - 10:42 PM

some notes:
- tables are not for layout, they are for tabular data
- <td> is only allowed inside <tr>
- if the value of the checkbox is not used, why put it into HTML?
- testing for a submit button’s name is no guarantee, the the values of interest exist. it is known that some versions of IE do not send the submit button’s name at all
- *always* test for the data you want to use
- *always* validate user provided data

in case of floats, filter functions provide additional help.
// returns null if not set
// returns false if not a float
// otherwise returns the passed float
$jp_1 = filter_input(INPUT_GET, "jp_001", FILTER_VALIDATE_FLOAT);

Was This Post Helpful? 3
  • +
  • -

#4 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: checkbox checked?

Posted 09 August 2012 - 02:21 AM

Is it possible that I can detect the checkboxes checked without adding a submit button?
If so, I do not even need to put all the checkboxes in a form?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: checkbox checked?

Posted 09 August 2012 - 02:45 AM

form rules about checkboxes
- unchecked boxes are never submitted
- form elements (including checkboxes) outside the <form> are not submitted
- form elements (including checkboxes) without a name attribute are not submitted
- a sequence of checked boxes (bearing the same name) is only read in as array in PHP, if the name adheres to PHP’s array notation

This post has been edited by Dormilich: 09 August 2012 - 02:47 AM

Was This Post Helpful? 1
  • +
  • -

#6 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: checkbox checked?

Posted 09 August 2012 - 02:56 AM

Are the form tags in this thread just omitted?
http://www.dreaminco...eckbox-checked/

it looks almost like he never used the form tags since he put the php code inside the input tags?
Was This Post Helpful? 0
  • +
  • -

#7 ZevhEr  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 38
  • Joined: 18-July 09

Re: checkbox checked?

Posted 09 August 2012 - 02:59 AM

If you just want to show what is checked in the current page. You can do this with Javascript.
<html>
<head>
<script type="text/javascript">
function check(checkbox)
  {

  if (document.getElementById(checkbox).checked) {
        document.getElementById("p"+checkbox).innerHTML = checkbox + " is checked";
    }
    else {
        document.getElementById("p"+checkbox).innerHTML = "";
    }
}

</script>
</head>
<body>

<input type="checkbox" id="check1" onclick="check('check1')">Check1</input>
<input type="checkbox" id="check2" onclick="check('check2')">Check2</input>
<input type="checkbox" id="check3" onclick="check('check3')">Check3</input>
<p id="pcheck1"></p>
<p id="pcheck2"></p>
<p id="pcheck3"></p>
</body>
</html>


Was This Post Helpful? 1
  • +
  • -

#8 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: checkbox checked?

Posted 09 August 2012 - 09:49 AM

View PostZevhEr, on 09 August 2012 - 02:59 AM, said:

If you just want to show what is checked in the current page. You can do this with Javascript.
<html>
<head>
<script type="text/javascript">
function check(checkbox)
  {

  if (document.getElementById(checkbox).checked) {
        document.getElementById("p"+checkbox).innerHTML = checkbox + " is checked";
    }
    else {
        document.getElementById("p"+checkbox).innerHTML = "";
    }
}

</script>
</head>
<body>

<input type="checkbox" id="check1" onclick="check('check1')">Check1</input>
<input type="checkbox" id="check2" onclick="check('check2')">Check2</input>
<input type="checkbox" id="check3" onclick="check('check3')">Check3</input>
<p id="pcheck1"></p>
<p id="pcheck2"></p>
<p id="pcheck3"></p>
</body>
</html>



if I want to save the checkbox to a variable?

function check(checkbox)
{
    var selected = '<br>';
	if (document.getElementById(checkbox).checked) {
	    selected = selected+checkbox.tostring()+'<br>';
	}
}

This post has been edited by g37752: 09 August 2012 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: checkbox checked?

Posted 09 August 2012 - 12:45 PM

and what is that function supposed to do (besides creating an error)? currently it does absolutely nothing.
Was This Post Helpful? 0
  • +
  • -

#10 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: checkbox checked?

Posted 09 August 2012 - 02:27 PM

I am supposed to collect page visitor's checkbox selections and save it to database. So I need to be able to save it to a variable.
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: checkbox checked?

Posted 09 August 2012 - 02:32 PM

but you have to do that on the server. and Javascript is executed on the client. so a JS variable will not help in that matter.
Was This Post Helpful? 1
  • +
  • -

#12 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: checkbox checked?

Posted 09 August 2012 - 03:57 PM

The website I see works like this:

1, matrix of checkboxes
2, use innerHTML to collect user checks of checkboxes
3, display checkbox selections in a form, action goes to a java servlet .jhtml file.

So java servlet is able to collect client side selections to send to server? but php cannot do that?
Was This Post Helpful? 0
  • +
  • -

#13 ZevhEr  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 38
  • Joined: 18-July 09

Re: checkbox checked?

Posted 09 August 2012 - 07:34 PM

You can use simple JQuery to automatically send the data to php script but aware your code might not works if the javascript is disabled in browser.
<html>
<head>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.6.2.min.js">
</script>
</head>
<body>
<input type="checkbox" name="option1" value="Milk">Milk</input>
<input type="checkbox" name="option2" value="Sugar">Sugar</input>
<input type="checkbox" name="option3" value="Chocolate">Chocolate</input>
</body>
</html>
<script type="text/javascript">
        $("input:checkbox").click( function(){
   var checked = $(this).attr('checked');
   if(checked){
      var value = $(this).val();
      $.post('save.php', { value:value }, function(data){
          // data = 0 - means that there was an error
          // data = 1 - means that everything is ok
          if(data == 1){
             // Do something or do nothing :-)
             alert('Data was saved in db!');
          }
      });
   }
});
</script>



save.php
<?php
if ($_POST && isset($_POST['value'])) {

    // db connection
    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    if (!$link) {
       // error happened
       print(0);
    }
    mysql_select_db('mydb');

    // sanitize the value
    $value = mysql_real_escape_string($_POST['value']);

    // start the query
    $sql = "INSERT INTO table (value) VALUES ('$value')";

    // check if the query was executed
    if(mysql_query($sql, $link)){
       // everything is Ok, the data was inserted
       print(1);    
    } else {
       // error happened
       print(0);
    }
}
?>


Was This Post Helpful? 2
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: checkbox checked?

Posted 09 August 2012 - 10:23 PM

I would just go for a simple form containing the checkboxes.
Was This Post Helpful? 0
  • +
  • -

#15 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: checkbox checked?

Posted 10 August 2012 - 01:03 AM

@ZevhEr - interesting jquery.

1, do i have to include the ajax library in the header? wordpress has a page template that send header before executing page, so that will become "warning, header already sent..?

2, suppose innerHTML already exists that gathers the checkbox selections and put them into a second form. the selections are somehow interpreted before being put into the form. (for example when "milk" is checked, maybe a "bycicle" is placed in the form.) It is the second form that I want to submit to the server. how do I gather the info in the second form?

I looked at the second form, and the source only shows lines of hidden input:

<form action="http://xxx.com/servlet/..">
<input type="hidden" value="" name="firstDate" id="jq_EndDat"/>
<input type="hidden" value="" name="dataVal" id="jq_dataVal"/>
....
</form>
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2