• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Submit Form Data Using Ajax POST Method

chris.esema

Cosplayer
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
137
Likes
50
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create Submit Form Data Using Ajax POST Method. This code can compressed multiple files at the same time. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So let's now do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_submit, after that click Import then locate the database file inside the folder of the application then click ok.
2018-07-04_14_48_49-localhost_127.0.0.1_db_submit_phpmyadmin_4.7.0.png

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'db_submit'

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!

    DOCTYPE html>
  2. <

    html lang=

    "en"

    >
  3. <

    head>
  4. <

    meta charset=

    "UTF-8"

    name=

    "viewport"

    content=

    "width=device-width, initial-scale=1"

    />
  5. <

    link

    rel=

    "stylesheet"

    type=

    "text/css"

    href=

    "css/bootstrap.css"

    />
  6. </

    head>
  7. <

    body>
  8. <

    nav class

    =

    "navbar navbar-default"

    >
  9. <

    div class

    =

    "container-fluid"

    >
  10. <

    a class

    =

    "navbar-brand"

    href=

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a>
  11. </

    div>
  12. </

    nav>
  13. <

    div class

    =

    "col-md-3"

    ></

    div>
  14. <

    div class

    =

    "col-md-6 well"

    >
  15. <

    h3 class

    =

    "text-primary"

    >

    PHP -

    Submit Form Data Using Ajax post Method</

    h3>
  16. <

    hr style=

    "border-top:1px dotted #ccc;"

    />
  17. <

    div class

    =

    "col-md-2"

    ></

    div>
  18. <

    div class

    =

    "col-md-8"

    >
  19. <

    form>
  20. <

    div class

    =

    "form-group"

    >
  21. <

    label>

    Firstname:</

    label>
  22. <

    input type=

    "text"

    id=

    "firstname"

    class

    =

    "form-control"

    />
  23. </

    div>
  24. <

    div class

    =

    "form-group"

    >
  25. <

    label>

    Lastname:</

    label>
  26. <

    input type=

    "text"

    id=

    "lastname"

    class

    =

    "form-control"

    />
  27. </

    div>
  28. <

    div class

    =

    "form-group"

    >
  29. <

    label>

    Address:</

    label>
  30. <

    input type=

    "text"

    id=

    "address"

    class

    =

    "form-control"

    />
  31. </

    div>
  32. <

    br />
  33. <

    div class

    =

    "form-group"

    >
  34. <

    button type=

    "button"

    id=

    "submit"

    class

    =

    "btn btn-primary form-control"

    >

    Submit</

    button>
  35. </

    div>
  36. </

    form>
  37. </

    div>
  38. </

    div>
  39. </

    body>
  40. <

    script src=

    "js/jquery-3.2.1.min.js"

    >

    </script>
  41. <

    script src=

    "js/bootstrap.js"

    >

    </script>
  42. <

    script src=

    "js/script.js"

    >

    </script>
  43. </

    hmtl>

Creating PHP Query

This code contains the php query of the application. This code will sent the data inputs to the database server by sending ajax post request method. To do that just copy and write this block of codes inside the text editor, then save it as save.php.
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  4. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  5. $address

    =

    $_POST

    [

    'address'

    ]

    ;

  6. $conn

    ->

    query

    (

    "INSERT INTO `student` VALUES('', '$firstname

    ', '$lastname

    ', '$address

    ')"

    )

    ;

  7. echo

    "Data Inserted!"

    ;
  8. ?>

Creating Ajax Post Script

This is where the code that uses ajax request been used. This code will sent a post request to process the data and successfully to be send to the database. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. $(

    '#submit'

    )

    .on

    (

    'click'

    ,

    function

    (

    )

    {
  3. var

    firstname =

    $(

    '#firstname'

    )

    .val

    (

    )

    ;
  4. var

    lastname =

    $(

    '#lastname'

    )

    .val

    (

    )

    ;
  5. var

    address =

    $(

    '#address'

    )

    .val

    (

    )

    ;

  6. if

    (

    firstname ==

    ''

    ||

    lastname ==

    ''

    ||

    address ==

    ''

    )

    {
  7. alert(

    "Please complete the required field!"

    )

    ;
  8. }

    else

    {
  9. $.post

    (

    'save.php'

    ,

    {

    firstname:

    firstname,

    lastname:

    lastname,

    address:

    address}

    ,

    function

    (

    data)

    {
  10. alert(

    data)

    ;
  11. $(

    '#firstname'

    )

    .val

    (

    ''

    )

    ;
  12. $(

    '#lastname'

    )

    .val

    (

    ''

    )

    ;
  13. $(

    '#address'

    )

    .val

    (

    ''

    )

    ;
  14. }

    )

    ;
  15. }
  16. }

    )
  17. }

    )

    ;

There you have it we successfully created Submit Form Data Using Ajax POST Method. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top