Submit user location in a form - javascript

I'm trying to figure out how to get the location of someone filling out a web form on my site. I just want to get it down to a state level for US and Country for international(although state or equivalent is fine international too). I just don't want the user to have to select from a super long list or fill out a field. So ideally something like a hidden field that could have javascript or php pass the info to it.
Any ideas?

You can try; http://www.w3schools.com/html/html5_geolocation.asp
And using Google API:
http://maps.googleapis.com/maps/api/staticmap?zoom=10&size=400x400&maptype=roadmap&markers=color:red%7Ccolor:red%7Clabel:C%7Cxx.xxxxxxx,xx.xxxxxxx&sensor=false
Using PHP or Javascript to replace the "xx.xxxxxxx"'s will give you a map of the users location.
You can probably use Google API to determine the users State, etc - as you mentioned. But I don't think you will be baby fed on this one.

You could try to figure out their location using their IP address. However, this is far from reliable. Unfortunately, if you need this data to be accurate, you're going to have to get them to give you their location.

Related

How do I get a value from html and post it?

im a begginer web developer and i want to make something like that:
you login with username end password, then it redirects you to the home page, and says something like this:
Hello (username) welcome!
but i dont know how to read the value from the input please help
i searched it up but it only showed a search input option
This is too generalized for such a question. Many questions: -Are you using any backend? If so which one (what language? And what kind of database (ie SQL etc.))?
To store data you need a backend or at least use frontend js's localStore (would work if every page is on the same domain and server).
Yet another front-end way would be to pass the information as url-parameters and get them.
But if you really want to build a robust login system of any kind, the best way is to use backend (PHP/node JS/Java/python etc.) and store the data in a database (SQL etc.). And make sure it is secure, but security is too broad of a topic for this question.
Your asked very generalized question, for my assumption there you want to do either one of these
when you login, you want to keep username from input and and redirect the user to home page and show the username on the label like hello abcd
You have an input field with username on home page and want to show the text of the input field into label
so, I will give answer considering the above scenarios, the answer will be considering how I will do things(as I don't know what you are using in backend the answer may vary)
for point 1 I will store the username in a localstorage, sessionstorage or cookie.
let's say I used sessionstorage, the below code will be performed on my login button click
sessionStorage.setItem('quantity',document.getElementById('username').value);
and on my pageLoad of home page I will set the value in my label like below
document.getElementById('lblusername').text = sessionStorage.getItem('quantity');
option 2,
on my home Page I will simply get the value from input field and show it on label
document.getElementById('lblusername').text = document.getElementById('username').value;
let me know if it helped you as I don't know your exact scenario

Fetching all values from a webpage along with user inputs

We have key-value pair contents on some of our web pages, and I need to get all of these for logging purposes. There are forms on some of these pages for the user to fill in, so i need to get their values too. It is clear that I need to scan and try to obtain these values when the user clicks submit button, to get the inputs' values too.
1) What i try to get from the page looks like for example Customer No: 12345
2) And there may be some forms on the page like Customer Name: ___________ I need to get the value that user provides into this inputs too.
3) Pages are always displayed in Internet Explorer, so I can prepare an activex, another process that uses shdocvw, or I may inject some javascript to fetch these values(at least these are coming to my mind)
4) After all of these, I need to have all values on a page in a form like {CustomerNo:12345, CustomerName: abcdef}
I need to prepare something generic, so that it can work on every page of ours. Is there any way to achieve this? Is there any third party software that converts all of a web page(along with user inputs) into a flat XML for example?
Thanks

How can I make a search box to search in a local database?

I'm playing around with Apache Cordova and I want to implement a search box that can search in a list or something similar (local database?). I want it to be local so that the app does not require an internet connection to search.
The way I want it to be set up is that it need to be able to search for both the name of the item and a number.
Example 1:
A user searches for: "Honda". He will then be sent to a page where information about "Honda" will be diplayed.
Example 2:
A user searches for the id: "1337". He will then be sent to the page containing information about "Honda".
So "Honda = 1337" and "1337 = Honda" if you guys understand?
The question:
I'm wondering what solution would suite my needs for this project?
or there any frameworks out there for this?
Thanks in advance!
It depends on what you want to have the search box and submit to be like.
One way is to use this really great tool http://ivaynberg.github.io/select2/ it can load data from database (using either ajax or load all of your data and only filter them) and shows them in selectbox. You can have submit button or something like that next to your selectbox.
Other way might be to use http://www.datatables.net/ it has search box, which can load data to table using ajax where you can have additional information and submit button for sending user to specific page.
There is plenty of other tools for this kind of problem, I used these two and I can say

