checkbox checked?

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: checkbox checked?

Posted 10 August 2012 - 06:03 AM

View Postg37752, on 10 August 2012 - 10:03 AM, said:

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

a) that goes without saying
b) HTTP headers are different from HTML headers

though it is unpleasent advice, you need to learn the basics of how the web works. youíre constantly confusing basic things like client/server relation, form handling, HTML basics and other technical terms. without learning those your issues on that topic wonít go away.

This post has been edited by Dormilich: 10 August 2012 - 06:03 AM

Was This Post Helpful? 1
  • +
  • -

#17 ZevhEr  Icon User is offline

  • New D.I.C Head

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

Re: checkbox checked?

Posted 10 August 2012 - 10:40 AM

My Friend, if you keep asking like this without any effort to find it yourself. you will be just running in an infinite loop.
The solution is easy. just add the break to the loop;

How to add the break :
1. Try Googling. Sometimes googling find your solution faster than any good forum.
2. Find yourself an online tutorial. there are tons of these nowadays.
3. Get yourself one or two books for your references. reading too many will just confused you.

After you put that break. your effort will not go to waste, you will find your good result in the future, not just circling around in some infinite loop.

int x = 0;
string Learn() {
   while (x==0) {
       ask(x);
       break; // Without the break - Infinite Ask
   }
   return "Success";
}



Happy Coding.
Was This Post Helpful? 0
  • +
  • -

#18 g37752  Icon User is offline

  • D.I.C Head

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

Re: checkbox checked?

Posted 14 August 2012 - 02:23 PM

why is this not working? I am trying to just send a number which is not posted on the html form. (nothing in error console). the alert works fine. the URL works fine.

<script type="text/javascript">
    $("#btnOrder").click( function(){
		var value = numBets;
		//alert('bets= '+numBets);		
	      $.post('save.php', { bets:value }, function(data){
	          if(data == 1){
	             alert('Data was saved in db!');
	          }
	      });	   
	});
</script>


<?php
  .... connect to db..
   $num_game=mysql_real_escape_string($_POST['value']);  // i tried $_POST['bets']..
    // start the query
    $sql = "INSERT INTO orders (order_number,total_bets) VALUES ('5',,'$num_game')";
?>

Was This Post Helpful? 0
  • +
  • -

#19 xxxjj18  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 53
  • View blog
  • Posts: 167
  • Joined: 30-November 11

Re: checkbox checked?

Posted 14 August 2012 - 06:22 PM

As for an answer to your initial question, you could loop through each $_POST value and check to see if each checkbox is checked.. Give each checkbox a name such as "checkBox1", "checkBox2", and so on.

Then, in the code, check to see if the key of the $_POST element contains the string "checkBox";


foreach($_POST as $key => $value) {
    
    if(strstr($key, "checkBox")) {
        
        if($value) {
            //The checkBox was checked, do something with it
        }
        
    }

}




And for each checkbox you could have a hidden value with the value that goes with each checkBox, like; "cBoxValue1", "cBoxValue2", etc.

I hope this helps somewhat
Was This Post Helpful? 1
  • +
  • -

#20 g37752  Icon User is offline

  • D.I.C Head

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

Re: checkbox checked?

Posted 14 August 2012 - 08:50 PM

foreach $_POST seems the most convenient, but still cant get it to work..?

<form>
   <table cellspacing=5 cellpadding=5 border=1>
   <tr><td id="cell2_1" style="display:none;">      (turns display:inline after user action)
   <input name="ckbox[]" type="checkbox" id="trn2_1"  value="trn2_1 onclick="chooseBets(this.form)">2=1</input>
   </td><td id="cell3_1" style="display:none;">
   <input name="ckbox[]" type="checkbox" id="trn3_1"  value="trn3_1 onclick="chooseBets(this.form)">3=1</input>
   </td></tr>
   <tr><td colspan=5><input type="submit" name="btnOrder" id="btnOrder" value="place order"/></td></tr>
   </table>
</form>


<script type="text/javascript">
    $("#btnOrder").click( function(){

		var $form = $(this);
		serializedData = $form.serialize();
    $.ajax({
        url: "save2tdb.php",
        type: "post",
        data: serializedData,
        success: function(response, textStatus, jqXHR){
            // log a message to the console
            console.log("Hooray, it worked!");
        },
        error: function(jqXHR, textStatus, errorThrown){
            // log the error to the console
            console.log(
                "The following error occured: "+
                textStatus, errorThrown
            );
        },
        complete: function(){
        }
    });
	  
	});

</script>


