Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi CPs,
I am currently working in Foundation and AlertifyJS. Below function checks if form element content is equal to 'heaven' and 'hell'. If it is false it shows the custom error message. But if it succeeds it should show the Success alert but the page refreshes. I was thinking about delaying the function return with callback() but didnt make a difference. Here's the code. Thanks in Advance.

C#
$(function() {
            $('.run').click(function(event) {
                var userid = document.getElementById('uid').value;
                var password = document.getElementById('pass').value;
                if(userid == 'heaven' && password == 'hell') {
                        alertify.success("Success");
                        return true;
                }
                else {
                    alertify.error("Failed");
                    return false;
                }
            });
        });


XML
<form method="post" onsubmit="return test();" action="success.php">
   <label for="uid"><b>Username</b></label>
   <input type="text" id="uid" name="uid"/>
   <label for="pass"><b>Password</b></label>
   <input type="password" id="pass" name="pass"/>
   <input type="submit" class="run" value="Log In"/>
</form>
Posted
Updated 2-Aug-15 7:25am
v2

1 solution

Apart from what you have shared, the page only refreshes in case of when the result (of expression) is true. That is because you have not stopped the code from doing what it is intended to do, submit the form.

To overcome this, write this function to your code. event.preventDefault() would make sure that the page is not refreshed, return false in the other blocks does this for you and makes sure that the code doesn't propagate and form isn't submitted (indirectly page doesn't refresh).

JavaScript
$(function() {
            $('.run').click(function(event) {
                event.preventDefault(); // This would do the trick now
                var userid = document.getElementById('uid').value;
                var password = document.getElementById('pass').value;
                if(userid == 'heaven' && password == 'hell') {
                        alertify.success("Success");
                        return true;
                }
                else {
                    alertify.error("Failed");
                    return false;
                }
            });
        });


Now when you will run the code, in both cases it won't submit the form. I would still recommend that you remove those return statements. They are of no use as event handlers are not intended to return anything at all.
 
Share this answer
 
Comments
Ashwin2013 2-Aug-15 13:23pm    
I'm sorry but the form has an action that will do necessary processing in a php file. I will update the code.
Afzaal Ahmad Zeeshan 2-Aug-15 14:36pm    
Did the code work?
Ashwin2013 2-Aug-15 15:29pm    
It does work, but it doesn't return any value. How can I pass the values to the php page? How can I resume my process after submitting the form with correct details?
Afzaal Ahmad Zeeshan 2-Aug-15 15:31pm    
You have not written any code for sending the data to PHP page. Also, your problem also didn't mention which framework to be used. I suggest that you close this thread by marking this as answer (to this problem), and start a new thread for sending the data. You will be able to seek help on the new thread you create for the problem you share. :)
Ashwin2013 2-Aug-15 15:47pm    
I was just hesitant to make another thread which will just be seriously down-voted. Imagine a thread like "How to send data from HTML to PHP?". Posting the same problem with a new line of code added in another thread seems to be a waste. while your suggestion partially worked, it creates a new problem of not sending data to the next page. This is a basic login form, so it would automatically talk with the database to authenticate or record the information. As to frameworks I'm open to suggestions, on which is better suited for the task. I was thinking about inline PHP code which would save the status to the database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900