Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling javascript fn from php 1

Status
Not open for further replies.

amir4oracle

Programmer
Nov 3, 2004
46
CA
The following code has a javascript function commalrt() instead giving the alert it echos: javascript: commalrt() in the window how can i invoke the javascript fn from php

The Code is:


<html>
<head>

<script language="javascript">

function commalrt() {
alert ('You forgot to enter Comment');
}
</script>

</head>



<body>

<?php
.
.
if (empty($_POST['comments']))
echo "javascript: commalrt()";

.
.
?>
.
.
<input type="text" name="comments" size="15" value="<?php echo $_POST['comments']; ?>"/>
.
.
</body>
</html>

.
.
<input type="text" name="comments" size="15" value="<?php echo $_POST['comments']; ?>"/>
.
.
</body>
</html>
 
What exactly are you trying to achieve? Do you realize that JavaScript runs on the client and PHP on the server. Do you know that all you will do with what you have that as soon as the new page loads, user will get an anoying alert box. You could just print out in red letters the fields the user forgot to enter.
 
NigeW I coded the function call like thisas you said, still it gives error:

if (empty($_POST['addi_info'])) echo "<script language="javascript"> alrt();</script>";

can you tell me what is the exact syntax, thanks

Vragabond: I use red text messages if there is a big form validation required. Saying this this and this are missing or not appropriately entered. Thanks for the concern though.
 
you just echo or print what you want in the html (remembering to escape any nested quotation marks...)


Code:
if (empty($_POST['addi_info'])) :
echo "<script type=\"text/javascript\">alrt();</script>";
endif;
[\code]
 
I did it simply like this and it worked
echo "<script> alrt();</script>";

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top