Is there any way to access browser form field suggestions from JavaScript?

Is there any way to access the autocomplete suggestions that appear under HTML input fields in some browsers (representing previously submitted data)? Is this only available to the browser?
I ask as I want to make my own autocomplete implementation in javascript, but I want to intermingle my own suggestions with the users previous searches. A bit like how youtube does (but youtube stores all the data obviously, and it is tied to a login, there are no accounts on my website and never will be).
I was wondering more if there was a way to do it with the data stored in the users browser rather than storing all the data on my server. Is there is a way to grab the data the browser uses to present previous input to a user?
Is the data that appears in html input fields representing previously submitted data only available to the browser?
Yes - until it appears in the DOM.
Is there is a way to grab the data the browser uses to present previous input to a user?
It's a browser-specific feature, and you can't access the data [history] directly (Where do browsers save/store auto fill data). You only can disable storing anything.
I ask as I want to make my own autocomplete implementation in javascript, but I want to intermingle my own suggestions with the users previous searches. I was wondering more if there was a way to do it with the data stored in the users browser rather than storing all the data on my server.
Especially if you want to utilize all previous searches, the browser's autofill doesn't help you anyway. But yes, you can store them in the browser (on the client side) manually: Use DOM Storage, like localStorage. Though I would recommend sessionStorage only, you might run into privacy issues otherwise if everybody using a browser could see the search terms of previous users…
You can use jstorage. Jstorage lets you store up to 5Mb of data on the client side.
<script src="//cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
<script src="https://raw.github.com/andris9/jStorage/master/jstorage.js"></script>
<script>
/* $.jStorage is now available */
// store some data
$.jStorage.set('yourkey', 'whatever value');
// get the data back
value = $.jStorage.get('yourkey');
</script>
The only way i see this working is with help of localStorage (html5) problem that it doesn't work in ie<8
Here's an example: http://jsfiddle.net/8NZY7/

Checking if the current user liked an URL using the built-in like action (og.likes)

My application implements a custom like button using Facebooks offical built-in like action.
The only missing piece is to disable the button if the user has already liked the resource. After testing various approaches using FQL which were all unsuccessful, I've just tried to issue a get request against https://graph.facebook.com/<user_id>/og.likes which lo and behold returns the Open Graph likes of the current user.
Unfortunately I cannot seem to find a way to filter the result for a specific object_id. Can anyone recommend a solution or knows a way to accomplish the same using FQL? Be advised that we are talking Open Graph Likes here which seem to differ from the standard likes created using the Facebook Like button.
I've also been struggling to know how to get information about these og.likes using the graph API or FQL. They don't seem to be stored in any of the usual tables (url_like, or link_stat). This may not be the "right" way to do it but here's how I do it.
In order to check if the user has liked the object, I am hitting the Graph API as you mention, with the URL graph.facebook.com/[userid]/og.likes. Then I just loop through the array in the response, and look for one that matches the URL or ID of the object that I'm interested in. It's not particularly efficient if the user has a lot of og.likes, but it gets the job done.
I am not sure whether API is available or not.
But, you have workaround. As you know the resource that is being viewed, you know the user id, when ever action gets posted, store that in your db and use that info to disable like.
I will keep you updated, once I find more info.
EDIT
An FQL table containing the Open Graph URLs that a user has Liked.
To read the url_like table you need
user_likes permissions for all Open Graph URLs liked by the current
session user friend_likes permissions for all Open Graph URLs like by
friends of the current session user
source: user likes
Looks like you can set a Like button for your URL then check if a user liked it by ANDing "url=" in the WHERE clause:
https://api.facebook.com/method/fql.query?query=SELECT url FROM url_like WHERE user_id = me() and url="http://www.yourUrl.com"&access_token=...
Note that the url has to be in "quotes"

Categories

Resources