Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 02-21-2013, 11:38 PM   #1
jingramm
Senior Member
 
jingramm's Avatar
 
Join Date: Oct 2009
Location: Rhode Island
Posts: 559
Default how to get the code on this button to work?

I saw it on another website and I'd like to implement something similar on mine. It's a simple email subscription form with one button to subscribe. The HTML code has
<button class="btn" style="width:125px; height:35px;" onClick="signup.addEmail()">

Where do I need to go to build the signup.addEmail() code and make that work?
And any thoughts on what that code would look like?
jingramm is offline   Reply With Quote
Old 02-21-2013, 11:44 PM   #2
Markbnj
Moderator
Programming
 
Markbnj's Avatar
 
Join Date: Sep 2005
Posts: 12,887
Default

The 'onClick' is calling a javascript event handler called 'addEmail()' which is declared as a property of an object named 'signup', and that's about all we can tell you from this snippet. The event handler probably posts the user email address to a url for processing on the server. In order to make this work you'd need javascript, a server, some server-side code in php, perl, java, c#, etc., and a database or file system to store the users who sign up.
__________________
Everytime I try to tell you, the words just come out wrong

**
Some meaningless scribbling of no account

The 4th Realm

Arts and Letters Daily - Get some culture
Markbnj is online now   Reply With Quote
Old 02-21-2013, 11:52 PM   #3
jingramm
Senior Member
 
jingramm's Avatar
 
Join Date: Oct 2009
Location: Rhode Island
Posts: 559
Default

Edit:

I figured it out. I didn't use javascript or anything, but I used a simpler PHP version to get the function working. Thanks!

Last edited by jingramm; 02-22-2013 at 03:02 AM.
jingramm is offline   Reply With Quote
Old 02-22-2013, 03:17 AM   #4
tech7
Junior Member
 
Join Date: Feb 2013
Posts: 7
Default

A simple implementation would be:

HTML Code:
<button class="btn" style="width:125px; height:35px;" onClick="signup.addEmail()" id="emailAddress"> 

<script>
var signup = {
    addEmail : function() {
        window.location = "addEmail.php?email=" + encodeURIComponent(document.getElementById("emailAddress").value);
    }
};
</script>
This would redirect to addEmail.php with the querystring variable "email".
tech7 is offline   Reply With Quote
Old 02-22-2013, 03:54 AM   #5
jingramm
Senior Member
 
jingramm's Avatar
 
Join Date: Oct 2009
Location: Rhode Island
Posts: 559
Default

Quote:
Originally Posted by tech7 View Post
A simple implementation would be:

HTML Code:
<button class="btn" style="width:125px; height:35px;" onClick="signup.addEmail()" id="emailAddress"> 

<script>
var signup = {
    addEmail : function() {
        window.location = "addEmail.php?email=" + encodeURIComponent(document.getElementById("emailAddress").value);
    }
};
</script>
This would redirect to addEmail.php with the querystring variable "email".
Is it possible to get each email address emailed to me instead of requiring me to go into addEmail.php file and also have the submit button change text to "Thank you" right after submitting without going to another webpage?

Just wondering, because if not I will just keep what I implemented using PHP
jingramm is offline   Reply With Quote
Old 02-22-2013, 11:33 AM   #6
tech7
Junior Member
 
Join Date: Feb 2013
Posts: 7
Default

Yes it's possible but you need to use a mix of Javascript and PHP to do that, specifically, PHP has to handle the email sending (can't be done with JS alone).

You may have heard of AJAX. Simply put AJAX allows you to call PHP scripts (or other server side scripts) behind the scenes without making the user change page. AJAX will allow you to do what said, the process would be something like:

  • On button click: grab the email in the textbox, fire off an AJAX request (behind the scenes) to a PHP script including the email address
  • The PHP script sends the email to yourself saying that xyz has registered their email
  • The Javascript updates the button text to "Thank you". You could also disable the button at this point to help prevent duplicate submissions.
Let me know if you want any help with the coding.
tech7 is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:17 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.