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,

9
  • 3
    all you would need is echo $_GET['name'];
    – Kris
    Jul 22, 2012 at 5: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, 2012 at 5:58
  • @IgnacioVazquez-Abrams yes, it is jQuery.
    – Brett
    Jul 22, 2012 at 5:59
  • So then this question has nothing to do with JSON. Or POST. Please edit. Jul 22, 2012 at 6: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, 2012 at 6:04

3 Answers 3

6

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

echo $_GET['name'];
1
  • 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, 2012 at 6: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!

1
  • 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, 2012 at 6: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.