#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Posts
    8
    Rep Power
    0
    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. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    1
    Rep Power
    0
    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
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Posts
    8
    Rep Power
    0
    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>";
    }
    ?>
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,646
    Rep Power
    4492
    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
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Posts
    8
    Rep Power
    0
    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
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Location
    Northern California
    Posts
    1
    Rep Power
    0
    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
  12. #7
  13. No Profile Picture
    Room for advertising!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Location
    Here.
    Posts
    344
    Rep Power
    57
    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.

    Comments on this post

    • mikeyskona agrees : About time you got a little rep for being helpful ;)
    Give me reputation if I'm helpful, use the scales icon, top right-hand corner (). Thanks!

Similar Threads

  1. PHP scripts to run in Javascript continuously
    By linhardt in forum JavaScript Development
    Replies: 0
    Last Post: February 13th, 2004, 02:51 PM
  2. Calling a JavaScript function from VBScript
    By starbar in forum JavaScript Development
    Replies: 3
    Last Post: February 7th, 2004, 04:31 PM
  3. Replies: 2
    Last Post: February 5th, 2004, 06:01 PM
  4. javascript inside php tags
    By andy123 in forum JavaScript Development
    Replies: 5
    Last Post: February 5th, 2004, 04:34 PM
  5. Scriplet issue : accessing javascript function
    By creativedr in forum JavaScript Development
    Replies: 1
    Last Post: February 4th, 2004, 10:22 PM

IMN logo majestic logo threadwatch logo seochat tools logo