SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to make an error-popup message after submitting a form?

    Hi!

    First of all, I am not sure if I post this in the correct sub-forum, and if that is the case, I apologize and ask that the thread is moved to the correct category.

    Anyway. I am pretty new with web design, and I am currently learning about the subject on high school. We work on software that is a bit outdated (Adobe CS3), but I still like doing it. As for web design, we use WAMP server, Dreamweaver CS3, MySQL Query Browser and PHP. I do, however, know little to nothing about coding in PHP, I just kind of know what it's for. I know how to make a functioning web page, how to create databases and connect them to the web page, and how to show these databases through a dynamic web page, but little else.

    So the thing I am trying to do now is that after using an "Record Insertion Form Wizard" in Dreamweaver CS3 (to which I've already made a database, a query and so on), I want two different popup messages to come after clicking "submit form", both one confirming that the entire process was successful, and one saying that the user entered invalid information. I already know how to do the former, by going under Tag <input>, behaviors, clicking the plus-sign, clicking popup message and then just writing the message and hitting "OK". But how do I make a seperate one for if the submission failed? And is it possible if this box automatically tells the user exactly what went wrong?

  2. #2
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, I think you should use javascript.

    attach onsubmit event, then do evaluation of the form content

    something like that:

    Code:
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript">
    function executeOnSubmit()
    {
            var status = checkField();
    
            if(status[0] == 0)
            {
                    alert(status[1]);
                    return false;
            }
    
            var res = confirm("Submit the form contents?");
    
            if(res)
                    return true;
            else
                    return false;
    }
    
    function checkField()
    {
            var check = new Array();
            var elemValue = document.getElementById("name").value;
            if(elemValue == "") {
                    check[0] = 0;
                    check[1] = "Enter something!!!";
            }
            else if (elemValue = "1") {
                    check[0] = 0;
                    check[1] = "You entered 1!! How dare you?!!";
            }
            else {
                    check[0] = 1;
            }
            return check;
    }
    </script>
    </head>
    
    <body>
    
    
    <form action="test.php" method="post" onsubmit="return executeOnSubmit();">
    <label>Name</label><input type="text" name="name" size="20" id="name" />
    <br>
    <input type="submit" name="submit" value="submit" />
    </form>
    </body>
    </html>
    with this you will get alerts when user don't enter anything, and different alert when user input was "1".

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, but since I am doing this in school, I think I am supposed to do stuff like this in PHP (if at all possbible). I don't even know if it is possible to use both javascript and PHP in one site. So if you have a PHP-solution, that would be great.

  4. #4
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP is an server-side scripting language, that means that to check if a user made a mistake or if he completed a form correctly, has to be always done on the server.
    What you can make is a php page which is both the form and the validation of the page. In that way it looks like you will never leave the page but in fact you leave the page to exact the same page.
    Makes any sense?

    Otherwise the only solution is a javascript solution like boatmark said.

  5. #5
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gathoon View Post
    I don't even know if it is possible to use both javascript and PHP in one site.
    Of course it is possible. Javascript is client-side and php is server-side.

    The problem is - you CAN'T create popup with php. You can do all logic in php, but alerts must be created with javascript.

    for php solutiuon:

    this is your form:
    Code:
    <form action="index.php" method="POST" enctype="multipart/form-data">  
        <h2>Title</h2>  
        <input class="form" type="text" name="title"/>  
        <br/><br/>  
        <h2>Content</h2>  
        <textarea class="form" name ="textentry" rows="3"></textarea>  
        <input type="hidden" name="submitted" value="1">  
        <br/><br/>  
        <input type="submit" value="Submit"/>  
    </form>
    Note input field with type="hidden". It's important. with it we will check if form was already submitted, or not.
    like this:
    Code:
    if ($_POST['submitted']==1) {  
       //something 
    }
    Also action="index.php" - index.php is actually the page where the form is.

    and next we will add some validation:

    Code:
    <?php
    if ($_POST['submitted']==1) {  
        $errorMsg = ""; // error messages variable  
        if (!empty($_POST['title'])){  // check if title was entered
            $title = $_POST['title']; 
        }  
        else{  
            $errorMsg = "You must enter title";  // and if not - add error message
        }  
        if ($_POST['textentry']){  // check if content was entered 
            $textentry = $_POST['textentry'];  
        }  
        else{  
            if (!empty($errorMsg)){ // if there is already an error, add next error  
                $errorMsg = $errorMsg . " & content";  
            }else{  
                $errorMsg = "You must enter content";  
            }  
        }
        if (!empty($errorMsg)){ // if there is errors display them
            echo ' <script type="text/javascript">
                 alert("'.$errorMsg.'");
            </script>
            ';
        }    
    }
    ?>
    As you can see we display errors with javascript popup at the end.

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,118
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Gathoon,

    I am not familiar with Dreamweaver CS3, so I can't really answer your question, but I did a quick google search and found these results:
    http://www.youtube.com/watch?v=fBfXQqDcNSk
    http://www.youtube.com/watch?v=KpJnwbyxUE4
    The Essential Guide to Dreamweaver CS4 With CSS, Ajax, and PHP


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •