PHP & JQUERY

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gotornot
Forum Commoner
Posts: 54
Joined: Fri Jul 31, 2009 2:30 am

PHP & JQUERY

Post 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????
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP & JQUERY

Post 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.
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: PHP & JQUERY

Post 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
Post Reply