Thank you! Your feedback has been delivered
Thank you! Your feedback has been sent

Fix my PHP Email Sign-up Form

Hello,

I'm a front-end developer, and have kept myself from learning the least bit of PHP to my own agony.

I've just created a minimal 'Coming Soon' HTML template. It uses shine.js, it's my first time with this JS library, and there are bugs throughout my project. However, the one issue I'm dealing with is successfully integrated email sign-up on my index.html page. I've tried and failed to interface a PHP document that I've found success with before.

In short, I simply need appropriate PHP in the just-mentioned, short file as well as the success in having the Subscribe form work functionally.

This is my first time on this website, but I will be as clear and available as I'm able. Help is extremely appreciated.

User Gravatar

dckallos

Posted Jun 1 2014 6:03 UTC

$40


  • Assigned To gridsane
  • Solved
  • php
    subscription
    email form
  • 567 Views

6 Replies


My apologies for making a mistake already! My project is being hosting on an IDE. Here is the link to the project: https://codio.com/dkallos/shine-js

It can be exported easily as a zip via Project >> Export as zip, although the two files in question are just index.html and subscribe.php.

User Gravatar

dckallos

Posted Jun 1 2014 6:09 UTC

Sample JS for form:

var $form = $(".subscribe form"),
    $success = $(".subscribe .done"),
    $loading = $("#loading");

$form.on('submit', function (e) {
    e.preventDefault();

    $form.hide();
    $loading.show();

    $.post(
        $form.attr('action'),
        {email: $("#email").val()},
        function (data) {
            if (data.success) {
                $success.show();
            } else {
                alert('Please, enter a valid email.');
            }
        }
    )
    .error(function () {
        $form.show();
        alert('Server error occured. Please, try again later.');
    })
    .always(function () {
        $loading.hide();
    });
});

subscribe.php contents:

<?php

header('Content-Type: application/json');

$email = trim(array_key_exists('email', $_POST) ? $_POST['email'] : '');

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    file_put_contents('subscribers.txt', $email . "n", FILE_APPEND);
    echo json_encode(array('success' => true));
} else {
    echo json_encode(array('success' => false));
}
User Gravatar

gridsane

Posted Jun 1 2014 6:42 UTC

I truly have to apologize, but I'm having trouble knowing where to insert both of the snippets. Could you assist with the location to place this code within the mentioned files?

Much appreciation for your help!

User Gravatar

dckallos

Posted Jun 1 2014 7:10 UTC

See my fork

index.html including jquery at line 8 and my snippet from line 33 to line 63

subscribers.php whole file

Saved emails located in the subscribers.txt

Notes:

  • For php to work you need to install php5 and apache2. Just a couple of commands. (Open "Tools > Terminal" and run parts install php5 php5-apache2 && parts restart apache2).

  • The .htaccess file used to deny 'subscribers' from direct access.

You can test my fork here.

User Gravatar

gridsane

Posted Jun 1 2014 7:35 UTC

Solution

This didn't solve your task? Get your own custom solution.

I'd found myself still awake this morning because of this one issue I couldn't move past for several hours, so I really have to thank you for working with me on this.

Everything is working beautifully!

Cheers - Dan

User Gravatar

dckallos

Posted Jun 1 2014 8:13 UTC

Thanks for the reward! Good luck with your project =)

User Gravatar

gridsane

Posted Jun 1 2014 19:57 UTC

Add a reply

By posting a reply on CodersClan you agree to our Terms & Conditions