阿贾克斯发送数据,但PHP不接受她不接受、数据、阿贾克斯、PHP

2023-09-10 17:02:48 作者:伤半世流离

虽然我还在读书尼古拉斯C. Zakas的书(读4天之内超过300页),在此期间,我有一个小项目,以创建一个半automatized文档生成页面,为以后的大项目。这是做了一半,但现在我想给更多的功能,它要求我需要文本域的值发送到PHP的,所以PHP可以更新mysql和正相反,但是这不是事了。的

的问题:阿贾克斯成功,我获得成功的消息,但PHP不阅读帖子。为什么呢?

The problem: Ajax succeed, I get the success message, but PHP doesn't read the POST. Why?

我做我的研究,我读的计算器主题和其他网站,phpmanual,w3school的例子,但事情错了音。

I made my research, I read tones of stackoverflow topics and other websites, phpmanual, w3school examples, but something is going wrong.

<form id="foo">
   <label for="bar">A bar</label>
   <input id="bar" name="bar" type="text" value="" />
   <input type="submit" value="Send" />
</form>

<div id="result"></div>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="script.js"></script>

基本的ajax调用(发送)我发现这一点:

/* Attach a submit handler to the form */
$("#foo").submit(function(event) {

    /* Stop form from submitting normally */
    event.preventDefault();

    /* Clear result div*/
    $("#result").html('');

    /* Get some values from elements on the page: */
    var values = $("#foo").serialize();
    console.log("val: "+values);

    /* Send the data using post and put the results in a div */
    $.ajax({
        url: "ez.php",
        type: "POST",
        data: values,
        success: function(){
            $("#result").html('Submitted successfully');
        },
        error:function(){
            alert("failure");
            $("#result").html('There is error while submit');
        }
    });
});

简单的PHP测试,如果我得到的数据还是不

<?php

   if(isset($_POST['bar']))
   {
       echo "hello " . $_POST['bar'];
   };

?>

所有这三个文件都在同一个文件夹中。

All three files are in the same folder.

我一定会了解有关此主题的更多更深以后,刚才我需要尽快完成这个小项目。我要成为前端开发,所以PHP的一部分,不会等我,但现在我需要它的测试,直到我可以与服务器端程序员说。我想确保我会送他的哪些变量和数据,这样他可以在以后处理它们。

I'm surely will learn about this topic more and deeper later on, just now I need to finish this small project as soon as possible. I'm going to be front-end developer, so the php part won't wait for me, but now I need it for testing until I can speak with the server-side programmer. I want to be sure which variables and data I'll send to him, so he can handle them later.

在此先感谢!

推荐答案

你怎么知道你的脚本不接收数据?

How do you know your script isn't receiving the data?

变更成功如下:

success: function(data){
        $("#result").html('Submitted successfully:' + data);
    },

所有你做的是说'如果接收到应答,而不错误更新一个div' - 你没有办法知道,如果你的PHP脚本已成功运行(在其运行时,你越来越成功)或不在的一会儿。

All you are doing is saying 'if a response is received without an error update a div' - you have no way of knowing if your php script has run SUCCESSFULLY (it has run as you are getting success) or not at the moment.

在该函数的'数据'然后你可以看到,如果信息已经传回你的脚本。

with the 'data' in the function you can then see if information has been passed back by your script.

试试上面,让我们知道您的身体情况如何

Try the above and let us know how you get on

为了清楚起见,

PHP脚本,当您访问它们的运行一次 - 他们有没有记忆(不是固定变量,如 $其他奶酪=美味;

PHP scripts when you access them are run ONCE - they have no memory (other than fixed variables such as $cheese = "yummy";

你看不到的响应,当您访问的页面(即使在运行脚本之后)的原因是,这是一个有点傻,不记得你运行它。

The reason you cannot see a response when you access the page (even after running your script) is that it is a bit stupid and doesn't remember you ran it.

所以访问脚本直接在脚本是怎么回事

So accessing the script directly the script is going

哦 - 它的表演时间,正确的.....犯错..... OK - 已的人给我发了'吧' - 没了 以及在这种情况下我也不会做什么,如果语句中 - 我的作业结束 - 无关这里我就什么都不发回

'Oh - it's show time, right.....err.....ok - has the person sent me 'bar' - nope well in that case I will not do what is inside the if statement - end of my job - nothing to do here I will send nothing back.'

试试这个:

变更 $ _ POST ['酒吧']到$ _GET ['酒吧'];

然后转到您的网址,其中剧本和类型 http://yoururl.com/script的.php?巴=泰伦斯

Then go to your URL where the script is and type http://yoururl.com/script.php?bar=terrence

您会发现,你现在得到回应,因为你通过什么想回到真实,所以做什么,如果语句中的脚本。

You will notice you now get a response because you passed the script what it wanted to return true and so do what is inside the if statement.

对于数据的持久化,你需要存储到数据库或文本文件。

希望这是显而易见的!