1

I am experiencing some problems with having the Ajax request function/method in JQuery recognizing a PHP-variable from outside the script code. What I am trying to do is using the variable $live_update_url as the url-argument in the Ajax code. The code below is not working, but if I hard code the value of the url there are no problems. So it should be the variable itself that is not accessed. What am I doing wrong here?

function ajaxd() {
 $.ajax({
 url: <?php print($live_update_url);?>,
 type: "get",
 data: {live_time: 'value'}, 
 dataType: "json",
 success: function(data){
 $('#local_time').html(data.live_time);
 }
 });
asked Feb 28, 2016 at 6:31
2
  • 1
    do this url: "<?php echo $live_update_url;?>" . " is neccesery Commented Feb 28, 2016 at 6:32
  • Just tried the echo function too and it's not working either for some reason Commented Feb 28, 2016 at 6:59

2 Answers 2

1

It looks like you are missing the quotes around the URL value in your JSON.

Make sure that the value returned by the $live_update_url includes quotes or try this:

function ajaxd() {
 $.ajax({
 url: "<?php print($live_update_url);?>",
 type: "get",
 data: {live_time: 'value'}, 
 dataType: "json",
 success: function(data){
 $('#local_time').html(data.live_time);
 }
 });
answered Feb 28, 2016 at 6:37
Sign up to request clarification or add additional context in comments.

1 Comment

You're right @Tim, the quotes marks are missing but even with them there it doesn't work..
0

Found a solution:

Used the following definition of $live_update_url in the PHP code:

$live_update_url = "'http://localhost/projectName/api/time.php?g_id=".$g_id."'";

This string already includes single quotes within the double quotes. Then I used the echo <<<_END-construct and just added the following line in the Ajax request:

url: $live_update_url

answered Feb 28, 2016 at 7:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.