Jump to content

Como usar array de um POST em ajax


Brunod93
 Share

Recommended Posts

Boa tarde!

Estou desenvolvendo um aplicativo em php que faz em registo que envolve um array ao fim de concluir os testes só com php Agora estava a tentar utilizar nesta aplicação ajax mas não estou a conseguir enviar os POST dos array para a pagina de registo.

Index.php

<html>
<head>
<title>User Registration Using PHP Ajax</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){

$.ajax({
type: "POST",
url: "ajax.php",
data: /* Nao sei o que por aqui */ ,
success: function(html)
{
if(html == "true")
		 {
			 $("#add_err").html("Registado com sucesso");
		 }
		 else
		 {
			 $("#add_err").html("Wrong username or password");
		 }

}
});
return false;
});
});
</script>
<style type="text/css">
#load
{
display:none;
width:500px;
height:500px;
background:url(loading3.gif) no-repeat;
}
#line
{
margin:20px 0;
}
</style>
</head>
<body>
<div id="load" style="">
</div>
<div id="box">
</div>
<form method="POST" action="" id="form2">
<div class="err" id="add_err"></div>
<?php
$i = '0';
$x = '4';
while ( $i <= $x)
{
 ?>
 <div id="line">USERNAME: <input type="text" name="username[]" id="username" /></div>
 <div id="line">aaaaaaaa: <input type="text" name="last_Name[]" id="a" /></div>
 <?php
 $i++;
}
?>

<!--div id="line">PASSWORD: <input type="password" name="password" id="password" /></div>
<div id="line">EMAIL:	 <input type="text" name="email" id="email" /></div-->
<input type="hidden" name="asd" id="asd" value="<?php echo $x; ?>" /></div><br/>
<input type="submit" id="submit" name="submit" />
</form>
</body>
</html>

Pagina de registo "Ajax.php"

<?php
$query=mysql_connect("localhost","root","");
mysql_select_db("freeze",$query);


foreach( array_combine($_POST['username'], $_POST['last_Name']) as $a1 => $a2)
{
echo $a1." ".$a2."<br/>";
$query2=mysql_query("insert into user values('','$a1','$a2','asdasdas')") or die(mysql_error());
if(!$query2)
{
 echo "falhou";
}
else
{
 echo "Registado com sucesso";
}
}

?>

Esta pagina aqui ainda não esta totalmente adaptada para ajax

Edited by apocsantos
geshi
Link to comment
Share on other sites

Se a tua dúvida é como tiras os dados do form, a resposta é, usa os selectores do jQuery.

Tu começas por ter um problema a partir do momento em que vais ter um ciclo em PHP que vai criar vários elementos com ids iguais. Como um id deve ser único, logo à partida já vais ter qualquer coisa mal.

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Se a tua dúvida é como tiras os dados do form, a resposta é, usa os selectores do jQuery.

Tu começas por ter um problema a partir do momento em que vais ter um ciclo em PHP que vai criar vários elementos com ids iguais. Como um id deve ser único, logo à partida já vais ter qualquer coisa mal.

mas eu faço um ciclo while para criar os input porque na aplicação que criando o numero de inputs varia consoante o mês em questão se souber de alguma maneira mas fácil de fazer isto me diga pff

Edited by Brunod93
Link to comment
Share on other sites

Ninguém te impede de criares múltiplos inputs num ciclo. Só que tens que lhes atribuir ids diferentes.

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Os selectores do jQuery são os mesmos selectores que usas para o CSS:

$('#xpto'); // selectiona o elemento cujo id="xpto"
$('.xpto'); // selecciona os elementos com classe xpto
$('div.xpto'); // seleciona os elementos div que tenham a classe xpto
Edited by KTachyon

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.