0

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?

2
  • first thing separate the server side code and uI code ,all click handles should be handled at uI level, this is very old way of doing it. Commented Feb 28, 2011 at 0:18
  • give a class name to the href and handle the click event in the jquery.., this way it will be very cleaner Commented Feb 28, 2011 at 0:19

2 Answers 2

1

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()'>"
Sign up to request clarification or add additional context in comments.

1 Comment

I think "<a href='javascript:$(\'#zoneNotifUnsub\').submit()'>" will still generate an error, as the escapes are in the PHP string, not the HTML string. You would have to do: "<a href='javascript:$(\\'#zoneNotifUnsub\\').submit()'>"
0

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(); });

Comments

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.