Loading...

9 Ajax Request to PHP and MySQL with JSON

9 Ajax Request to PHP and MySQL with JSON


RRelated Posts

Create dynamic JSON file in PHP Mysql

This video tutorial show you how to create dynamic json file from taking data from mysql database and convert that data into one array and convert that array to json file formate and thenafter create

Beautiful REST & JSON APIs

The design behind an equally clean and intuitive REST + JSON API is no easy feat. For one, you've got to worry about resources, collections of resources, pagination, query parameters, references to

CComments

  • Appelkoekje
    A few things are done not as it should be:
    - Dont send the data url encoded, but send it as an json object (use serialize)
    - Never use $_REQUEST. It's bad practice
  • Md. Shahjahan Jewel
    This idea helps me a lot, thanx to 'therealdarthnix'
  • Thuận Lê
    can you give me sourse code? thank you
  • Pankaj Rana
    thanks a lot
  • Touhida Sultana
    nice
  • Cesar Cisneros
    Dude, This was so Helpful! Vejo que você fala português no seu computador. Onde você mora?
  • Webslesson
    it's cool. Nice video. Thanks for sharing this video.
  • PRAVEEN RAWAT
    same problem with me writing the same code but no affect , submit button is not showing any message.
  • Arvind Rawat
    this is my code , i don't where i am wrong ,, can any one help me to sort out this prob.

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Add Student Info</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="bootstrap.min.css">
    </head>
    <body>
    <div class="container" >
    <h2>Login</h2>
    <form role="form">
    <div class="form-group">
    <label for="email">Email:</label>
    <input type="email" class="form-control" id="email" placeholder="Enter email" name="eml">
    </div>
    <div class="form-group">
    <label for="pwd">Password:</label>
    <input type="password" class="form-control" id="pass" placeholder="Password" name="pass">
    </div>


    <input type="button" name="test" id="submit" class="btn btn-default" value="Submit"></button>
    <div id="result">

    </div>
    </form>
    </div>
    <script src="jquery.min.js"></script>
    <script src="jquery-1.9.js"></script>
    <script>
    $(document).ready(function(){

    $("#submit").click(function(){

    var user = $("#email").val();
    var pass = $("#pass").val();

    $.ajax({
    url:"login.php",
    type:"POST",
    data:"user1="+user+"&pass1="+pass,
    dataType:"JSON",
    success:function(msg,string){

    $('#result').html(msg+string);
    }
    });
    });
    });
    </script>
    </body>
    </html>



    <?php
    //if(isset($_POST['test']))
    //{
    $id=$_POST['eml'];
    $pass=$_POST['pass'];

    $data = array(
    'email' => $id,
    'pass' => $pass

    );
    echo $json_encode($data);
    //}
    ?>
  • Maksym Morozenko
    Could you help me. I have a code, but haven't successfull result!

    nullsuccess[object XMLHttpRequest]

    Where I wrong?

    <html>
    <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    </head>
    <script type="text/javascript">
    $(document).ready(function(){

    $("#button").click(function(){
    var author=$("author").val();
    var book_name=$("#book_name").val();
    var count_pages=$("#count_pages").val();

    $.ajax({
    type: "POST",
    url : "api.php",
    data : "author="+author+"book_name="+book_name+"count_pages="+count_pages,
    dataType: "json",
    success: function(msg,string,jqXHR){
    $("#result").html(msg+string+jqXHR);
    }
    });
    });
    });
    </script>
    <body>
    Автор книги:<br>
    <input type="text" id="author" name="author"><br>
    Название книги:<br>
    <input type="text" id="book_name" name="book_name"></br>
    Количество страниц:<br>
    <input type="text" id="count_pages" name="count_pages"><br><br>
    <input type="button" id="button" name="button" value="Зарегистрировать"><br>
    <p><div id="result">Результат:<br></div>
    </body>
    </html>
  • Juan Muñoz
    Where is the Mysql Code ? THnk u
  • Sameera Lakshan
    Thank you sir Its really Helpful :)
  • Oscar Fernando Mora González
    Hi Dilan, do you need check this line:
    var sendU=$("username").val();
    var sendP=$("password").val();

    The correct form is (you need to add the symbol #):
    var sendU=$("#username").val();
    var sendP=$("#password").val();
  • Dilan Wijerathne
    Thank you, I got an idea, but it gives me like this, print result as 

    undefined
    undefined

    my ajax.html code is:

    <html>
    <head>
      <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
      <script type="text/javascript" >
      
      $(document).ready(function(){
    $("#button").click(function(){
    var sendU=$("username").val();
    var sendP=$("password").val();

    $.ajax({
    type:"POST",
    url :"ajax.php",
    data :"username="+sendU+"&password="+sendP,
    dataType:"json",
    success:function(msg,string,jqXHR){
    $("#result").html(msg.name+"<br>"+msg.password);
    }
    });
    });
      });
      </script>
    </head>
    <body>
    User Name : <input type="text" id="username" name="username"/><br><br>
    Password : <input type="password" id="password" name="password"/><br><br>
    <input type="button" id="button" name="submit" value="Submit"/><br><br>

    <div id="result"></div>
    </body>
    </html>


    and my ajax.php code is :

    <?php

    $name=$_REQUEST['username'];
    $password=$_REQUEST['password'];

    $list=array('name'=>$name,'password'=>$password);

    $c=json_encode($list);

    echo $c;
    ?>


    Can you help me ?
  • Grisaille
    Not sure why the dislike, but this is the only video that helped me! Thanks!
  • kemal karaduman
    thank you so much
    you really helped me alot
  • Omran AbazId
    thank you so much 
    you really helped me alot