Lena Bru
Lena Bru

Reputation: 13937

send data to php and receive response

i want to send data from a jquery script file to a php script file process it and receive a response, however, the response comes as the content of the php file

what to do ?

this is my jquery script:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>test</title>
    <script type="text/javascript" src= "jquery.js"></script>
    <script type="text/javascript">
        function Client(name,lastName){
            this.name = name;
            this.lastName = lastName;
        }
        $(function(){
            var submit = $("#submit");
            var clientName = $("#clientName");
            var clientLastName = $("#clientLastName");
            submit.click(function(){
                var client = new Client(clientName.val(),clientLastName.val());
                alert(JSON.stringify(client));

                $.ajax({
                    type :'GET',
                    url:'save.php',
                    data: {json:JSON.stringify(client)},
                    success:function(data){
                    alert(data);
                        }
                    });
            });


        });

    </script>
</head>
<body >
    <div>
        <label for="clientName" >clientName</label>
        <input id="clientName" type="text"/>
        <label for="clientLastName" >clientLastName</label>
        <input id="clientLastName" type="text"/>
        <input type="submit" id="submit" value="submit"/>
    </div>
</body>

this is my php script:

<?php
 $value = json_decode($_GET['json']);
 echo $value['clientName'];
?>

this is what i get in return :

enter image description here

what do i need to do to get only the echoed variable rather than the contents of the php file?

Upvotes: 0

Views: 550

Answers (2)

user2541120
user2541120

Reputation:

Check if php is installed on your computer or else install wamp server and then run your code.

Upvotes: 1

curious_coder
curious_coder

Reputation: 2458

Check whether php can be compiled in your localhost as suggested by Nirazul.
To access json values in php make use of ->. Modified script is below

<?php
 $value = json_decode($_GET['json']);
 echo $value->name; // or $value->lastName
?>

Upvotes: 1

Related Questions