I can't understand why the code doesn't seems to function very well

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SAN CAZIANO

    I can't understand why the code doesn't seems to function very well

    can you help me please because I can't understand why the code doesn't seems
    to function very well:

    in onkeypress it must verify if insert number or string value in the fiels,
    but it doesn't seems to function very well
    (in frontpage it works but here it doesn't)

    PROGRAMMA IN FRONTPAGE

    <form method="get" name="f">
    numerico<input type="text" name="t1" onkeypress="ret urn
    numeralsOnly(ev ent)"/><br/>

    stringa <input type="text" name="t2" onkeypress="ret urn
    lettersOnly(eve nt)"/><br/>



    PROGRAMMA IN PHP


    <!doctype html public "-//W3C//DTD HTML 4.0 //EN">
    <html>
    <head>
    <title>Title here!</title>
    </head>
    <body>
    <script type="text/javascript">

    function numeralsOnly(ev t)
    {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ?
    evt.keyCode : ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    {
    alert("Puoi inserire solo numeri!");
    return false;
    }
    return true;
    }

    function lettersOnly(evt )
    {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ?
    evt.keyCode : ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 65 || charCode > 90) && (charCode < 97 ||
    charCode > 122))
    {
    alert("Puoi inserire solo lettere!");
    return false;
    }
    return true;
    }
    </script>

    <?php
    //--------------------------------------------------------------------------
    ----
    // INSERISCE UN CAMPO DI EDIT (CAMBIA COLORE AL SUO SFONDO QUANDO HA IL
    FOCUS)
    // InsertEditField ("digita il tuo Nome","Nome","p rova","60",10);
    //--------------------------------------------------------------------------
    ----
    function InsertEditField ($DataType,$Fie ldName)
    {
    if ($DataType='int eger')
    echo "<input type='TEXT' name='$FieldNam e' onkeypress='ret urn
    numeralsOnly(ev ent)'>";
    else
    if ($DataType='str ing')
    echo "<input type='TEXT' name='$FieldNam e' onkeypress='ret urn
    lettersOnly(eve nt)'>";
    }

    function Prova($Host,$Us er,$Password,$D BName,$TableNam e)
    {
    $Query="select * from $TableName";

    //SI CONNETTE AD UNA TABELLA IN MY SQL
    $Connessione=my sql_connect($Ho st,$User,$Passw ord);

    //SELEZIONA UN DATABASE IN MY SQL
    mysql_select_db ($DBName,$Conne ssione);

    //CREA UNA QUERY SU UNA TABELLA DI UN DATABASE IN MY SQL
    $result=mysql_q uery($Query);

    //ottiene l'elenco dei campi
    $campi = mysql_list_fiel ds($DBName,$Tab leName,$Conness ione);

    //ottiene il totale dei campi
    $colonne = mysql_num_field s($campi);

    //creo la form
    echo "<form>";

    for ($i=0; $i<$colonne; $i++)
    {
    $TipoCampo=mysq l_field_type($c ampi, $i);

    if ($TipoCampo='in t')
    InsertEditField ("integer","Cam po $i");
    else
    if ($TipoCampo='st ring')
    InsertEditField ("string","Camp o $i");
    }

    //chiudo il tag della form
    echo "<input type='submit' value='Invia'>" ;
    echo "<input type='reset' value='Annulla' >";
    echo "</form>";
    }

    Prova('localhos t','utente','pa ssword','db','t abella');
    ?>
    </body>
    </html>



  • steve

    #2
    Re: I can't understand why the code doesn't seems to function ve

    "SAN CAZIANO1" wrote:[color=blue]
    > can you help me please because I can't understand why the code
    > doesn't seems
    > to function very well:
    >
    > in onkeypress it must verify if insert number or string value
    > in the fiels,
    > but it doesn't seems to function very well
    > (in frontpage it works but here it doesn't)
    >
    > PROGRAMMA IN FRONTPAGE
    >
    > <form method="get" name="f">
    > numerico<input type="text" name="t1" onkeypress="ret urn
    > numeralsOnly(ev ent)"/><br/>
    >
    > stringa <input type="text" name="t2" onkeypress="ret urn
    > lettersOnly(eve nt)"/><br/>
    >
    >
    >
    > PROGRAMMA IN PHP
    >
    >
    > <!doctype html public "-//W3C//DTD HTML 4.0 //EN">
    > <html>
    > <head>
    > &lt;title&gt;Ti tle here!&lt;/title&gt;
    > </head>
    > <body>
    > <script type="text/javascript">
    >
    > function numeralsOnly(ev t)
    > {
    > evt = (evt) ? evt : event;
    > var charCode = (evt.charCode) ? evt.charCode :
    > ((evt.keyCode) ?
    > evt.keyCode : ((evt.which) ? evt.which : 0));
    > if (charCode &gt; 31 && (charCode &lt; 48 || charCode &gt;
    > 57))
    > {
    > alert("Puoi inserire solo numeri!");
    > return false;
    > }
    > return true;
    > }
    >
    > function lettersOnly(evt )
    > {
    > evt = (evt) ? evt : event;
    > var charCode = (evt.charCode) ? evt.charCode :
    > ((evt.keyCode) ?
    > evt.keyCode : ((evt.which) ? evt.which : 0));
    > if (charCode &gt; 31 && (charCode &lt; 65 || charCode &gt;
    > 90) && (charCode &lt; 97 ||
    > charCode > 122))
    > {
    > alert("Puoi inserire solo lettere!");
    > return false;
    > }
    > return true;
    > }
    > </script>
    >
    > <?php
    > //------------------------------------------------------------
    > --------------
    > ----
    > // INSERISCE UN CAMPO DI EDIT (CAMBIA COLORE AL SUO
    > SFONDO QUANDO HA IL
    > FOCUS)
    > // InsertEditField ("digita il tuo
    > Nome","Nome","p rova","60",10);
    > //------------------------------------------------------------
    > --------------
    > ----
    > function InsertEditField ($DataType,$Fie ldName)
    > {
    > if ($DataType='int eger')
    > echo "&lt;input type='TEXT' name='$FieldNam e'
    > onkeypress='ret urn
    > numeralsOnly(ev ent)'>";
    > else
    > if ($DataType='str ing')
    > echo "&lt;input type='TEXT' name='$FieldNam e'
    > onkeypress='ret urn
    > lettersOnly(eve nt)'>";
    > }
    >
    > function Prova($Host,$Us er,$Password,$D BName,$TableNam e)
    > {
    > $Query="select * from $TableName";
    >
    > //SI CONNETTE AD UNA TABELLA IN MY SQL
    > $Connessione=my sql_connect($Ho st,$User,$Passw ord);
    >
    > //SELEZIONA UN DATABASE IN MY SQL
    > mysql_select_db ($DBName,$Conne ssione);
    >
    > //CREA UNA QUERY SU UNA TABELLA DI UN DATABASE IN MY SQL
    > $result=mysql_q uery($Query);
    >
    > //ottiene l'elenco dei campi
    > $campi = mysql_list_fiel ds($DBName,$Tab leName,$Conness ione);
    >
    > //ottiene il totale dei campi
    > $colonne = mysql_num_field s($campi);
    >
    > //creo la form
    > echo "&lt;form&g t;";
    >
    > for ($i=0; $i&lt;$colonne ; $i++)
    > {
    > $TipoCampo=mysq l_field_type($c ampi, $i);
    >
    > if ($TipoCampo='in t')
    > InsertEditField ("integer","Cam po $i");
    > else
    > if ($TipoCampo='st ring')
    > InsertEditField ("string","Camp o $i");
    > }
    >
    > //chiudo il tag della form
    > echo "&lt;input type='submit' value='Invia'&g t;";
    > echo "&lt;input type='reset' value='Annulla' &gt;";
    > echo "&lt;/form&gt;";
    > }
    >
    > Prova('localhos t','utente','pa ssword','db','t abella');
    > ?>
    > </body>
    > </html>[/color]

    Suggest you put it through a PHP debugger. There are many free and
    paid debuggers. I used zend (paid, but very nice).

    --
    http://www.dbForumz.com/ This article was posted by author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbForumz.com/PHP-understa...ict157032.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=526229

    Comment

    • Andy Barfield

      #3
      Re: I can't understand why the code doesn't seems to function verywell

      SAN CAZIANO wrote:[color=blue]
      > can you help me please because I can't understand why the code doesn't seems
      > to function very well:
      >
      > in onkeypress it must verify if insert number or string value in the fiels,
      > but it doesn't seems to function very well
      > (in frontpage it works but here it doesn't)
      >
      > PROGRAMMA IN FRONTPAGE
      >
      > <form method="get" name="f">
      > numerico<input type="text" name="t1" onkeypress="ret urn
      > numeralsOnly(ev ent)"/><br/>
      >
      > stringa <input type="text" name="t2" onkeypress="ret urn
      > lettersOnly(eve nt)"/><br/>[/color]

      You seem to be confusing Javascript and PHP -
      Javascript is *client*-side while PHP is *server*-side.

      Assuming that PROGRAMMA IN FRONTPAGE and PROGRAMMA IN PHP are seperate
      files, your PHP program will not be aware of any data until the form is
      submitted to the server, therefore your validation needs to be done in
      the browser.

      Regards,

      Andy

      Comment

      Working...