Javascript 如何在php代码中调用javascript函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14977151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:33:12  来源:igfitidea点击:

How to call javascript function in php code?

phpjavascriptvalidationserver-side-validation

提问by Kalpana Dixit

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.

在我的登录表单上,我将进行服务器端验证,如果发生错误,我想在经过验证的控件下方显示这些错误。现在为此,我正在尝试调用 javascript 函数以在 php 代码中显示验证消息,但无法调用。

<?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.

请告诉我如何在表单中经过验证的控件下方显示服务器端验证。

回答by Aamir Mahmood

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>

回答by Yogesh Suthar

use this

用这个

if($_POST['txtUsername']=='')
{
     echo '<script> showMessage("txtUsername"); </script>';
}

回答by Riste

You can put your php code anywhere you like let's say in the body like attribute. You can try the following code:

你可以把你的 php 代码放在任何你喜欢的地方,比如在 body like 属性中。您可以尝试以下代码:

<body  <?php

        if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['txtUsername']=='')
{
  echo "onload = 'showMessage("VALUE")'";    
}
}
?> > // end of body start tag

 <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>
</body>

<script type="text/javascript">

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

If the validation is successful the php code wont echo anything and the javascript function will not be called. Works for me :). Tell me if this helps.

如果验证成功,php 代码不会回显任何内容,并且不会调用 javascript 函数。对我有用:)。告诉我这是否有帮助。

回答by Samy

    <?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
      }
    }
?>              //Its not working