How to call javascript function in php code?
92,849
Solution 1
use this
if($_POST['txtUsername']=='')
{
echo '<script> showMessage("txtUsername"); </script>';
}
Solution 2
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>
Solution 3
You can put your php code anywhere you like let's say in the body like attribute. You can try the following code:
<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.
Solution 4
<?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
Author by
Kalpana Dixit
Updated on February 26, 2020Comments
-
Kalpana Dixit about 4 years
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.
-
Kalpana Dixit about 11 years:: but i dont want to show message in alert box. i want to show as innerHTML
-
Aamir Mahmood about 11 yearscheck updated answer, also note that i have changed the IDs of the textboxes
-
Aamir Mahmood about 11 yearsi just have added <script> tags around that please check now
-
Aamir Mahmood about 11 yearsYou need to define
showMessage
function above the code. check updated answer -
Nehal over 8 years@yogesh what if I need to pass the POST value in the function and then use in javascript to assign that value to input