user3528944
user3528944

Reputation: 21

Why isn't the "subscribe" button working?

The subscribe button would send the artist and email to the "subscriptions" part of the database. When the button is clicked, nothing happens. I am very new to this and can't seem to figure it out. The following is the .html .php and .js files. (the only thing I took out was the background)

<!DOCTYPE html>
<script>
        $(function() {
                $("#top-section").load("top-section.html", function() {
                    $($(".nav-button").get(2)).attr("id","active");
                    $.getJSON("php/get_artists.php", function(data) {
                         $.each(data, function() {
                            $("#artist-dropdown").append($("<option></option>").val(this.id).html(this.uniqueid));
                        });
                    });
                });
            });
        </script>
        <script src="js/bootstrap.min.js"></script>

        <div id="container">
            <div id="top-section">
            </div>
        </div>

        <div style="margin:0 auto;width:75%;text-align:center">
        <form id="artwork-subscribe" enctype="multipart/form-data">
                        <h3>Subscribe to Artist</h3>
                        <div class="inputs">
                         <label>Artist   </label>
                         <select id="artist-dropdown" form="artwork-subscribe" name="uniqueID"></select><br>
                         <label>Email   </label>  
                         <input id="email" type="text" name="email"><br>
                        </div>
                        <div>
                            <input id="submit-email" type="button" value="Subscribe" >
                        </div>
         </form>
         <label id="artwork-status"></label>
        </div>

    </body>
</html>


$(document).ready(function() {
    $("#artwork-subscribe").on("subscribe", function() {
        var $stat = $("#artist-status");
        var $form = $("#artwork-subscribe");

        if (!isFilled($form)) {
            $stat.text("Failed");
            alert("Must complete all fields.");
            return;
        }
        $stat.text("");
        $.post("php/subscribe_artists.php", $(this).serialize())
        .success(function(data) {
            var response = $.parseJSON(data);
            if (response.stat == "success") {
                $form[0].reset();
                $stat.text("Success!");
                getArtists($("#artist-dropdown"));
            } else if (response.stat == "error") {
                $stat.text("Failed!");
                alert(response.message);
            }
        })
        .fail(function(data) {
            $stat.text("Failed: " + data);
        });
    });


    function isFilled($form) {
        var filled = true;
        $.each($form.serializeArray(), function(i, field) {
            if (field.value.trim() == "") {
                filled = false;
            }
        });

        return filled;
    }

    function getArtists($dropdown) {
        $dropdown.find("option").remove();
        $.getJSON("php/get_artists.php", function(data) {
            $.each(data, function() {
                $dropdown.append($("<option></option>").val(this.id).html(this.uniqueid));
            });
        });
    }
  });

<?php
    require "db_connect.php";

//validates email
    $email = test_input($_POST["email"]);
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
            {
                $emailErr = "Invalid email format"; 
            }
//enter into table
$email = mysqli_real_escape_string($_POST['email']);
$sql = "INSERT INTO subscriptions (email) VALUES ('$email')";
$result = mysql_query($sql);


//select from artist
$artist = mysqli_real_escape_string($con,$_POST["artist"]);
    $artist_name = "";
    $result = mysqli_query($con, "SELECT artist FROM artists where id=".$artist);
    while($row = mysqli_fetch_array($result)) {
        $artist_name = $row['artist'];
    }
?>

Upvotes: 0

Views: 111

Answers (1)

Aage
Aage

Reputation: 6252

Try this:

Change:

$("#artwork-subscribe").on("subscribe"

To:

$("#artwork-subscribe").on("submit"

And This:

<input id="submit-email" type="button" value="Subscribe" >

To:

<input id="submit-email" type="submit" value="Subscribe" >

Upvotes: 1

Related Questions