Page 1 of 1
PHP & JQUERY
Posted: Wed May 16, 2012 5:54 pm
by gotornot
Hi All (Again)
I am looking to pick up variables in jquery from a string created:
function search_by() {
var search_term = $('#search_for', $(this).attr('value'));
alert(search_term);
}
I cant seem to get the value i am trying to into a "var".
This is the code on the page i am using a onClick="search_by();"
i.e:
<a class="show_cats" search_cat="PDAs & Accessories" search_for="fire" onclick="search_by();" href="#">
I am trying to get the value inside the "search_for" field.
Any ideas????
Re: PHP & JQUERY
Posted: Wed May 16, 2012 9:45 pm
by califdon
You can't use Javascript syntax (jQuery is a Javascript library) in PHP. PHP is running in the server and has no contact with Javascript running in the browser. What you can do is send the Javascript, just like HTML, to the browser to be used after it receives the web page. To do that, you can use the echo command.
Re: PHP & JQUERY
Posted: Thu May 17, 2012 4:16 am
by Grizzzzzzzzzz
gotornot wrote:Hi All (Again)
I am looking to pick up variables in jquery from a string created:
function search_by() {
var search_term = $('#search_for', $(this).attr('value'));
alert(search_term);
}
I cant seem to get the value i am trying to into a "var".
This is the code on the page i am using a onClick="search_by();"
i.e:
<a class="show_cats" search_cat="PDAs & Accessories" search_for="fire" onclick="search_by();" href="#">
I am trying to get the value inside the "search_for" field.
Any ideas????
well for starters $('#search_for'); doens't refer to anything, the # selector is used for ids of elements, you don't have an element with an ID of 'search_for'
$(this).attr('value'), where has 'this' come from. To be able to use 'this' like what i think you're doing, you'd have to have
Code: Select all
$('.show_cats').click(function(){
alert($(this).attr('search_for'));
});
Getting the attribute value (a better way would be to just do .val()) only works on attributes that actually have a value assigned to them. An anchor tag doesn't have one by default, and you're not accessing the 'value' attribute, you're trying to access your own 'search_for' attr
I imagine your code should look like
Code: Select all
function search_by() {
var search_term = $('.show_cats').attr('search_for');
alert(search_term);
}
I don't really understand what you're trying to accomplish with php here from what you've written, but before you attempt it i'd recommend going and reading up on jQuery selectors & .attr() and .val(), as it seems you have abit of a messy understanding of them and it's going to cause you issues