0

I've been using PHP to insert a user's IP address into a database like this:

INSERT INTO ip_hits (address) VALUES ('$user_ip')

One of our new vendors is requiring that we use a Javascript function to do this and I'm not very familiar with how to use Javascript for this sort of thing.

Basically I simply need a Javascript function, that when executed, will insert the current visitor's IP address into our database. Is this possible to do? I've been looking for simple examples of doing this, but all of the answers seem quite different.

James Sandzalone
  • 31
  • 1
  • 1
  • 4
  • As described, It's a nonsensical requirement. Why JS? Sure you could do it, but it's way easier and better to do it in PHP or, in general, on the server-side. – VLAZ Oct 25 '16 at 18:02
  • Totally agree, unfortunately we're kinda in the position that we need to do as larger vendors ask of us :/ – James Sandzalone Oct 25 '16 at 18:04
  • @AndreCanilho what does that have to do with the question? – VLAZ Oct 25 '16 at 18:08
  • Use AJAX to send the request to a PHP script. – Jay Blanchard Oct 25 '16 at 18:11
  • @JamesSandzalone if I were you'd clarify the requirements with your client because, as I said, it doesn't seem sensible. It might be that they misunderstand something or you did or, perhaps, they didn't communicate clearly what they want. At any rate, perhaps there is something there that is different and you may be able to provide. If they insist and if you can get away with it, I'd just make it _almost_ conform - do an AJAX request but _don't_ send the IP, just pick it up from the request header on the server-side and insert it. – VLAZ Oct 25 '16 at 18:12
  • @vlaz... Did he changed the question? Because I think I saw a request to use "mysql" from javascript , to send the IP -.- – Canilho Oct 25 '16 at 18:12
  • 1
    @AndreCanilho the question is, apparently, for "JavaScript" to log the IP...somehow. Doesn't make much sense to me but still - it does not really touch using, or not using, `mysql_*` functions in PHP. – VLAZ Oct 25 '16 at 18:14
  • The reason it doesn't touch that @vlaz is because the OP is just aware those things are required to make this work. Those tags are in place here. – Jay Blanchard Oct 25 '16 at 18:18
  • While what you're asking is really broad, the simple answer is as I stated before. Make an AJAX request to the PHP script with the query in it. There are many tutorials for doing this, even [with jQuery](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Oct 25 '16 at 18:30

1 Answers1

0

You can use ajax, sending the IP to your controller, that will insert it in your DB as you did till now.

without Jquery : How to make an AJAX call without jQuery?

with Jquery, use $.post() method : https://api.jquery.com/jQuery.post/

Community
  • 1
  • 1
singe batteur
  • 401
  • 2
  • 14
  • 1
    You could. But why do that? What's the point? It's going around in circles to get information that 1. is _unreliable_ as it's user controlled 2. is _redundant_ as the very request you get will also contain it anyway. – VLAZ Oct 25 '16 at 18:04