I'm having trouble bringing back data from my PHP file. I guess I don't really understand the data parameter of this jquery function so I just went off some tutorials.
Jquery
$.ajax(
    {
        url: 'test.php',
        dataType: 'text',
        data: {test: '1'},
        success: function(data)
        {
            window.alert(data);
        }
    })
Now from my understanding the test: declares the variable used in php and 1 is the value in that variable. But I'm not entirely sure...
Here's my PHP
$item1 = $_POST['test'];
    echo $item1;
Now it's just supposed to alert that value so I know it is at least returning something but in the alert it's just blank so I'm losing the value somewhere, but where?
use $_REQUEST it will handle both the GET and POST
$item1 = $_REQUEST['test'];
by default ajax request is GET type, either specify expilicitly the type like
$.ajax(
    {
        url: 'test.php',
        type:'POST'
        dataType: 'text',
        data: {test: '1'},
        success: function(data)
        {
            window.alert(data);
        }
    })
or use $_GET like
item1 = $_GET['test'];
    echo $item1;
                        The correct way:
<?php
$change = array('key1' => 'blabla', 'key2' => '12432rr424234');
echo json_encode($change);
?>
Then the jquery script:
<script>
$.get("location.php", function(data){
  var mydata= $.parseJSON(data);
  var art1 = mydata.key1;  // <-----------  access the element
});
</script>
                        This is work for me
ajax code
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
        <title>Test Page</title>
        <script>
            $.ajax(
                    {
                        url: 'test.php',
                        type: 'POST',
                        dataType: 'text',
                        data: {latitude: '7.15588546', longitude: '81.5659984458'},
                        success: function (response)
                        {
                            alert(response);
                        }
                    });
        </script>
    </head>
    <body>
    </body>
</html>
php code (test.php)
<?php
$lat = $_REQUEST['latitude'];
$lon = $_REQUEST['longitude'];
echo 'latitude- '.$lat . ', longitude- ' . $lon;
?>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With