<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Linha de comando.com - Tecnologia, Informação, Dicas e muito mais. &#187; checkbox</title>
	<atom:link href="http://www.linhadecomando.com/tag/checkbox/feed" rel="self" type="application/rss+xml" />
	<link>http://www.linhadecomando.com</link>
	<description>quem sabe eu possa te ajudar!</description>
	<lastBuildDate>Thu, 02 Feb 2012 09:42:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>PHP: Trabalhando o resultado obtido de uma consulta sql (checkbox)</title>
		<link>http://www.linhadecomando.com/php/php-trabalhando-o-resultado-obtido-de-uma-consulta-sql-checkbox</link>
		<comments>http://www.linhadecomando.com/php/php-trabalhando-o-resultado-obtido-de-uma-consulta-sql-checkbox#comments</comments>
		<pubDate>Thu, 19 Aug 2010 02:21:07 +0000</pubDate>
		<dc:creator>Tiago</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[consulta sql]]></category>
		<category><![CDATA[mysql_fetch_array]]></category>

		<guid isPermaLink="false">http://www.linhadecomando.com/?p=1612</guid>
		<description><![CDATA[Continuando a nossa batalha de códigos&#8230; vamos a mais um post destacando o tratamento dos dados retornados de uma consulta a base de dados. Vale lembrar, que sempre estarei abordando o banco de dados MySQL, ok?! Neste post eu disponibilizo a estrutura de uma tabela chamada &#8220;produtos&#8221; para utilização nos exemplos. Aproveitando a tabela &#8220;produtos&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>Continuando a nossa batalha de códigos&#8230; vamos a mais um post destacando o tratamento dos dados retornados de uma consulta a base de dados.</p>
<p>Vale lembrar, que sempre estarei abordando o banco de dados MySQL, ok?!</p>
<p><a href="http://www.linhadecomando.com/php/php-trabalhando-o-resultado-obtido-de-uma-consulta-sql-completo" target="_blank">Neste post</a> eu disponibilizo a estrutura de uma tabela chamada &#8220;produtos&#8221; para utilização nos exemplos.</p>
<p>Aproveitando a tabela &#8220;produtos&#8221; vamos acrescentar mais um campo. O campo será chamado &#8220;ativo&#8221; e receberá os valores 0 = não (ex: não exibir no site de vendas) e 1 = sim (exibir no site de vendas).</p>
<p><em>Veja como ficará a nossa tabela</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #990099; font-weight: bold;">IF <span style="color: #CC0099; font-weight: bold;">NOT</span> EXISTS</span> <span style="color: #008000;">`produtos`</span> <span style="color: #FF00FF;">&#40;</span>
  <span style="color: #008000;">`codigo`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">3</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #FF9900; font-weight: bold;">unsigned</span> <span style="color: #FF9900; font-weight: bold;">zerofill</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`descricao`</span> <span style="color: #999900; font-weight: bold;">varchar</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">COLLATE</span> utf8_unicode_ci <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`ativo`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">'0'</span><span style="color: #000033;">,</span>
  <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`codigo`</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">ENGINE</span><span style="color: #CC0099;">=</span>MyISAM  <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span><span style="color: #CC0099;">=</span>utf8 <span style="color: #CC0099; font-weight: bold;">COLLATE</span><span style="color: #CC0099;">=</span>utf8_unicode_ci <span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">`produtos`</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`codigo`</span><span style="color: #000033;">,</span> <span style="color: #008000;">`descricao`</span><span style="color: #000033;">,</span> <span style="color: #008000;">`ativo`</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">VALUES</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">001</span><span style="color: #000033;">,</span> <span style="color: #008000;">'Produto1'</span><span style="color: #000033;">,</span> <span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">002</span><span style="color: #000033;">,</span> <span style="color: #008000;">'Produto2'</span><span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">003</span><span style="color: #000033;">,</span> <span style="color: #008000;">'Produto3'</span><span style="color: #000033;">,</span> <span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">004</span><span style="color: #000033;">,</span> <span style="color: #008000;">'Produto4'</span><span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></div></div>

<p><em>Vamos ao código PHP</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #666666; font-style: italic;">// variaveis para acesso ao banco de dados</span>
	<span style="color: #000088;">$servidor</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'lc'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// validando a conexao</span>
	<span style="color: #000088;">$conexao</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$servidor</span><span style="color: #339933;">,</span> <span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> 
				or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Erro ao conectar ao banco de dados&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #009900;">&#41;</span>
				or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Erro ao selecionar banco de dados&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// efetuando um select na tabela Produtos</span>
	<span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT codigo, descricao, ativo FROM produtos&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// exibindo o resultado</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;strong&gt;Produtos&lt;/strong&gt;:&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>	
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// recebendo o valor do campo ativo</span>
		<span style="color: #000088;">$check</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ativo'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// se valor for igual a 1 é selecionado</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$check</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$checked</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;checked&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// senão, fica em branco</span>
			<span style="color: #000088;">$checked</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;strong&gt;Código&lt;/strong&gt;: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'codigo'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; - &quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;strong&gt;Descrição&lt;/strong&gt;: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'descricao'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; - &quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;strong&gt;Ativo&lt;/strong&gt; &lt;input type='checkbox' <span style="color: #006699; font-weight: bold;">$checked</span> /&gt; &quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Resultado</strong>:</p>
<p><img src="/wp-content/uploads/img-checkbox.jpg" alt="Resultado do checkbox" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.linhadecomando.com/php/php-trabalhando-o-resultado-obtido-de-uma-consulta-sql-checkbox/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript: Contando Checkbox Selecionados (3)</title>
		<link>http://www.linhadecomando.com/javascript/javascript-contando-checkbox-selecionados-3</link>
		<comments>http://www.linhadecomando.com/javascript/javascript-contando-checkbox-selecionados-3#comments</comments>
		<pubDate>Fri, 04 Sep 2009 13:23:24 +0000</pubDate>
		<dc:creator>Tiago</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[getElementsByTagName]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[inputs]]></category>
		<category><![CDATA[JavaScript checkbox]]></category>
		<category><![CDATA[resultados]]></category>
		<category><![CDATA[retorno da função]]></category>

		<guid isPermaLink="false">http://www.linhadecomando.com/?p=815</guid>
		<description><![CDATA[Parte final da nossa sequência de códigos. Resultado final: Please upgrade your browser Código completo: &#60;html&#62; &#60;head&#62; &#60;title&#62;Contando Checkbox&#60;/title&#62; &#60;script language=&#34;JavaScript&#34;&#62; &#60;!-- ***** funcao javascript ***** --&#62; function contaCheckbox&#40;selecionados&#41;&#123; var inputs, x, selecionados=0; inputs = document.getElementsByTagName&#40;'input'&#41;; for&#40;x=0;x&#60;inputs.length;x++&#41;&#123; if&#40;inputs&#91;x&#93;.type=='checkbox'&#41;&#123; if&#40;inputs&#91;x&#93;.checked==true &#38;&#38; inputs&#91;x&#93;.id == 'check'&#41;&#123; selecionados++; &#125; &#125; &#125; return selecionados; &#125; function pegaQuantidade&#40;&#41;&#123; var total; total [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-2234028370580589";
/* 468x15, criado 22/07/09 */
google_ad_slot = "1021735866";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Parte final da nossa sequência de códigos.</p>
<p><strong>Resultado final</strong>:<br />
<div class="iframe-wrapper">
  <iframe src="http://www.linhadecomando.com/javascript/conta_checkbox.html" frameborder="0" style="height:120px;width:400px;margin:0">Please upgrade your browser</iframe>
</div>
<p><strong>Código completo</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Contando Checkbox<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;JavaScript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;!--</span> <span style="color: #339933;">*****</span> funcao javascript <span style="color: #339933;">*****</span> <span style="color: #339933;">--&gt;</span>
<span style="color: #003366; font-weight: bold;">function</span> contaCheckbox<span style="color: #009900;">&#40;</span>selecionados<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> inputs<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> selecionados<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  inputs <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>x<span style="color: #339933;">&lt;</span>inputs.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>inputs<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">type</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'checkbox'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>inputs<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">checked</span><span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">true</span> <span style="color: #339933;">&amp;&amp;</span> inputs<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'check'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        selecionados<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> selecionados<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> pegaQuantidade<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> total<span style="color: #339933;">;</span> 
  total <span style="color: #339933;">=</span> contaCheckbox<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>total <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Quantidade retornada: &quot;</span> <span style="color: #339933;">+</span> total<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
  <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Selecione pelo menos um checkbox&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;!--</span> <span style="color: #339933;">*****</span> html <span style="color: #339933;">-</span> formulario <span style="color: #339933;">*****</span> <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 1&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">1</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span> 
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 2&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">2</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span> 
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 3&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">3</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 4&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">4</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 5&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">5</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span> 
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 6&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">6</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span> 
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 7&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">7</span>
  <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;VERIFICAR&quot;</span> onclick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;pegaQuantidade();&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.linhadecomando.com/javascript/javascript-contando-checkbox-selecionados-3/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JavaScript: Contando Checkbox Selecionados (2)</title>
		<link>http://www.linhadecomando.com/javascript/javascript-contando-checkbox-selecionados-parte-2</link>
		<comments>http://www.linhadecomando.com/javascript/javascript-contando-checkbox-selecionados-parte-2#comments</comments>
		<pubDate>Fri, 04 Sep 2009 13:11:14 +0000</pubDate>
		<dc:creator>Tiago</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[getElementsByTagName]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[inputs]]></category>
		<category><![CDATA[retorno da função]]></category>

		<guid isPermaLink="false">http://www.linhadecomando.com/?p=809</guid>
		<description><![CDATA[Como prometido, vamos pegar o retorno da função e mostrar o resultado na tela. - Primeiro, vamos utilizar uma função para obter o retorno, chamado pegaQuantidade. - Segundo, inserir um botão, que ao clicar, vamos exibir na tela a quantidade de checkbox selecionados; o código deve ficar dentro das tags &#60;form&#62; &#60;/form&#62; function pegaQuantidade&#40;&#41;&#123; var [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-2234028370580589";
/* 468x15, criado 22/07/09 */
google_ad_slot = "1021735866";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Como prometido, vamos pegar o retorno da função e mostrar o resultado na tela.</p>
<p>- Primeiro, vamos utilizar uma função para obter o retorno, chamado pegaQuantidade.<br />
- Segundo, inserir um botão, que ao clicar, vamos exibir na tela a quantidade de checkbox selecionados; o código deve ficar dentro das tags &lt;form&gt; &lt;/form&gt;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> pegaQuantidade<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> total<span style="color: #339933;">;</span> 
  total <span style="color: #339933;">=</span> contaCheckbox<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>total <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Quantidade retornada: &quot;</span> <span style="color: #339933;">+</span> total<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
  <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Selecione pelo menos um checkbox&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="html4script" style="font-family:monospace;">&lt;input type=&quot;button&quot; value=&quot;VERIFICAR&quot; onclick=&quot;pegaQuantidade();&quot; /&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.linhadecomando.com/javascript/javascript-contando-checkbox-selecionados-parte-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript: Contando Checkbox Selecionados (1)</title>
		<link>http://www.linhadecomando.com/javascript/javascript-contando-checkbox-selecionados</link>
		<comments>http://www.linhadecomando.com/javascript/javascript-contando-checkbox-selecionados#comments</comments>
		<pubDate>Fri, 04 Sep 2009 12:42:21 +0000</pubDate>
		<dc:creator>Tiago</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[getElementsByTagName]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[inputs]]></category>

		<guid isPermaLink="false">http://www.linhadecomando.com/?p=798</guid>
		<description><![CDATA[Neste post você irá aprender, através de javascript, a contar o checkbox selecionados. Explicando o código: - função contaCheckbox obtém todos os inputs (checkbox) em forma de array e atribui a variável inputs. - dentro do for foram utilizados alguns if para filtrar a nossa busca; lembrando que queremos contar os checkbox selecionados. - o [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-2234028370580589";
/* 468x15, criado 22/07/09 */
google_ad_slot = "1021735866";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Neste post você irá aprender, através de javascript, a contar o checkbox selecionados.</p>
<p><strong>Explicando o código</strong>:</p>
<p>- função <strong>contaCheckbox</strong> obtém todos os inputs (checkbox) em forma de array e atribui a variável inputs.<br />
- dentro do for foram utilizados alguns if para filtrar a nossa busca; lembrando que queremos contar os checkbox selecionados.<br />
- o primeiro if verifica se os inputs são do tipo checkbox, pode ser aplicado aos demais inputs.<br />
- o segundo if verifica se o checkbox está selecionado e <em>id</em> é igual a &#8220;check&#8221;; o id é utilizado aqui para diferenciar de outros checkbox, quero dizer, se você tem vários checkbox, no mesmo formulário, separados por assunto.<br />
- <strong>selecionados++</strong> é o incrementador; quando passar por esta linha acrescentamos ao total de selecionados o valor de 1.</p>
<p><em>Este post está dividido em 3 partes, na segunda parte, acrescentaremos alguns códigos para pegarmos o retorno da função e ao clicar num botão será exibido um alerta com a quantidade de checbox selecionados</em>; a terceira parte, será disponibilizado o código completo e o resultado final para testar online.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Contando Checkbox<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;JavaScript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;!--</span> <span style="color: #339933;">*****</span> funcao javascript <span style="color: #339933;">*****</span> <span style="color: #339933;">--&gt;</span>
<span style="color: #003366; font-weight: bold;">function</span> contaCheckbox<span style="color: #009900;">&#40;</span>selecionados<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> inputs<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> selecionados<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  inputs <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>x<span style="color: #339933;">&lt;</span>inputs.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>inputs<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">type</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'checkbox'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>inputs<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">checked</span><span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">true</span> <span style="color: #339933;">&amp;&amp;</span> inputs<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'check'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        selecionados<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> selecionados<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;!--</span> <span style="color: #339933;">*****</span> html <span style="color: #339933;">-</span> formulario <span style="color: #339933;">*****</span> <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 1&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">1</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span> 
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 2&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">2</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span> 
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 3&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">3</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 4&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">4</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 5&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">5</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span> 
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 6&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">6</span><span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span> 
   <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;valor 7&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;check&quot;</span><span style="color: #339933;">&gt;</span> Checkbox <span style="color: #CC0000;">7</span>
  <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.linhadecomando.com/javascript/javascript-contando-checkbox-selecionados/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

