Brunod93 Posted April 16, 2015 at 02:31 PM Report Share #581379 Posted April 16, 2015 at 02:31 PM (edited) 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 April 16, 2015 at 09:37 PM by apocsantos geshi Link to comment Share on other sites More sharing options...
KTachyon Posted April 16, 2015 at 03:06 PM Report Share #581383 Posted April 16, 2015 at 03:06 PM 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 More sharing options...
Brunod93 Posted April 16, 2015 at 03:20 PM Author Report Share #581385 Posted April 16, 2015 at 03:20 PM (edited) 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 April 16, 2015 at 03:23 PM by Brunod93 Link to comment Share on other sites More sharing options...
KTachyon Posted April 16, 2015 at 03:37 PM Report Share #581386 Posted April 16, 2015 at 03:37 PM 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 More sharing options...
Brunod93 Posted April 16, 2015 at 04:34 PM Author Report Share #581389 Posted April 16, 2015 at 04:34 PM Ninguém te impede de criares múltiplos inputs num ciclo. Só que tens que lhes atribuir ids diferentes. Será que me podias explicar um pouco sobre como se usa os selectores do jQuery. pff e obrigado Link to comment Share on other sites More sharing options...
KTachyon Posted April 16, 2015 at 05:20 PM Report Share #581391 Posted April 16, 2015 at 05:20 PM (edited) 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 April 16, 2015 at 05:21 PM 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 More sharing options...
bioshock Posted April 16, 2015 at 05:56 PM Report Share #581394 Posted April 16, 2015 at 05:56 PM (edited) data: /* Nao sei o que por aqui */ , Aí deves colocar o identificador e o valor. data: {user: $('#username').val(), pass: $('#password').val()}, Edited April 16, 2015 at 05:57 PM by bioshock Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now