SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validate a form field where the name is a PHP VAR

    hello! I'm making a page to manage a campaign of the Magical Pirilampo.
    Basically the page would go to manage the part of stocks, purchase orders, impression of receipts. The problem appeared when it wanted to validate a form with Javascript where the name of input was an Array in php! However the Javascript for some reason does not recognize the name of the field therefore in php is quantidade[$i ].

    Code:

    <html>
    <head>
    <link href='estilos.css' rel='stylesheet' type='text/css'>
    <link href='listagens.css' rel='stylesheet' type='text/css'>

    <script language="javascript">
    function valida()
    {
    var existente=document.encomendar.elements.value;
    var quantidade=document.encomendar.quantidade.value;
    <? printf("alert('Teste! Valor = ' + existente + '!');"); ?>
    <? printf("alert('Teste! Valor = ' + quantidade + '!');"); ?>
    if (encomendar.existente.value < encomendar.quantidade.value)
    {
    alert("primeiro.");
    encomendar.quantidade.focus();
    return false;
    }
    else
    {
    alert("segundo.");
    }
    }
    </SCRIPT>
    </head>

    <?
    $numero=$_GET['numero'];

    $lig=mysql_connect("localhost","root","") or die("Não consegui ligar ao servidor!");
    $db=mysql_select_db("pirilampix") or die("Não encontrei a Base de Dados");

    $sql_nome_encomenda="SELECT numero_cliente FROM encomenda_cabecalho WHERE numero_encomenda='$numero'";
    $query_nome_encomenda=mysql_query($sql_nome_encomenda);
    $registo_nome_encomenda=mysql_fetch_array($query_nome_encomenda);
    $numero_cliente=$registo_nome_encomenda[0];

    $sql="SELECT nome FROM cliente WHERE numero_cliente=$numero_cliente";
    $query=mysql_query($sql) or die("Não consegui registar");
    $num_reg=mysql_num_rows($query);
    $registo=mysql_fetch_array($query);

    echo "<center><br>Encomenda Pendente<br>";
    ?>
    <form name="encomendar" method="post" action="insere_encomenda_satisfeita.php" onSubmit="return valida();">
    <?
    echo "Cliente: $registo[0] <br><br>";

    $sql_artigo="SELECT * FROM artigo";
    $query_artigo=mysql_query($sql_artigo);
    $num_registo_artigo=mysql_num_rows($query_artigo);

    $i=0;
    echo "<table border=1>";
    echo "<br><table border=1 style='border-collapse:collapse;border-bottom-color:#003366;' bordercolor='#003366'>";
    echo "<tr bgcolor=#003366 style='color:#FFFFFF; font-family:Arial, Helvetica, sans-serif;font-size:14px;font-weight: bold;text-align:center;'><td style='padding-left: 10px;padding-right: 10px;'>Artigo</td><td>Existente<td>Pendente</td><td>Encomendada</td></tr>";
    $sql_encomenda_detalhe="SELECT * FROM encomenda_detalhe WHERE numero_encomenda='$numero'";
    $query_encomenda_detalhe=mysql_query($sql_encomenda_detalhe);
    $sql_teste="SELECT designacao FROM artigo where numero_artigo = $numero";
    $query_teste=mysql_query($sql_teste);
    $reg_teste_n=mysql_num_rows($query_encomenda_detalhe);
    $i=0;
    while ($i < $reg_teste_n){
    $reg = mysql_fetch_row($query_encomenda_detalhe);
    $sql_teste="SELECT * FROM artigo where numero_artigo = $reg[1]";
    $query_teste=mysql_query($sql_teste);
    $reg_teste=mysql_fetch_row($query_teste);
    echo "<tr style='font-family:Arial, Helvetica, sans-serif; font-size:12px;text-align:center;'>
    <td>$reg_teste[1]</td>
    <td><input name='existente' value='$reg_teste[2]' size=5></td>
    <td><input name='pendente' value='$reg[2]' size=5></td>
    <td><input name='quantidade[$i]' value='0' type='text' size=5 maxlength=5>
    <input name='prod[$i]' value='$reg_teste[0]' type='hidden'>
    <input name='encomendada[$i]' value='$reg[2]' type='hidden'>";
    $i++;
    echo "<input name='num' value='$i' type='hidden'>";

    }

    echo "</table>";

    mysql_free_result($query);
    mysql_free_result($query_encomenda_detalhe);
    mysql_close($lig);

    echo "<input name='cliente' value='$numero_cliente' type='hidden'>";
    echo "<input name='encomenda' value='$numero' type='hidden'>";

    echo "<br><span class='submit'><input type='submit' name='Submit' value='Encomendar'></span>
    <span class='submit'><input type='reset' name='Apagar' value='Apagar'></span>";

    echo "</form>";
    echo "</html>";
    ?>

    it was thankful if someone could clarify me or to help...

    My mail: renato.miguel@clix.pt or msn: dicromio_@hotmail.com

    Thanks!!!

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi renatofmiguel, Welcome to SPF!

    What does it look like on the client-side? Can you give us a link to the page?

  3. #3
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello! thank u for your so quick response! the link of my page is http://www.cecdmirasintra.org/pirila...r.php?numero=6
    i'm sorry but the language is Portuguese but a translate

    Artigo = Article
    Existente = Existing ( the amount exist in stock )
    Pendente = Pending or Hanging (Pending or Hanging amount that was ordered)
    Encomendada = Orderer ( the amount that u gonna order but can't be greater than existing or hanging )

    and i want the page alert the user!

    thank so far for our concern

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this

    Code:
    <script type='text/javascript'>
    window.onload = function()
    {
      document.forms['encomendar'].onsubmit = valida;
    };
    function valida()
    {
      var i, e, p, qo, q, err = '', el = this.elements;
      var n = el['existente'].nodeType ? 1 : el['existente'].length;
    
      for (i = 0; i < n; ++i) {
        e = el['existente'];
        if (n > 1) e = e[i];
        e = parseInt(e.value) || 0;
        p = el['pendente'];
        if (n > 1) p = p[i];
        p = parseInt(p.value) || 0;
        qo = el['quantidade[' + i + ']'];
        q = parseInt(qo.value) || 0;
    
    //    alert('i:' + i + ', e:' + e + ', p:' + p + ', q:' + q);
    
        if (q > e) {
          err = 'q > e';
        }
        else if (q > p) {
          err = 'q > p';
        }
        if (err) {
          qo.focus();
          qo.select();
          alert(err);
          return false;
        }
      } // end for
      return true;
    }
    </script>

  5. #5
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a much better implementation. I've also included the entire file I used for testing.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Pirilampix</title>
    
    <script type='text/javascript'>
    window.onload = function()
    {
      document.forms['encomendar1'].onsubmit = validate;
      document.forms['encomendar2'].onsubmit = validate;
    };
    function validate()
    {
      var i, el = this.elements;
      // if existente is a node
      if (el['existente'].nodeType) {
        return valid(el['existente'], el['pendente'], el['quantidade[0]']);
      }
      // if existente is a node-list (array)
      for (i = 0; i < el['existente'].length; ++i) {
        if (!valid(el['existente'][i], el['pendente'][i], el['quantidade[' + i + ']'])) {
          return false;
        }
      }
      return true;
    }
    function valid(e, p, q)
    {
      var err = '', qo = q;
      e = parseInt(e.value) || 0;
      p = parseInt(p.value) || 0;
      q = parseInt(q.value) || 0;
      //alert('e:' + e + ', p:' + p + ', q:' + q);////
      if (q > e) {
        err = 'q > e';
      }
      else if (q > p) {
        err = 'q > p';
      }
      if (err) {
        qo.focus();
        qo.select();
        alert(err);
        return false;
      }
      return true;
    }
    </script>
    
    </head>
    
    <body>
    <form name='encomendar1' method='post' action=''>
    <p>
    <input name='existente' value='520' size=5>
    <input name='pendente' value='1000' size=5>
    <input name='quantidade[0]' value='0' type='text' size=5 maxlength=5>
    <input name='prod[0]' value='1' type='hidden'>
    <input name='encomendada[0]' value='1000' type='hidden'>
    <input name='num' value='1' type='hidden'>
    </p>
    <p>
    <input name='existente' value='400' size=5>
    <input name='pendente' value='600' size=5>
    <input name='quantidade[1]' value='1' type='text' size=5 maxlength=5>
    <input name='prod[1]' value='1' type='hidden'>
    <input name='encomendada[1]' value='1000' type='hidden'>
    <input name='num' value='2' type='hidden'>
    </p>
    <p>
    <input name='existente' value='400' size=5>
    <input name='pendente' value='600' size=5>
    <input name='quantidade[2]' value='2' type='text' size=5 maxlength=5>
    <input name='prod[2]' value='1' type='hidden'>
    <input name='encomendada[2]' value='1000' type='hidden'>
    <input name='num' value='2' type='hidden'>
    </p>
    <p>
    <input name='cliente' value='1' type='hidden'>
    <input name='encomenda' value='6' type='hidden'>
    <input type='submit' name='Submit' value='Encomendar'>
    <input type='reset' name='Apagar' value='Apagar'>
    </p>
    </form>
    
    <form name='encomendar2' method='post' action=''>
    <p>
    <input name='existente' value='1000' size=5>
    <input name='pendente' value='500' size=5>
    <input name='quantidade[0]' value='0' type='text' size=5 maxlength=5>
    <input name='prod[0]' value='1' type='hidden'>
    <input name='encomendada[0]' value='1000' type='hidden'>
    <input name='num' value='1' type='hidden'>
    </p>
    <p>
    <input name='cliente' value='1' type='hidden'>
    <input name='encomenda' value='6' type='hidden'>
    <input type='submit' name='Submit' value='Encomendar'>
    <input type='reset' name='Apagar' value='Apagar'>
    </p>
    </form>
    
    </body>
    </html>

  6. #6
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    helllo mike!!!! i'm very very very thankful!!! it works perfectly!!!

    Let me say that the C.E.C.D. it is a Cooperative for social solidarity that develops services of quality for the people in disadvantage, giving with priority reply to the people with intellectual deficiency promoting is inclusan in the society, dignifying its image, fighting for its rights and improving its quality of life. We "born" 30 years ago. The site have the objective to manage a campaign to collect funds to our organization. So i'm very thankful for your help!

    Bye

  7. #7
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are very welcome

    In case it might help anyone who needs a little more explanation about what I did, refer to this demo.

  8. #8
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for explanation! now i understand



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •