Stuck with PHP and HTML forms

Started by
2 comments, last by Carolina 18 years, 9 months ago
Hi there! I´m developing a dynamic website using PHP,HTML and MySQL,and I´m stuck on a problem. I have a HTML table and its contents are taken from a MySQL table at my database. The PHP commands to insert stuff into the MySQL table and get stuff from it are working fine.The problem is,I put 2 buttons(for each row) at the rightmost column of my HTML table and I want them to do the following: 1.The button which has the value "<-" must send all data at the MySQL table corresponding to the row that the button is on in the HTML table to another dynamic page. 2.The button which has the value "X" must send a command to the MySQL table so that all data at the MySQL table corresponding to the row that the button is on in the HTML table gets deleted. Huh....well,I know this is confusing.Anyway,the most basic problem is that I don´t know how to make my HTML forms work getting the table data.And I need 2 forms,since I have 2 buttons.But I read on a site that when you use a form inside a table,it works only for a single column... Here is part of my code: /*            <div style=" width:100%; height:15%; overflow:auto;"> <table border="1" align="center"> <tr> <td align="center"> Nome da Atividade </b </td> <td align="center"> Nome do Remetente </td> <td align="center"> Cargo do Remetente </td> </tr> <?php $dados="SELECT Nome_Atividade,Nome_Remetente,Cargo FROM PEDIDOS_SERVICOS"; mysql_connect("localhost","projeto adf"); mysql_select_db("projeto adf"); $resultado=mysql_query($dados); for($i=0;$i<mysql_num_rows($resultado);$i++) { ?> <form name="Categorizaç&atildeo_Ativ" method="get" action="ADF_select_cat.html"> <form name="Deletar_Ativ" method="get" action="ADF_"> <tr> <?php $linha=mysql_fetch_array($resultado_dados); for($j=0;$j<3;$j++) { ?> <td align="center"> <?php echo $linha[$j]; } ?> </td> <td align="center"> <input name="pedidos_submit" type="submit" value="<-"> </form> <input name="pedidos_excluir" type="submit" value="X"> </form> </td> </tr> <?php } ?> </table> */
Advertisement
For future reference: Please use the [ source ] [/ source ] brackets.. especially for code containing HTML markup..

On to the question: Wrap both your buttons in individual forms. Right now you have both buttons inside the Deletar_Ativ form, which is embedded in the Categorizac&atiledo_Ativ forms.

For my solution, I'm going to assume that one of the fields in the resultset returned from your mysql_query call is going to be an ID for that record in the table. I'll assume it's the first field.

<div style=" width:100%; height:15%; overflow:auto;"><table border="1" align="center"><tr><td align="center">Nome da Atividade</b</td><td align="center">Nome do Remetente</td><td align="center">Cargo do Remetente</td></tr><?php$dados="SELECT Nome_Atividade,Nome_Remetente,Cargo FROM PEDIDOS_SERVICOS";mysql_connect("localhost","projeto adf");mysql_select_db("projeto adf");$resultado=mysql_query($dados);for($i=0;$i<mysql_num_rows($resultado);$i++){  ?>  <tr>    <?php    $linha=mysql_fetch_array($resultado_dados);    for($j=0;$j<3;$j++)    {      ?>      <td align="center">      <?php      echo $linha[$j];    }    ?>    </td>    <td align="center">      <form name="Categorizaç&atildeo_Ativ" method="get" action="ADF_select_cat.html">        <input name="recordid" type="hidden" value=" <?php $linha[0] ?>" />        <input name="field2" type="hidden" value=" <?php $linha[1] ?>" />        <input name="field3" type="hidden" value=" <?php $linha[2] ?>" />        <input name="pedidos_submit" type="submit" value="<-" />      </form>      <form name="Deletar_Ativ" method="get" action="ADF_">        <input name="recordid" type="hidden" value=" <?php $linha[0] ?>" />        <input name="pedidos_excluir" type="submit" value="X">      </form>    </td>  </tr>  <?php}?></table>


Ok, so I've wrapped each of your buttons in it's own form. I've added hidden inputs called "recordid" to each form (which won't show up in the rendered HTML). When the form is submitted via button, there will be a "recordid" parameter submitted along with it, which you can then use to delete/update the record in the database

Also, the "<-" button will submit the values from the other two fields in your recordset.. I wasn't sure of the field names, so I've used "field2" and "field3" in my example.. modify to your own needs.
I tried what you said,but it still doesn´t work...I´ll put here my code so you´ll see what I did:


[</td>]
[<td>]
[<form name="Categorizaç&atildeo_Ativ" method="get" action="ADF_select_cat.html">]
[<input name="pedidos_ativ" type="hidden" value=" <?php $linha[0] ?>">]
[<input name="pedidos_remetente" type="hidden" value=" <?php $linha[1] ?>">]
[<input name="pedidos_cargo" type="hidden" value=" <?php $linha[2] ?>">]
[<input name="pedidos_submit" type="submit" value="<-">]
[</form>]

[<form name="Deletar_Ativ" method="get" action="ADF_pedidos.php">]
[<input name="pedidos_ativ" type="hidden" value=" <?php $linha[0] ?>">]
[<input name="pedidos_excluir" type="submit" value="X">]
[</form>]
[</td>]
[</tr>]

[<?php]
[}]
[?>]
[]
[</table>]
[]
[<?php]

[$linha_pedida=$_GET["pedidos_ativ"];]
[$pedido="DELETE FROM PEDIDOS_SERVICOS WHERE Nome_Atividade=$linha_pedida";]
[mysql_connect("localhost","projeto adf");]
[mysql_select_db("projeto adf");]
[$resultado_pedido=mysql_query($pedido);]
[?>]
somebody?;_;

This topic is closed to new replies.

Advertisement