0

I am trying to access php variable inside jquery selector but it can't get to work. This php variable is taken from the views page from a foreach php statement. Check the code below.

HTML:

<?php foreach($items as $key => $value):?>
 <div id="uploader<?php $value['id'] ?>">Upload</div>
<?php endforeach?>

The above code works concat string.

jQuery:

$(document).ready(function($) {
  $("#uploader<?php echo $value['id'] ?>").uploadFile({
  url:"YOUR_FILE_UPLOAD_URL",
  fileName:"myfile"
  });
});

Now I wanted to concat the php variable inside the jquery element selector but code above won't work. What would be the best thing to do here? Thanks

1
  • PHP is a server side technology, so once the html is created, php in effect does not exist. There is no php in the browser. You need to write any code on the browser side using Javascript ( jQuery is a javascript library). jQuery and underscore and lowdash have relatively easy to implement templating systems for generating html Commented May 5, 2016 at 5:04

2 Answers 2

1

Try the following answer without php, select all the elements that have a id that starts with uploader

$(document).ready(function($) {
  $('div[id^="uploader"]').uploadFile({
  url:"YOUR_FILE_UPLOAD_URL",
  fileName:"myfile"
  });
});

or safer use a class

<?php foreach($items as $key => $value):?>
 <div class="toupload" id="uploader<?php $value['id'] ?>">Upload</div>
<?php endforeach?>

js:

$(document).ready(function($) {
      $('.toupload').uploadFile({
      url:"YOUR_FILE_UPLOAD_URL",
      fileName:"myfile"
      });
    });
Sign up to request clarification or add additional context in comments.

5 Comments

select all the elements that have a id that starts with uploader
is OP mean about all item or specific item??
This is awesome. It loops automatically. But still have an issue. I'll fix this later.
@claudios glad to know, don't forget to accept the answer :P
This works fine but only selects the last item at the end of the loop
1

You can also use ~ for selecting any id have the value as uploader.

$(document).ready(function($) {

    $('div[id~="uploader"]').uploadFile({
        url:"YOUR_FILE_UPLOAD_URL",
        fileName:"myfile"
    });
});

Ref:

[attribute^=value]  $("[title^='Tom']") All elements with a title attribute value starting with "Tom"
[attribute~=value]   $("[title~='hello']")  All elements with a title attribute value containing the specific word "hello"
[attribute*=value]  $("[title*='hello']")   All elements with a title attribute value containing the word "hello"

5 Comments

$value['id'] will only refer to the last value fetched after the php FOREACH is done...
Yep this is helpful answer but not working for me. Maybe it's somethin to do with the loop
@Webomatik, yes. I am using loop here so I wanted to loop also the selector with id concat
@claudios see my answer, you don't need any loop
right, so you need to do the same thing in another way.

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.