Ajax send the data, but php doesn't recieve

我是研究僧i 提交于 2019-12-04 23:26:50
Graham Ritchie

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

change success to the following:

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

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

FOR CLARITY

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

'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.'

Try this:

change $_POST['bar'] to $_GET['bar'];

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.

For data to persist you need to store to a database or a text file.

Hope that is clear!

If you're on windows, get Fiddler. It's a free packet sniffer from Microsoft. It will allow you to see what is in between. You will be able to see what the socket sees coming from the browser and what the socket receives from the server.

http://msdn.microsoft.com/en-us/library/bb250446%28v=vs.85%29.aspx#ie_introfiddler_topic2

It may be a little overkill for what you are doing, but I think it's always good to have great tools for debugging. It buys you options, and opens possibilities.

There are others out there too. And I'm sure there are nix tools available as well.

I think everything is correct except you dint pass any value from you form.Just replace this code

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

I think it will solve your problem.And add change this small code in js

Form

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

TO

success: function(result){
    $("#result").html('Submitted successfully'+result);
},
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!