Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    73
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Using Javascript to save Variables to DB

    Hi, I would like to SILENTLY store variables from an HTML form through JS/Ajax/PHP

    The form is used to calculate link urls via a javascript function.
    It does not post or get, and does not have a submit button, only OnClick buttons for various JS functions..

    I would like to, without any redirect or opening of new browser windows/tabs/popups, store the form data to mysql..

    This is so I can basically monitor usage of the site.. any ideas?

    (I know js is clientside, but is there a way to silently call a php function that would store the data, without opening new windows/popups/tabs/etc, etc..)

    Thanks,
    -Steven
    Last edited by stevenryals; 10-11-2013 at 06:09 PM.

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    You already mentioned the solution yourself: just issue an Ajax request to your PHP file (just search for "javascript ajax", you'll find many examples). You can do that at any point and the user won't notice (unless they're monitoring their network connection ).

    Client-side data can be manipulated easily, so make sure you don't expose sensible information or open a way to attack your servers.

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    73
    Thanks
    4
    Thanked 0 Times in 0 Posts
    thanks

  • #4
    New Coder
    Join Date
    Jul 2012
    Posts
    73
    Thanks
    4
    Thanked 0 Times in 0 Posts
    btw, are there any specific terms I should search? or just javascript ajax?

  • #5
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Those terms are enough (and you would've gotten that answer faster by just doing it). Here is a link where the highest voted answer will tell you how to do it both using Vanilla JS and the popular jQuery library: http://stackoverflow.com/questions/8...without-jquery

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You can also use hidden iframe or img as an alternative to ajax if you don't care about the response. Just set the src of the hidden iframe or img to your php file.

  • #7
    New Coder
    Join Date
    Jul 2012
    Posts
    73
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    You can also use hidden iframe or img as an alternative to ajax if you don't care about the response. Just set the src of the hidden iframe or img to your php file.

    The site is behind a decent bit of security and has a limited amount of users.. that being the case, i think and iframe would work and not create any real security issues.. i THINK..

    I've tried so many of the stackoverflow solutions and none of them are working asynchronously.. if i do a post/get it works fine, but I cant get it to work in the background for some reason.. :/ which is preferred..

  • #8
    New Coder
    Join Date
    Jul 2012
    Posts
    73
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I ended up doing this, in case anyone else is looking for something like this:

    call the function logdata(vars,vars,vars,vars);

    Code:
    function logsearch(vars,vars,vars,vars)
    {
    
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      }
      if (varcity3 > '' && vardate3 > '') {
    xmlhttp.open("POST","logsearch.php?vars="+vars+"&vars="+vars+"&vars="+vars+"&vars="+vars,true);
    xmlhttp.send(); 
    }
    }

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,334
    Thanks
    11
    Thanked 587 Times in 568 Posts
    Quote Originally Posted by stevenryals View Post
    I ended up doing this, in case anyone else is looking for something like this:

    call the function logdata(vars,vars,vars,vars);
    i don't think that's the droid you're looking for...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #10
    New Coder
    Join Date
    Jul 2012
    Posts
    73
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    i don't think that's the droid you're looking for...

    lol just got rid of my convoluted names..


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •