最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ajax PHP Jquery - echo-ing back data - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Nov 25, 2011 at 6:57 Howdy_McGeeHowdy_McGee 10.6k30 gold badges115 silver badges189 bronze badges
Add a comment  | 

8 Answers 8

Reset to default 18

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;
?>

add POST method:

$.ajax(
    {
        url: 'test.php',
        dataType: 'text',
        type: 'post',
        data: {test: '1'},
        success: function(data)
        {
            window.alert(data);
        }
    })

you forgot to put the method POST/GET by which you are sending the data to your php file.

$.ajax(
    {        
        type:'POST',        
        url: 'test.php',
        dataType: 'text',
        data: {test: '1'},
        success: function(data)
        {
            window.alert(data);
        }
    })
 just add "type" POST or GET     

 //example
 $.ajax(
{
    url: 'test.php',
    type: 'POST',
    data: {test: '1'},
    success: function(data)
    {
        window.alert(data);
    }
})

If you are trying to get information from the php file, I don't think the data field is required. Here is what I have.

                $.ajax(
                {
                    url: 'response.php',
                    type: 'get',
                    dataType:'text',
                    success: function(data){
                        window.alert(data);
                    }
                }

and as far as the php goes..

<?php
   echo "1";
   echo "2";
   echo "3";
   echo "4";
   echo "5";
   echo "6";
   echo "7";
   echo "8";
?>

Alert box on execution

Been struggling on this for a while, my issue wasn't on the js but in php script : error reporting was set to E_ALL - which apparently blocks it from returning data to the AJAX call. My guess is it prints notices before the final echo and blocks it all. Weird thing is it didn't print me any notice in the logs i set up to get to the bottom of this.

Changed it to E_ERROR and now works like a charm.

发布评论

评论列表(0)

  1. 暂无评论