Search code examples
javascriptjqueryhtmlajaxvxworks

how to send a single element from browser to server without reload


How can I send just one element from browser to server fast and without reloading browser page?

Is there an AJAX way to do this, that is a NON-FILE method? The opposite of ".load"?

.load works great sending a single element from server to browser without page reload. How to do the opposite direction?

Browser is JavaScript. Server is vxworks with windmarks.

PRESENT METHOD THAT WORKS BUT RELOADS PAGE: Presently, the browser element is and I use submit to send it to the server, but this takes too long and reloads the browser page.

The element's innerHTML contains data formatted as a vxworks WINDMARK. (When the vxworks server receives this submission, it reads the windmark and copies it to a 'C' string for backend software to process.)


Solution

  • You can get and send data using jQuery. using something like this:

    $.post('urlfromserver',browserdata,function(datafromserver){
        //do stuff
    })
    

    if you let me put a little bit more, it's a good idea to use JSON to send/receive data to/from server. Having that in mind, you can do something like:

    $.post('urlfromserver',{browserdata: JSON.stringify(browserdata)},function(datafromserver){
        javascriptObject = jQuery.parseJSON(datafromserver)
        //do stuff
    })
    

    And in your PHP code, it would be as simple as using json_encode to send data to javascript and json_decode to receive data from javascript

    UPDATE

    Obtaining the data in the server should be as simple as requesting the object via post or get depending on your send method, and parsing the JSON.

    In PHP, this is an example of obtaining data using the above code:

      $dataFromBrowser = json_decode($_POST['browserdata'])