|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I need to test for a value in PHP (that is the easy part). If the value is null I would like to call a JavaScript function that will display an alert box. Or, I could always display an alert box directly from my PHP script should PHP allow for this.
Please help either on syntax for call to JavaScript or PHP error handling... Thanks, Stephanie |
|
#2
|
|||
|
|||
|
Hi Stephanie,
The Kind of COde you may be looking for is: <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre><HTML> <HEAD> <SCRIPT LANGUAGE="javascript"><!-- function ProcA () { alert ("message"); } // --></SCRIPT> ... <? if ($value eq "") { echo "<SCRIPT LANGUAGE="javascript"><!--n"; echo "ProcA();n"; echo "// --></SCRIPT>n"; } ?> ... </HTML>[/code] Or simply: <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre><HTML> <HEAD> <? if ($value eq "") { echo "<SCRIPT LANGUAGE="javascript"><!--n"; echo " alert ("message");n"; echo "// --></SCRIPT>n"; } ?> ... </HTML>[/code] HTH, Ethereal |
|
#3
|
|||
|
|||
|
Thanks for the help it is appreciated....
This is what worked for me... <?php if ($ID = '') { echo "<script language=javascript>alert('Please enter a valid username.')</script>"; } ?> |
|
#4
|
||||
|
||||
|
Number 1:
Why use javascript? pop up alert boxes are annoying. that's the beauty of php, you can have the same code create different looks for a page, based on the values of certain variables. If something in a form wasn't filled in, show the form again, with the text colored red around it. functions work very well for this... If you have a text box called text1, then echo it's value as: echo "value=$text1"; That way if you process the form and there are errors, you can show the form again, and their original values will still be filled in. if it's the first visit to the page, $text1 will be empty, so you'll just get an empty text box... Number 2: Why use php? You can do most of your validating before you even submit the form, on the client side. if you have a form like this: <form method=post action=mypage.php3 name="my_form" onsubmit="validate();"> <input type=text name="userid"> <input type=submit value="Submit"> </form> you can use the javascript function to deny submitting the form if the values aren't filled in: <script language="javascript"> if (window.document.my_form.userid.value = '') { return false; } else { return true; } </script> the function won't submit the form if the text box doesn't have anything in it.. I know this is very lengthy, but hopefully it gives you a few options or ideas... Email me if you want some more code examples... ---John Holmes ---john.holmes@mindspring.com |
|
#5
|
|||
|
|||
|
Thank you for all the help it is much appreciated.
My web application is a little different than what has been explained here. It is basically a web-based electronic in/out board (this part of it anyhow). A user only has to log on once which is validated on the client side. That is no problem. Once they log on successfully a cookie is set to their machine to remember their UserID. My problem was that sometimes cookies get deleted and I had no validation to check whether it actually had a UserID or not. So when the page loads I need to check for a null or invalid UserID. There is no user input and no buttons clicked to invoke any procedures. My solution (yes I actually figured it out!!) was similar to this... <?php if ($UserID == '') { //plus testing for wrong values... echo "<scriptlanguage=javascript>alert('Please logon again to reset the cookie!')</script>"; } ?> It is not the most elaborate code one has seen but it works for me! Thanks again for your help! ![]() Stephanie |
|
#6
|
|||
|
|||
|
John,
Thanks for this very useful information regardding validating input from forms. Could you (or others with ideas) provide more guidance/code regarding this? Javascript followup question: <form method=post action=mypage.php3 name="my_form" onsubmit="validate();"> How is the validate() associated with the javascript code? Won't I need to create a function named validate?. E.g., <script language="javascript"> function validate() { if (window.document.my_form.userid.value = '') { return false; } else { return true; } } </script> PHP followup question: I don't know how to (re)show the same form again if there are user input errors. Do you have a code example for this? I think I'd like to put the form inside a while statement until valid values are provided. Do you recommend this approach? And to indicate to the user that the form has changed since they submitted it, I'd like to set the background color to red for the input box for which the invalid values were submitted and to add a message to "Please reenter XXXX value" next to the text input field. Do you have a PHP code examples for doing that? Thanks very much! PeteH |
|
#7
|
|||
|
|||
|
To show the form based on POST:
if(!isset($_POST)) { // show form }else{ // add to database and show message to confirm } Instead of while() loops you could use the onChange event to see whether the info has changed I believe.
__________________
Give me reputation if I'm helpful, use the scales icon, top right-hand corner ( ). Thanks!
|
![]() |
| Viewing: Dev Shed Forums > Web Design > JavaScript Development > Calling JavaScript function from PHP |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|