Botó basculant

Si fas clic al botó de sota canvia de valor

<script>
function myFunction()
{
c= document.proves_form.idEntrada.value;
    if (c==1)
      {
      document.proves_form.idEntrada.value = 0 ;
        }
      else if (c==0)
       {
        document.proves_form.idEntrada.value = 1 ;
      }
}
</script>
<form method="POST" action="" name="proves_form"> 
<input type= "button" id= "idEntrada" value="0" onclick="myFunction()">
</form>

Passar un array de JavaScript a php

array_JS_php.htm

[codesyntax lang=”html4strict”]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Language" content="ca" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>passsar array JS a php</title>

<script type="text/javascript">

scriptAr = new Array();
scriptAr[0] = "one";
scriptAr[1] = "two";
scriptAr[2] = "three";

 function setValue()
{
// ho passa a cadena
var arv = scriptAr.toString();
test.arv.value = arv ;

// ho escriu al formulari de forma oculta per al scrip php
}
</script>
 
</head>

<body>

<form action="phpArrayTest.php" method="post" name="test" onsubmit="setValue()">

<p>
Aquest exemple crea&nbsp; l&#39;array scriptAr[0] = &quot;one&quot;; scriptAr[1] = &quot;two&quot;; scriptAr[2] = &quot;three&quot;. <br />
Quan  

s&#39;envia el formulari demes de l&#39;acció &quot;phpArrayTest.php&quot; també s&#39;executa la 
funció JS &quot;setValue()&quot;. Mirar codi posant el ratolí 
<span style="color: #FF0000">aquí</span> i premen lo 
boto dret  

.<br />
La funció setValue() passa l&#39;array a un camp ocult del formulari de nom 
<span style="font-weight: 700">arv</span> . Es a dir el prepara per a que 
l&#39;acció php ho pugui llegir. </p>




<input name="arv" type="hidden" />
<input type="submit" value="enviar formulari"/>

</form> 

<p>
Codi de l'acció phpArrayTest.php:</p>

</body>

</html>

[/codesyntax]

phpArrayTest.php

[codesyntax lang=”php”]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sin título 1</title>
</head>

<body>

<?php

$ss = $_POST['arv']; 
//echo " valor de ss= ". $ss;
// La funcion explode convertira la cadena a arreglo 
//******************************
$tok = explode(',',$ss); 
//******************************
// per a visualitzar-la la tornem a possar en una lìnia
echo  implode(',',$tok);

?>

</body>

</html>

[/codesyntax]

JavaScript: array php a array JS

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<!--
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>

<meta http-equiv="Content-Language" content="ca" />
<title>passsar array php a JS</title>

<?php $array = array('one', 'two', 'three'); ?>

<script type="text/javascript">
var xx=new Array("<?=implode("\",\"", $array); ?>")
</script>

<script type="text/javascript">
function f0(){
alert(xx[0]);}
</script>

<script type="text/javascript">
function f1(){
alert(xx[1]);}
</script>

<script type="text/javascript">
function f2(){
alert(xx[2]);}
</script>

</head>
<?php $array = array('one', 'two', 'three'); ?>

<body>

Exemple com passar una variable array de php a javascrip<br />

<!--<?php echo $array&#91;1&#93;; ?> -->

<input name="B1" type="button" value="XX&#91;0&#93;" onclick="f0()" />
<input name="B2" type="button" value="XX&#91;1&#93;" onclick="f1()" />
<input name="B3" type="button" value="XX&#91;2&#93;" onclick="f2()" /><br />

</body>

</html>

JavaScript: exemple onchange sobre entrada


<script type="text/javascript">
function checkEmail(email)
{
if(email.length > 0)
{
if (email.indexOf(' ') >= 0)
alert("email addresses cannot have spaces in them");
else if (email.indexOf('@') == -1)
alert("a valid email address must have an @ in it");
}
}
//-->
// ]]></script>

<form action="../cgi-bin/mycgi.pl" method="POST">
name: <input type="text" name="realname" />

email: <input type="text" name="email" onchange="checkEmail(this.value)" />

destination: <input type="text" name="desination" /></form>omplir el camp email deixant un espai o sense posar @ dona error, quan el focus
passa  un altre punt, com destination, per example

&amp;amp;nbsp;

Per mira el codi posar el ratolí <span style="color: #ff0000;">aquí</span>
i prem lo boto dret.