AKor
AKor

Reputation: 8882

jQuery + onClick Problem when using PHP

I had a line of code just like this:

<a onClick="someFunc();">Click</a>

It worked fine. But I tried to output it in PHP as such:

echo "<h3>" . $zone . " <a href='javascript:$('#zoneNotifUnsub').submit()'>Unsubscribe</a></h3>";

And it doesn't work. Using onClick or href makes no difference, the result is that the code doesn't work. It's clearly some issue of how PHP outputs <a> elements. Any help?

Upvotes: 0

Views: 428

Answers (2)

Hamish
Hamish

Reputation: 23316

There is nothing wrong with PHP, you've just included a syntax error in the output, see:

"<a href='javascript:$('#zoneNotifUnsub').submit()'>"

Note the apostraphes?

You'll need to escape them, ie:

"<a href='javascript:$(\'#zoneNotifUnsub\').submit()'>"

Or

 "<a href='javascript:$(\"#zoneNotifUnsub\").submit()'>"

Upvotes: 1

alex
alex

Reputation: 490143

PHP outputs a like any other element - as the HTML serialised in a string, i.e. it doesn't output it any special way.

Since you are using jQuery, your best bet is probably...

$('h3 a').click(function() { $('#zoneNotifUnsub').submit(); });

Upvotes: 0

Related Questions