validating the form with javascript and printing with PHP

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
skylines
Forum Newbie
Posts: 8
Joined: Tue Jul 28, 2009 2:45 am

validating the form with javascript and printing with PHP

Post 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>
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: validating the form with javascript and printing with PHP

Post 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
skylines
Forum Newbie
Posts: 8
Joined: Tue Jul 28, 2009 2:45 am

Re: validating the form with javascript and printing with PHP

Post by skylines »

hey, thanks for your reply.it worked for me. Now i realised how silly the mistake is.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: validating the form with javascript and printing with PHP

Post by andyhoneycutt »

Awesome! Sometimes it just takes another pair of eyes :)
Post Reply