Web Analytics Made Easy -
StatCounter Ajax code not working properly - CodingForum

Announcement

Collapse
No announcement yet.

Ajax code not working properly

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Ajax code not working properly

    What I want to do is to basic database insert .Actually inserting part works fine the problem is index.php redirects process.php after inserting.But It should stay at the same page and the clear all the fields What am I doing wrong.

    this is index.php file

    Code:
     <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Title</title>
        
        </head>
        <body>
        <form action="process.php" id="myForm" method="post">
            UserName <input type="text" name="uname"><br>
            Pass <input type="text" name="pass"><br>
            FirstName <input type="text" name="fname"><br>
            LastName <input type="text" name="lname"><br>
          <button id="submit" value="register"></button>
        
        
        </form>
        <div id="ack">Ack</div>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <script type="text/javascript" src="site.js"></script>
        </body>
        </html>
    this is the site.js
    Code:
        $("#submit").click( function() {
        
            $.post( $("#myForm").attr("action"),
                $("#myForm :input").serializeArray(),
                function(info) {
        
                    $("#ack").empty();
                    $("#ack").html(info);
                    clear();
                });
        
            $("#myForm").submit( function() {
                return false;
            });
        });
        
        function clear() {
        
            $("#myForm :input").each( function() {
                $(this).val("");
            });
        
        }
    And this is for inserting

    Code:
      <?php
        require("config.php");
        $uname =$_POST["uname"];
        $pass = $_POST["pass"];
        $fname = $_POST["fname"];
        $lname = $_POST["lname"];
        
        $insert=$db->prepare("insert into users (uname,pass,fname,lname) values(?,?,?,?)");
        $insert->bind_param("ssss",$uname,$pass,$fname,$lname);
        if($insert->execute()){
            echo "ok";
        }
        else
        {$db->error;}
        
        ?>

  • #2
    Your code works for me.

    Are you sure of the spelling of the filename and path for the site.js file?

    Have you saved all changes to files and reloaded the page in your browser after making any changes?

    Have you used your browser's developer tools/console to check for client-side errors?
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment


    • #3
      I have added this line of code and it works now any idea?
      src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">

      Comment


      • #4
        Why are you using such an antiquated version of jQuery? The current version is 3.1.1
        Last edited by felgall; Sep 29, 2016, 05:01 PM.
        Stephen
        Learn Modern JavaScript - http://javascriptexample.net/
        Helping others to solve their computer problem at http://www.felgall.com/

        Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

        Comment


        • #5
          thank you all

          Comment

          Working...
          X