0

So I am trying to do a post using jQuery.ajax instead of an html form. Here is my code:

$.ajax({

type: 'POST', // GET is available if we prefer
url: '/groups/dissolve/$org_ID',
data: data,
success: function(data){
    $('#data_box').html(data);
    }
});

Here is my problem:

When this was in an html form, the $org_ID that was part of the URL would actually pull the variable and send it as part of the URL. Now that this is in jquery, its just sending $org_ID as text. How can I get this to figure out what the variable, $org_ID is? I tried declaring it in the javascript but I am brand new to jquery/javascript and don't really know what i'm doing.

Thanks!

1
  • how was "it part of the html form" was it a hidden field? Commented Aug 10, 2012 at 17:42

3 Answers 3

2

Are you rendering this in PHP? In that case you need to do:

url: '/groups/dissolve/<?php print $org_ID; ?>'

Otherwise, you need to do something like

var org_id = 'foo'; 
// or
var org_id = '<?php print $org_id ?>';
$.ajax({

type: 'POST', // GET is available if we prefer
url: '/groups/dissolve/'+org_ID,
data: data,
success: function(data){
    $('#data_box').html(data);
    }
});

Unlike PHP, you can't interpolate variables in javascript, you have to concatenate them with the string.

If you're trying to POST a variable (org_id) then you should put it in data:

data['org_id'] = org_id;

$.ajax({

type: 'POST', // GET is available if we prefer
url: '/groups/dissolve/',
data: data,
success: function(data){
    $('#data_box').html(data);
    }
});

While you can concatenate params onto your url to send them in an HTTP request, putting them in a data object not only lets jQuery do more work for you & escape HTML entities etc (and keep your code cleaner), but also allows you to easily debug and play around with ajax() settings.

Sign up to request clarification or add additional context in comments.

6 Comments

Also it assumes that this javascript is defined in a PHP page.
@bokonic, appending +org_ID does seem to append the variable to the URL so that takes me 1 step closer. However, $org_ID is a variable defined in PHP. How do I get the org_ID variable which I now have to define in javascript to equal the PHP variable?
just do var org_id = '<?php print $org_id; ?>';, assuming you generate the JS in php. If not, you'll need to make an ajax call to a php page that returns the variable/some JSON...
var org_id = '<?php echo $org_id; ?>'; does not work but if i just set a static value like var org_id = 7;, it works fine. Does not seem to like the PHP code in there.
@Progger you're probably not passing the page with this JS through PHP; if you view source on the file is the <?php print $org_id ?> there? or is it blank (where there would be the <?... statement) -- if it's blank, then the variable isn't set in PHP. If it's the <?php.. statement, then you have problems w/ your server or you aren't running this through a server running php (likely apache)
|
0

It's not clear in the question where your data comes from, but you can use something like:

url: '/groups/dissolve/'+orgId,

or:

url: '/groups/dissolve/?orgId='+orgId,

Comments

0

Short answer, concatinate

url: '/groups/dissolve/' + $org_ID

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.