2

I'm trying to make the URL in the following example contain a php variable like this:

http://api.crunchbase.com/v/1/company/$company.js

This is my jquery code:

$.ajax({
url: "http://api.crunchbase.com/v/1/company/airbnb.js",
dataType: 'jsonp',
success: function(results){
    var number_of_employees = results.number_of_employees;
        }

What do I need to do to achieve this?

Thanks!

2
  • What is the exact problem you are facing? Commented Jul 15, 2011 at 8:24
  • you can refer this question also-stackoverflow.com/questions/1616231/… Commented Jul 15, 2011 at 8:26

3 Answers 3

3

You'll need to output the variable as JavaScript from PHP at some point in the script. This can be done quite easily using json_encode:

var company = <?php echo json_encode($company) ?>;

Note that this will have to be placed in a file that is actually executed by PHP, rather than an inert .js file. One way to achieve this is to put it in an inline <script> in the HTML in your PHP template; e.g.

<script type="text/javascript">
    var company = <?php echo json_encode($company) ?>;
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

<script type="text/javascript">
    var company = <?php echo json_encode($company); ?>;
    alert(company);
</script>

You're setting the company variable with the output of $company, you can then use this variable however you please in JavaScript.

$.ajax({
url: "http://api.crunchbase.com/v/1/company/" + company + ".js",
dataType: 'jsonp',
success: function(results){
    var number_of_employees = results.number_of_employees;
}

1 Comment

Parentheses missing and a string containing " or `` at the end will break things...
0

Can you try this, assuming you have the company name in the php $company variable:

$.ajax({
url: "http://api.crunchbase.com/v/1/company/<?php echo $company ?>.js",
dataType: 'jsonp',
success: function(results){
    var number_of_employees = results.number_of_employees;
}

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.