-3

I am fairly new to php so this functionality stumps me, I'll give you a picture for context. layout of application for reference image

So I can query my data base and set each bit of data to an individual variable (maybe not the best way for this situation?) But I don't know how to $_post that data (in variable form... :) probably bad idea, and I'll have to set the data as variables where ever I work with the formulas and only send information over ajax) back to the displayed page (note: I don't want my page to ever have to refresh) And I foresee another problem, and its due to lack of knowledge and google failing to answer my questions, how can I have php Run my algorithms and keep the original data in the page every time my users change one field on the page, I want them to all run on the server and not in browser so I'm planing on having ajax send all the data including the changed data to a php file every time they change something and have it reset the information on the page after it runs its formulas. i have been studying $_post and get and request ect. and not been able to mentally layout how the transmission of data would be the most efficient and easiest to work with.

Sorry for the ramble, I hope for some constructive criticism, solutions and explanations to a newbie at php and thank you for any and all help.

YakovL
  • 7,557
  • 12
  • 62
  • 102
Gdfelt
  • 161
  • 15

1 Answers1

1

HI it works like this in jQuery

$.post('url', {input}, function(data){

 });

The javascript will make a web request, same as if you put url in the browser, right ( if it's $.get that is, obviously we cant send post data using the browser url bar, however you could go to the page just without the post data ). Now because it's AJAX anything returned goes into data. It's asynchronous, which means your JS wont wait for the request, for example if you put

$.post('url', {input}, function(data){

 });
alert(data.result);

It wont work, for 2 reasons, one is scope ( data is a function input parameter not accessible outside the function ). The other is that the alert will fire before the post completes, even though it is written after the $.post request. Because of this, you have to use the data in the call back function.

Now on PHP side where ever that url is it gets ran just like you went there normally. Essentially there is no difference to the server its AJAX or a browser request. It's just like submitting a form. The interesting things you can do is return your data as JSON. by using the application JSON header and by using json_encode() in PHP. This is essentially JavaScript String Object notation. ( not sure if I remember that right ) but that is what it is.

There is no magic in the request. Most people don't really understand that anything the server returns is only text. Be it a PDF file, a JPG, a webpage, anything its only text. JSON allows you to keep the structure of your data, such as an array. It's a special format of text that JavaScript understands as object and arrays.

I explained this to one of my Junior Developers the other day. That if you set the correct headers you can generate CSS files with PHP, images etc. Because it's all just Text. The web is very simple. You only have get and post for requests and text as a response. That's it for the most part.

Anyway,

Right, so now in PHP ( for a quick example )

  header('Content-Type: application/json');
  $a = array('one'=>1);
  echo json_encode( $a );

With json_encode() it becomes ( this is the response sent back from the server )

 '{one:1}'

And in JS on the client side ( inside the $.post callback ) its

  data.one;

We can take this data in the callback then you can just use some basic jQuery to replace the values of inputs or content of HTML tags with your returned data.

    $('#input').val(data);
    $('#htmlElement').text(data); // or data.one  - whatever you had in php array keys.

Make sense? Above, we take a PHP array use json_encode(), to make it a JSON string, and then (with the correct header ) we can access that normally in JS using its dot syntax. Remember what I said above about only returning text from the sever, this is why we have to convert it to a JSON string. The dot in JS is like the -> in PHP. You could also use data['one'] which is even more like PHP, but that is technically not the correct way if you know what one is.

The process flow is also simple, just like we only have post get, and text. We only can make requests from the client and responses from the server. So it always goes

 Client Request -> Sever Response -> Client receives response.

We cannot for example call the Client from the sever.

 Sever Call-> Client receives

This doesn't work without things like NODE.js or a socket server.

For reference ( about json responses )

Returning JSON from a PHP Script

Community
  • 1
  • 1
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • WOW! I'll upvote just for the effort you put in answering this ever too broad question. Well done – Alon Eitan Jun 25 '16 at 19:09
  • Wasn't much effort, it's like second nature to me at this point. – ArtisticPhoenix Jun 25 '16 at 19:11
  • Thank you very very much, This informed me enough to get past all my foreseeable problems. Sorry about the rant, I was kinda freaking out because I had no direction to go. – Gdfelt Jun 25 '16 at 21:29