Christian
Christian

Reputation: 111

Javascript - Adding variable to a string of code

I realized how bad I am with javascript :(

I have this:

function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,     
function(m,key,value) {
    vars[key] = value;
});
return vars;
}

var theidis = getUrlVars()["id"];

and

$.tzPOST = function(action,data,callback){
$.post('php/ajax.php?id='+theidis,'&action='+action,data,callback,'json');
}

$.tzGET = function(action,data,callback){
$.get('php/ajax.php?id='+theidis,'&action='+action,data,callback,'json');
}

The first gets the ?id=value parameter properly. I tested with an alert(theidis).

I am looking to do a simple thing - sorry if this is stupid -.- I have worked as this for so long that I am beginning to oversee things.

I want an ?id=value to be added to the php/ajax.php. I tried with the above, but it doesn't seem to work. I also tried by just adding it.

Thank you in advance. :-)

Upvotes: 0

Views: 130

Answers (2)

Mortalus
Mortalus

Reputation: 10712

you probably meant:

$.tzGET = function(action,data,callback){
    $.get('php/ajax.php?id='+theidis + '&action='+action,data,callback,'json');
}

Change the , sign to + sign.

NOTE!: data should be null if you are cocncating string to the URL's query string.

Upvotes: 1

David Hedlund
David Hedlund

Reputation: 129782

Your $.get call specifies

'php/ajax.php?id='+theidis, '&action='+action, data ...

It looks like you're passing data twice. Either &action='+action should be your data parameter, or data. Did you mean to concatenate the &action part into the URL?

Upvotes: 1

Related Questions