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 l'array scriptAr[0] = "one"; scriptAr[1] = "two"; scriptAr[2] = "three". <br />
Quan
s'envia el formulari demes de l'acció "phpArrayTest.php" també s'executa la
funció JS "setValue()". Mirar codi posant el ratolí
<span style="color: #FF0000">aquí</span> i premen lo
boto dret
.<br />
La funció setValue() passa l'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'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]