2

I use a combination of PHP and JS.

I have a URL parameter in my parent URL called market.

http://example.com?market=stackoverflow

I store it to a variable.

<?php 
    $market = $_GET['market'];
?

Then in JS:

<script>
    var market;
    market = <?php echo $market; ?>;
</script> 

Then I have a function - xmlHTTPREQUEST. This is where the problem lies. I need to pass variable market into the function - append it to the script being called - mexx.php

<script>
    function test() {

        var ajax = new XMLHttpRequest();
        ajax.onreadystatechange = function() {
          if (ajax.readyState == 4) {
            xxx = (ajax.responseText);
            console.log(xxx);

          }
        };
        ajax.open("GET", "'mexx.php?market='+market", true);
        ajax.send(null);

    }
</script>

I can't see anything in console - all blank.

Where is the syntax error?

1
  • 1
    It should be ajax.open("GET", "mexx.php?market="+ encodeURIComponent(market), true);, but why do you need to send something that the server already has Commented Feb 29, 2016 at 17:38

1 Answer 1

3

You have extra double quotes " in following line :

ajax.open("GET", "'mexx.php?market='+market", true);
_________________^_________________________^

Should be :

ajax.open("GET", 'mexx.php?market='+market, true);

Also put double qoutes " arround :

market = <?php echo $market; ?>;

Should be :

market = "<?php echo $market; ?>";
_________^______________________^

Hope this helps.

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

1 Comment

Correct. Also, I just found out that I had to put quotes between the php echo. market = "<?php echo $market; ?>"; Your answer and the quotes for the echo solved my issue. Thank you.

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.