$ .post功能似乎没有发布

I am new to both javascript and php. I am trying to write a javascript function that will post a string back to the server so that I can capture it and save it in the database. It will eventually post data obtained from the google.maps geocoding api,but for now, I'm just trying to get ANY post to work. I have been able to post to the server with html forms, but not successfully yet using a javascript function, and I have a hunch that I am missing something basic.

I have included the jquery library in my Script.js file with: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

I have then added this function, which I pretty much just copied from the examples in my Script.js file:

<script>
function namepost(){
document.write("Tired of trying to debug this bloody thing");
$.post("test.php",{firstname:"John"});
}
</script>

For testing purposes, I have created the file test.php, and here is the code:

<?php
require_once 'Script.js';
echo "<script>namepost()</script>";
$name = $_POST['firstname'];
print_r($_POST);
echo "Firstname is {$name}";

Here is the output I get when I run the code:

Tired of trying to debug this bloody thingArray ( ) Firstname is

My function is getting called, but it doesn't appear that I am posting firstname because the $_POST gives me an empty array. I can't tell why, and I have been at this for hours and hours. Is there another file I need to include to use the jquery $.post function? Please, help me save the rest of my hair and sanity!

查看全部
dongzhiman2162
dongzhiman2162
2014/04/22 17:48
  • post
  • php
  • ajax
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复