jQuery + Ajax + JSON

Discussion in 'jQuery' started by jonathandey, Nov 3, 2010.

  1. #1
    I'm need a little help as I'm new to this what I'd like to do is make the div box give the output of the PHP and after some research I believe using json is the best thing to do.

    So here is my jQuery, I post the data to the PHP and show the div box. This all works.
    <script type="text/javascript">
    						function remove() {
    							var userid = <?= $u['id']; ?>;
    							var usertype = "PA";
      							   $.ajax({
          							type: "POST",
          							url: "connectionssu.php",
          							dataType: 'json',
          							data: "userid=" + userid + "&usertype=" + usertype,
          							success: function(){
          								$(".user"+userid).remove();
          								$('div.success').fadeIn();
          							}
       							});
    
    						}
    
    					</script>
    Code (markup):
    Then my PHP picks it up using

    if(isset($_POST['userid'])){
    
    	$id = $_POST['userid'];
    	$type = $_POST['usertype'];
    	mysqli_query($dbc, "UPDATE connections SET requestorid = '1' WHERE requestingid = '$id' AND requestingtype = '$type'");
    	echo "done";
    }
    PHP:
    At the moment a div box fades in as displayed below, however I would like the div box content to give the output from the PHP.

    <div class="success" style="display:none;"><span style="color:green;font-weight:bold;">You Ended The Connection</span</div>
    Code (markup):
    Can someone help me?
     
    jonathandey, Nov 3, 2010 IP
  2. GNi33

    GNi33 Peon

    Messages:
    16
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In the php file, you can return the output you want to see in the div.

    in the .ajax() - function you already use the success-parameter with a function.
    However if you define function(returnValue), the output you returned in the php file will be stored in the "returnValue" - variable, so you can add this into your div...

    hth
     
    GNi33, Nov 3, 2010 IP
  3. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Do I need datatype: 'json' ?
     
    jonathandey, Nov 3, 2010 IP
  4. GNi33

    GNi33 Peon

    Messages:
    16
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    no, if you just want to get an html - output, you can use "html" as datatype too

    
    $.ajax({
        type: "POST",
        url: "submit.php",
        data: submitData,
        dataType: "html",
        success: function(msg){
      }
    });
    
    Code (markup):
    this is how I used it for a commenting-system. The submit.php returns the new comment as html-code and in the success-function I simply add it to the right place in the DOM
     
    GNi33, Nov 3, 2010 IP
    jonathandey likes this.
  5. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Thanks a lot I have it working now :D
     
    jonathandey, Nov 3, 2010 IP
  6. GNi33

    GNi33 Peon

    Messages:
    16
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    glad I could help
     
    GNi33, Nov 3, 2010 IP