开发者

"Was this helpful?" button

开发者 https://www.devze.com 2023-03-05 11:05 出处:网络
I\'m wanting to create a \"Was this helpful?\" button, just with the options Yes/No. I don\'t h开发者_C百科ave a database ready to receive the input, so, I\'d just like each button to simply send an e

I'm wanting to create a "Was this helpful?" button, just with the options Yes/No. I don't h开发者_C百科ave a database ready to receive the input, so, I'd just like each button to simply send an email to me if it was clicked. Maybe the email could just say, "a site user found this page helpful/unhelpful: [URL]".

I've coded email feedback forms in PHP before but can't think of where I'd start with this. I'm not sure if an email can be sent with just one click of a javascript button. Does anyone see this as a possibility?


You could use a library such as jquery to do an ajax post to a proxy PHP script:

$.post("/likedthis.php", { 'page': window.location.href},
   function(data) {
     // Deal with the post results here if you want
     alert("Data Loaded: " + data);
});

Though one issue you will run into is people refreshing and clicking multiple times, potentially spamming you with email. A good basic protection against this is to set a $_SESSION variable or cookie with their IP address, and the page, so they can't keep clicking again and again.

Please note however that they can simply use another IP address, or say another page is helpful. However that would be a lot of work just for that.

Also for sanity purposes it would be a good idea to also check the page variable against parse_url() to be safe.


You cannot directly send an email through JavaScript. Here's one way you could do it:

  • Using JavaScript, add a click event listener to the button, which, when fired:
    • Disables the button, then
    • Sends an ajax request to the server, instructing the server to send the email

Using a cross-browser library will make writing the JS part of this simpler:

  • What cross-browser JavaScript libraries exist?


You wouldn't want to send an email in Javascript, even if you could. You'd need access to an SMTP server and you'd have to pass your credentials to that server. By using JS, your entire code will be client side and so your credentials would be easily obtainable. Are you able to use a server-side language?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号