Reputation: 1925
I'm writing a system in HTML5 and Javascript, using a webservice to get data in database.
I have just one page, the index.html, the other pages i load in a <div>
The thing is, i have one page to edit and add new users.
When a load this page for add new user, i do this:
$("#box-content").load("views/motorista_add.html");
But, i want send a parameter or something else, to tell to 'motorista_add.html' load data from webservice to edit an user. I've tried this:
$("#box-content").load("views/motorista_add.html?id=1");
And i try to get using this:
function getUrlVar(key) {
var re = new RegExp('(?:\\?|&)' + key + '=(.*?)(?=&|$)', 'gi');
var r = [], m;
while ((m = re.exec(document.location.search)) != null)
r.push(m[1]);
return r;
}
But don't work.
Have i an way to do this without use PHP?
Upvotes: 0
Views: 80
Reputation: 1430
This won't work. Suppose your are loading the motorista_add.html
in a page index.html
. Then the JS code, the function getUrlVar()
, will execute on the page index.html
. So document.location
that the function will get won't be motorista_add.html
but index.html
.
So Yes. To do the stuff you are intending, you need server side language, like PHP. Now, on the server side, you get the id
parameter via GET
variable and use it to build up your motorista_add.php
.
Upvotes: 2
Reputation: 2511
You can pass data this way:
$("#box-content").load("views/motorista_add.html", { id : 1 });
Note: The POST method is used if data is provided as an object (like the example above); otherwise, GET is assumed. More info: https://api.jquery.com/load/
Upvotes: 0