Khalid
Khalid

Reputation: 467

how can i call java script in php which is in same html page?

How can I call java script in php or can convert same php code in js. Please explain I am new to javascript

<?php
  if(isset($_POST['Submit']))
{
     $user_name = $_POST['login_name'];                                            
     if(empty($_SESSION['captcha_code'] ) || strcasecmp($_SESSION['captcha_code'], $_POST['captcha_code']) != 0)
     {
       $msg="<span style='color:red'>The Validation code does not match!</span>";
}else{
       echo "<script>FormSubmit();</script>";  //how to call here
 }   
}
?>

<html>
<body>
    <form name="ddd" id="ddd">
      <label>name:</name></label>
             <input id="login_name" type="text" />
      <button  type="submit" onclick="return validate(); value="Submit">
    </form> 

  <script>
    function FormSubmit(){
     ................
     ................

   }
   </script>
</body>
</html>

Thanks in advance.

Upvotes: 0

Views: 158

Answers (2)

PhuLuong
PhuLuong

Reputation: 537

The problem is FormSubmit function is definied after calling it on PHP

The solution is defining FormSubmit function before calling

Upvotes: 0

Mahadeva Prasad
Mahadeva Prasad

Reputation: 709

Try this.

<html>
<body>
    <form name="ddd" id="ddd">
      <label>name:</name></label>
             <input id="login_name" type="text" />
      <button  type="submit" onclick="return validate(); value="Submit">
    </form> 
<?php
  if(isset($_POST['Submit']))
{
     $user_name = $_POST['login_name'];                                            
     if(empty($_SESSION['captcha_code'] ) || strcasecmp($_SESSION['captcha_code'], $_POST['captcha_code']) != 0)
     {
       $msg="<span style='color:red'>The Validation code does not match!</span>";
}else{
       echo "<script>FormSubmit();</script>";  //how to call here
 }   
}
?>
  <script>
    function FormSubmit(){
     // JS logic goes here

   }
   </script>
</body>
</html>

Upvotes: 1

Related Questions