1

Sorry about the simple question.

I am posting a JSON object to a PHP page using the following code:

    $.get("ProcessName.php", { name: "John" },
        function(data){
            alert("Data Loaded: " + data);
        });

What code do I need to write in ProcessName.php to have the alert show the name is John?

I realise I could process the JSON object on the client but this is a simple example to help me understand how PHP pages read JSON objects sent from the client. I have ready many questions and beginner tutorials but they all seem to skip this simple step or maybe I am missing something.

Thanks,

Kris
  • 6,094
  • 2
  • 31
  • 46
Brett
  • 3,296
  • 5
  • 29
  • 45
  • 3
    all you would need is echo $_GET['name']; – Kris Jul 22 '12 at 05:56
  • well it may have cost me some reputation to find an answer but I really did not know how to do this. Thanks for your answer @Kris. I don't know why nobody explicitly stated this is what they were doing. – Brett Jul 22 '12 at 05:58
  • @IgnacioVazquez-Abrams yes, it is jQuery. – Brett Jul 22 '12 at 05:59
  • So then this question has nothing to do with JSON. Or POST. Please edit. – Ignacio Vazquez-Abrams Jul 22 '12 at 06:02
  • I am sure you know what edits you are talking about @IgnacioVazquez-Abrams but I have no idea. If you make the relevant changes I will approve them. – Brett Jul 22 '12 at 06:04
  • I can remove all references to "JSON" and replace "POST" with "GET", but then the final paragraph won't make any sense since it is very, very full of inaccuracies. – Ignacio Vazquez-Abrams Jul 22 '12 at 06:07
  • I don't see what's so inaccurate about the question. He only said "posting", not "POST", and `{ name: "John" }` is JSON, albeit very simple JSON. What exactly is the problem? – Jonathan Jul 22 '12 at 06:14
  • I retagged it from json to jquery. I don't see what the big deal is – Kris Jul 22 '12 at 06:15
  • Thanks Kris and and Joe. – Brett Jul 22 '12 at 06:22

3 Answers3

6

In your ProcessName.php page, to alert John all you would need is

echo $_GET['name'];
Kris
  • 6,094
  • 2
  • 31
  • 46
  • 1
    No problem! I know when I first started AJAX it was all very confusing, but all the tutorials never seemed to explain the very basics and they just assumed you knew the basics. – Kris Jul 22 '12 at 06:02
0

js:

 $.get("ProcessName.php", { name: "John" },
        function(data){
            alert("Data Loaded: " + data);
        });

ProcessName.php:

<?php

if($_GET['name'] == "John") {
    echo "This work!";
}

?>

or sleep

<?php

sleep(200); 

if($_GET['name'] == "John") {
    echo "This work!";
}

?>

or

<?php

echo $_GET['name'] == "John" ? "At works" : null;

?>

for example :) if you need append response to html use

$.get("ProcessName.php", { name: "John" },
        function(data){
            $("#append").html(data);
        });

your need create div id=append, for example! good luck!

johniek_comp
  • 322
  • 3
  • 8
  • This answer is good as well but I am going for the simplest answer as I deliberately asked this as a beginner question. Thanks for your help @johniek. – Brett Jul 22 '12 at 06:12
0

In Javascript/JQuery if you want to access JSON you can write

data.name

if you want to get the posted value on the page you can get it 2 ways

$_POST['name'];

and

$_REQUEST['name'];

you can also include the following statement

if(isset($_POST['name']))
    echo "Name is ".$_POST['name'];

some people consider using $_REQUEST is bad practice, basically $_REQUEST checks the name in both $_POST and $_GET. To clear your concept you can click here.

Community
  • 1
  • 1
Salman
  • 1,380
  • 7
  • 25
  • 41