-1

I'm writing a simple search engine script using HTML/JS/Ajax/PHP and generating results dynamically from within the PHP script. The search process begins with a JS function called from an onclick() event. I'd like to embed a button into my search results that would call the original JS function, but with a new value to search for. Here's what I mean:

(foo     )  [first search button]

info
more info [new search]

info
more info [new search]

info
more info [new search]

The first line would be a text box where the user searches for 'foo'; then all the other lines (below) would be generated based on that search. I'd like for the user to be able to follow up on particular search results by hitting its respective [new search] button, which would run the original JS function, passing in the 'more info' value as a new search criteria.

This doesn't seem to be working, however. All I'm getting from the JS console when I hit the [new search] button is 'unexpected number' and 'unexpected identifier'. Is what I'm trying to do conceivably possible or does something about ajax content prevent this from working in the first place?

Here's the dynamically generated line that calls the JS function:

print '<input type="button" value="search" onclick="doSearch('. $searchValue . ')">';

I'm not sure what else to share that would be helpful, since there are several hundred lines over two files. If you'd like to see more, please let me know.

user1419715
  • 1,607
  • 2
  • 12
  • 8
  • 11
    oddly enough, our crystal balls are ALL in the shop for repairs, so we can't magically read your mind to see what your code looks like. – Marc B Apr 26 '13 at 20:42
  • 1
    No need to be snarky about it. I'll see what I can do. – user1419715 Apr 26 '13 at 20:45
  • Actually, my crystal ball was repossessed by Jon Skeet ? – adeneo Apr 26 '13 at 20:48
  • 3
    after a few million "my code is broken, but you don't need to see it, just tell me what to fix" posts, snark is the only joy left to us. – Marc B Apr 26 '13 at 20:48
  • I understand your frustration, but sometimes code is ungainly, confidential and/or the question is more conceptual, (e.g. 'can x b done? How would I do that?'). – user1419715 Apr 26 '13 at 20:50
  • 1
    @user1419715 - What you're trying to do seems to be relatively easy, and you're having a specific problem with some dynamically loaded buttons. What's confidendtial or conceptual about that, and how should we know what the problem is without any code, as this is usually pretty straight forward ? – adeneo Apr 26 '13 at 20:54
  • I bet `$searchValue` is a string in which case it needs quotes. Otherwise, the browser will treat it as a variable and the console will output some pretty little error message like `Variable [string] is undefined.`. Try added escaped quotes like so `"doSearch(\'' . $searchValue . '\');"` Make sure that you do not use double quotes (") as these signify the opening and closing of an element's attribute. – War10ck Apr 26 '13 at 21:00
  • no one didn't mention that, but it's a bad practice to have javascript inline of an attribute. in rare cases you may use that to reference a function, but executing a function with arguments within an attribute, will end you up in a mess of php/html/js (and quotation quirks). please read http://stackoverflow.com/questions/5871640/why-is-using-onclick-in-html-a-bad-practice . – metadings Apr 26 '13 at 21:08

1 Answers1

3

You are missing quotes around your string $searchValue:

print '<input type="button" value="search" onclick="doSearch(\''. $searchValue . '\')">';
Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60