Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

on my login form, i am putting server side validations and if error occurs i want to display those error just below the validated control. Now for this, i am trying to call javascript function to show validation message in php code but not able to call.

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['txtUsername']=='')
{
    //here i want to call javascript function to display message    
}
}
?>
 <form action="login.php" method="POST">

 Username <input type="text" size="30" name="txtUsername" id="user" /><br />

 Password <input type="password" size="30" name="txtPassword" id="pass" /><br />

 <input type="submit" value="Login" name="loginSubmit"/>

</form>


<script type="text/javascript">

   function showMessage(value)
   {
    document.getElementById(value).innerHTML= value+"can not be empty."; 
   }
   </script>

Please tell me how to display server side validation just below the validated control in form.

share|improve this question
Why don't you validate the inputs from form and then submit the form? – Sudip Pal Feb 20 at 10:11
Would it not be useful to actually assign a jQeury script to the submit? The script should then be able to do the validation for you unless I am missing something – Bas Jansen Feb 20 at 10:11

3 Answers

use this

if($_POST['txtUsername']=='')
{
     echo '<script> showMessage("txtUsername"); </script>';
}
share|improve this answer
its not working..... – Kalpana Dixit Feb 20 at 11:18
<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
  if($_POST['txtUsername']=='')
  {
  ?>
      <script>
         //Define the function somewhere in the top or in external js and include it.
        callyourfunction();
      </script>
  <?php
  }
}

?>

share|improve this answer

Something like this

<html>
<head>
<script type="text/javascript">

   function showMessage(value)
   {
       document.getElementById(value).innerHTML= value+"can not be empty."; 
   }
   </script>
</head>
<body>
<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if($_POST['txtUsername']=='')
    {
        echo '<script> showMessage("txtUsername"); </script>';
    }
}
?>
 <form action="login.php" method="POST">

 Username <input type="text" size="30" name="txtUsername" id="txtUsername" /><br />

 Password <input type="password" size="30" name="txtPassword" id="txtPassword" /><br />

 <input type="submit" value="Login" name="loginSubmit"/>

</form>

</body>
</html>
share|improve this answer
:: but i dont want to show message in alert box. i want to show as innerHTML – Kalpana Dixit Feb 20 at 10:11
check updated answer, also note that i have changed the IDs of the textboxes – Aamir Mahmood Feb 20 at 10:11
1  
showMessage must be contained in javascript tag – Laxus Feb 20 at 10:13
just edited that. thank you – Aamir Mahmood Feb 20 at 10:14
@Aamit sorry but it showing like "showMessage'txtUsername'". – Kalpana Dixit Feb 20 at 10:15
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.