Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9036211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:56:00+00:00 2026-06-16T08:56:00+00:00

I am trying to use ajax jquery concept in my application, which is new

  • 0

I am trying to use ajax jquery concept in my application, which is new to me. I have written out the code. But not getting exact output. My code is like:

<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<form name="ajax" id="ajax" method="post" action="">
 Username: <input type="text" name="username" id="username" /><br />
 Password: <input type="text" name="pass" id="pass" value="" /><br />
 <input type="submit" name="sub_btn" id="sub_btn" value="Login" />
</form>
<script type="text/javascript">
$(document).ready(function(){
 $('#sub_btn').click(function(){
  var username = $('#username').val();
  var password = $('#pass').val();
  var datastring = "username="+username+"&password="+password;
  $.ajax({
   type:"POST",
   url: "bin/login-process.php",
   data: datastring,
   success: function(){
    alert("Success");
   },
   error: function(){
    alert("Fail");
   }
  });   
 });
});
</script>

and in the bin/login-process.php file I just echo “hello”.
Now if I am trying to run this page,I am getting the alert message(“Fail”) but that is for a very very short time and it again redirects to the same page. But I don’t want to disappear the alert message until user clicks on the “ok” button.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:56:01+00:00Added an answer on June 16, 2026 at 8:56 am

    You have two ways to fix this problem.

    1. Change the button type to “button”

    <input type="button" name="sub_btn" id="sub_btn" value="Login" />
    

    2. Modify your click handler

    $(document).ready(function(){
        $('#sub_btn').click(function(event){
            event.preventDefault();
    

    Reason:
    When you add a submit button to your form, clicking the button trigger a submit event hence submit the form to the provided action, in your case you want to submit your form using Ajax so your should prevent the default behavior by adding event.preventDefault() that means you are preventing default action of the event.

    Suggestion:

    Instead of hooking click event of the button, hook the submit event of the form, after all people can also submit your form but hitting “Enter” key. This will work for both mouse click and keyboard Enter key.

    $(document).ready(function(){
        $('#ajax').submit(function(event){
            event.preventDefault();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use AJAX via jquery with ASP.NET MVC, but I'm not able
I'm trying to use Ajax in CakePHP, and not really getting anywhere! I have
I'm trying to use AJAX (not jQuery).... I basically have this going on. Content
I'm trying to use Ajax of jQuery, I have this code below and although
I'm trying to use some jQuery code for Ajax which works well in Firefox
In my Rails app, I'm trying to use jQuery ajax to create a new
I'm trying to figure out how to use jQuery AJAX with Python's Bottle module.
I am trying to use a jquery in my application where i have already
Im ready to start pulling my hair out here. Trying to use jquery ajax
I am trying to use Jquery / ajax in rails 3.0.7. I do not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.