Need help with a form check function.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Metalmurphy
Forum Newbie
Posts: 2
Joined: Mon Feb 11, 2008 4:25 pm

Need help with a form check function.

Post by Metalmurphy »

Can anyone tell me why the checkform() function isn't working

Code: Select all

<!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=iso-8859-1" />
<script>
    function checkform() {
    if(document.testinho.qtd[<?=$reg_lista['ID_produto']?>].value<"0") { 
        alert("Valor inválido.") 
        return false; 
    }
    alert("Testing.");
    return true;
}
</script>
<title>Amazonia</title>
<link type="text/css" rel="stylesheet" href="estilos.css"/>
<style type="text/css">
<!--
.style1 {   font-size: 12px;
    font-weight: bold;
}
-->
</style>
</head>
<body>
<div class="wrapper">
  <div class="banner"><a href="#"><img src="images/logo_10.jpg" alt="AMAZONIA" width="351" height="108" border="0" /></a></div>
  <div class="content">
    <div class="leftcolumn">
    <ul>
    <?php
      if (!(isset($_SESSION['MM_Username']))) {
      ?>
        <li><a href="login.php">Login</a></li>
        <li><a href="registo.php">Registo</a></li>
        <li><a href="esqueci.php">Esqueci-me</a></li>
      <?php } else { ?>
        <li><a href="<?php echo $logoutAction ?>">Logout</a></li>
        <li><a href="alterar.php">Alterar Dados</a></li>
        <li><a href="historico.php">Histórico</a></li>
      <?php
      }   
      ?>
      </ul>
    <div id="navvy">
 
<ul id="navvylist">
<li><a href="index.php">Novidades</a></li>
<li><a href="livros.php">Livros</a></li>
<li><a href="filmes.php">Filmes</a></li>
<li><a href="musica.php">M&uacute;sica</a></li>
<li><a href="pesquisa.php">Pesquisar</a></li>
</ul>
</div>
    </div>
    <div class="rightcolumn">
      <h2>Carrinho de compras</h2>
 <div class="article">
<form name="testinho" action="cart.php?action=altera_qtd" method="post" onsubmit='[b]return checkform()[/b]'>
<p>&nbsp;</p>
<table border="1" width"100%">
    <tr>
        <td width="120"><div align="center"><strong>PRODUTO</strong></div></td>
        <td width="100"><div align="center"><strong>REMOVER</strong></div></td>
        <td width="80"><div align="center"><strong>PREÇO</strong></div></td>
        <td width="80"><div align="center"><strong>QTD6</strong></div></td>
        <td width="100"><div align="center"><strong>SUBTOTAL</strong></div></td>
    </tr>
     <?
     $sql_lista = "SELECT * FROM Carrinho WHERE sessao ='".session_id()."' ORDER BY titulo ASC";
     $exe_lista = mysql_query($sql_lista) or die(mysql_error());
     $num_lista = mysql_num_rows($exe_lista);
     
     if ($num_lista > 0) {
     $total_carrinho = 0;
     while ($reg_lista = mysql_fetch_array($exe_lista, MYSQL_ASSOC)){
        $total_carrinho += ($reg_lista['preco']*$reg_lista['qtd']);
     ?>
            <tr>
         <td><?=$reg_lista['titulo']?>
           <div align="center"></div></td>
        <td><div align="center"><a href="cart.php?action=del&ID_produto=<?=$reg_lista['ID_produto']?>">X</a></div></td>
        <td><?=$reg_lista['preco']?>
          <div align="center"></div></td>
        <td><div align="center">
          <input type="text" size="3" name="qtd[<?=$reg_lista['ID_produto']?>]" value="<?=$reg_lista['qtd']?>"/>
        </div></td>
        <td><?=number_format(($reg_lista['preco']*$reg_lista['qtd']),2, ",",".")?>
          <div align="center"></div></td>
    </tr>
   <?
   }
   }
   ?>
   <tr>
     <td colspan="4"><div align="right">Total</div></td>
     <td><?=number_format($total_carrinho,2, ",",".")?>
       <div align="center"></div></td>
     </tr>
</table>
<p align="right">&nbsp;</p>
<p align="right"><a href="index.php">Adicionar mais compras ao carrinho</a></p>
<p align="right">
    <input type="submit" class="button" value="Alterar quantidade"/>
    
</p>
</form>
<form action="compra.php" method="post">
    <label>
    <div align="right">
      <input name="confirmar" type="submit" class="button" id="confirmar" value="Comprar" />
    </div>
    </label></form>
</div>
    </div>
    <div class="clear"></div>
  </div>
  <div class="footer">Amaz&oacute;nia SI</div>
</div>
</body>
</html>
Since I'm not even geting the "Testing" alert box it means the function isn't even being called, no idea why though.

I'm sorry if I'm doing something really stupid, it's been a while since I last did anything in PHP.

*edit* This probably should have been in the "Miscellaneous" since it might be more Javascript related then PHP. Sorry about that.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Need help with a form check function.

Post by John Cartwright »

What does the source look like after the script is run? Short tags may be disabled.
Metalmurphy
Forum Newbie
Posts: 2
Joined: Mon Feb 11, 2008 4:25 pm

Re: Need help with a form check function.

Post by Metalmurphy »

Hum you're right, the <? tags isn't working inside the <script> ones. I get this on the source

Code: Select all

...
if(document.testinho.qtd[].value<"0") { 
...
I should be getting something like

Code: Select all

...
    if(document.testinho.qtd[105].value<"0") { 
...
But they're working everywhere else. Any idea how to get them to work inside the java script section?
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Need help with a form check function.

Post by liljester »

in the <script> part of your code:

Code: Select all

   if(document.testinho.qtd[<?=$reg_lista['ID_produto']?>].value<"0") {
$reg_lista['ID_produto'] is not yet defined ("document.testinho.qtd[].value") wich causes javascript to throw an error because the object doesnt exist in the document, and the script halts.

you should populate $reg_lista['ID_produto'] before your javascript is processed.
Post Reply