Page 1 of 1

validating the form with javascript and printing with PHP

Posted: Tue Jul 28, 2009 3:45 am
by skylines
Hi,
i am new to programming. Now i want to take userid from user, validate it through javascript and print the username to browser .For that i wrote the following code. Here i am able to take input from user and validate it but can't print the userid to browser.
I googled it but not able to find a solution.can you folks please help?
I tried by adding add PHP_SELF to action attribute of form tag but that does not work.
Below is the my code.

Code: Select all

<html>
 <head>
   <title> List of table ITEMMST </title>
        <style type="text/css">
        body {background-color:lightcyan}
        </style>
        <script language="javascript">
        <!--
        function  checkform()
        {
          if(document.myform.itemid.value =='')
        {
          alert ("pls enter itemid");
          return false;
        }
         else
           return true;
        }
        //-->
       </script>
</head>
   <body>
     <form method="post"  name="myform" onsubmit="return checkform()">
        Item Id:
          <input type="text" name="itemid">
          <br />
       <input type="submit" value="submit" />
          <br />
      </form>
      <?php
      if (isset($_POST["submit"]))
      {
              $itemid=$_POST["itemid"];
          echo "ItemId is: $itemid <br />";
          
     }
     ?>
     </body>
     </html>

Re: validating the form with javascript and printing with PHP

Posted: Tue Jul 28, 2009 11:44 am
by andyhoneycutt
You are missing a name for your submit:

Code: Select all

<!--incomplete: -->
       <input type="submit" value="submit" />
<!-- complete: -->
       <input type="submit" value="submit" name="submit"/>
Which causes this to fail:

Code: Select all

if (isset($_POST["submit"]))
Because $_POST['submit'] doesn't exist.

-Andy

Re: validating the form with javascript and printing with PHP

Posted: Tue Jul 28, 2009 1:31 pm
by skylines
hey, thanks for your reply.it worked for me. Now i realised how silly the mistake is.

Re: validating the form with javascript and printing with PHP

Posted: Tue Jul 28, 2009 1:32 pm
by andyhoneycutt
Awesome! Sometimes it just takes another pair of eyes :)