save2tdb.php:
<?php
$num_bets=0;
$bets_selected="";
foreach($_POST as $key => $value) {	     
      if(strstr($key, "ckbox")) {	         
	    if($value) {
	         $num_bets++;
	         $bets_selected = $bets_selected.', '.$value;
	    }	         
      }	 
}
 
$sql = "INSERT INTO orders (order_date,bets_selected,total_bets) VALUES ('2012-08-13','$bets_selected','$num_bets')";
?>

This post has been edited by g37752: 14 August 2012 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#21 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: checkbox checked?

Posted 14 August 2012 - 10:40 PM

it doesnít work because you treat $_POST["ckbox"] as string value when it is an array.
Was This Post Helpful? 1
  • +
  • -

#22 g37752  Icon User is offline

  • D.I.C Head

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

Re: checkbox checked?

Posted 15 August 2012 - 02:34 AM

I changed it to below, but still nothing happened?

foreach($_POST as $key => $value) {
  if(strstr($key, "ckbox")) {	  	     
    $num_bets = count($value);
    for ($i=0;$i<$num_bets;$1++) 
    {   
       $bets_selected = $value[i].', '.$bets_selected;
    }  
    break;
  }
}

This post has been edited by g37752: 15 August 2012 - 02:37 AM

Was This Post Helpful? 0
  • +
  • -

#23 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: checkbox checked?

Posted 15 August 2012 - 12:22 PM

print out the SQL string before the query. what do you get there?
Was This Post Helpful? 0
  • +
  • -

#24 g37752  Icon User is offline

  • D.I.C Head

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

Re: checkbox checked?

Posted 15 August 2012 - 12:57 PM

it does not do anything even if I echo something at the very beginning of the php code. it just goes straight back to the blank form.
Was This Post Helpful? 0
  • +
  • -

#25 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: checkbox checked?

Posted 15 August 2012 - 10:48 PM

how do you execute the redirect (or whatever it is)?
Was This Post Helpful? 0
  • +
  • -

#26 g37752  Icon User is offline

  • D.I.C Head

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

Re: checkbox checked?

Posted 15 August 2012 - 11:46 PM

this is what I have done: http://methinks.info...testform0mt.php

1, form 1 (above) collects user clicks to populate form 2 (below), including writing the selections into a <span> element in form 2.
2, when the "order" button on form 2 is clicked, call jQuery to save <span> and checkboxes to db.
Was This Post Helpful? 0
  • +
  • -

#27 Commontoast  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 08-May 12

Re: checkbox checked?

Posted 17 August 2012 - 01:45 AM

The attempt you made from your first post was very close already, only a few tweaks needed.

Use name instead of id to name the input variables for PHP to use properly, id is good for the JS and CSS actions though.
Now when you give a value attribute to a checkbox it will have that value when you submit it, when you leave out the value attribute it will be marked "on" on submitting (when checked).

Also the checkbox name will not be present in the $_POST array if it is not checked.


Here is what i tested with ..
<?php
echo @print_r($_POST).'<br/>';

if(@$_POST['jq_001'] === 'on'){	echo '1';	}
if(@$_POST['jq_002'] === 'on'){	echo '2';	}
if(@$_POST['jq_003'] === 'on'){	echo '3';	}
if(@$_POST['jq_004'] === 'on'){	echo '4';	}
?>
<html>
<body>
<form method="post" action="index.php">
<input type="checkbox" name='jq_001'>1.90<br/>
<input type="checkbox" name='jq_002'>1.70<br/>
<input type="checkbox" name='jq_003'>3.20<br/>
<input type="checkbox" name='jq_004'>2.60<br/>
<input type="submit" value="submit" />
</form>
</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#28 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: checkbox checked?

Posted 17 August 2012 - 01:52 AM

@Commontoast: it is very bad practice to use the @ (error supression) operator. not only does it eat resources, it also prevents you from spotting problems and sometimes itís just not needed (like in line #2).

and I might add that your HTML page is invalid with the pre-HTML output.
Was This Post Helpful? 1
  • +
  • -

#29 Commontoast  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 08-May 12

Re: checkbox checked?

Posted 17 August 2012 - 03:00 AM

@Dormilich: for testing purposes this is however a very quick and easy way to show exactly what i want to g37752. I am assuming the exact code will not be used as is in the final version of his/her script :)
Was This Post Helpful? 0
  • +
  • -

#30 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: checkbox checked?

Posted 17 August 2012 - 04:33 AM

View PostCommontoast, on 17 August 2012 - 12:00 PM, said:

I am assuming the exact code will not be used as is in the final version of his/her script :)

From experience I can say that beginners usually want to "get the code working". whether the code is intended for a quick'n'dirty test doesnít matter. it works, why change it?

and especially for beginners, itís establishing bad habits. PHP doesnít have a bad reputation for no reason.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2