开发者

Using .Post() to pass a JQuery Variable to PHP

开发者 https://www.devze.com 2023-03-21 02:55 出处:网络
I am trying create an email subscription input box using JQuery, but for some reason, the inputted email addresses are not inserting into the MySql database,can anyone help me?

I am trying create an email subscription input box using JQuery, but for some reason, the inputted email addresses are not inserting into the MySql database, can anyone help me?

Here is the code I have in my newsletter.js file:

jQuery(function($){

var validateRegexps = {
    email: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
    "default": /.{4}/ 
    }

if ($("#newsletter form").length == 0) return

var defaultMessage = $("#newsletter").attr("data-default")
var subscribedMessage = $("#newsletter").attr("data-subscribed")
var newsletterForm = $("#newsletter form")

 /* newsletter code */
function newsletter(send) {
        var subscribe = true
        newsletterForm.find('input[type="submit"]').val(subscribe ? "Sign Up" : "Unsubscribe")

        var email = newsletterForm.find('input[name="email"]')
        var check = true

        if (email.val() == defaultMessage || email.val() == subscribedMessage) {
            email.val("")
            if (!send) check = false
        } 

        if (check) {
            if(validateRegexps["email"].test(email.val())) {
                    // valid mail
                    email.removeClass("error")
                    if (send) {
                            $.post("php/newsletter.php",{add: email.val()},  function(data) {
                                    email.val(subscribedMessage)
                            });
                            email.val("sending")
                    }

            } else {
                    // invalid mail
                    email.addClass("error")
            }
        }


        return false
}

function restore(e) {
    var jqEl = $(e.currentTarget) 
    if (jqEl.val() == "") {
        jqEl.val(defaultMessage)
    }
    return true
}

function signUp() {
        return newsletter(false)
}

function 开发者_如何转开发signUpAndSend() {
        return newsletter(true)
}

newsletterForm.find('input[name="email"]').focus(signUp).focusout(restore)
newsletterForm.change(signUp)
newsletterForm.submit(signUpAndSend)

});

Here is the code in my newsletter.php file:

require("config.php");

$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);

$subsql = "INSERT INTO subscribe(email) VALUES(".$_POST['add'].");";
mysql_query($subsql);

And here is the code in my other php page to display the input box:

<div id="newsletter" data-default="youremail@yourdomain.com" data-subscribed="Thank you for subscribing.">

<form action="#" method="POST">
<input type="text" name="email" value="youremail@yourdomain.com" />
<input type="submit" value="submit" />
</form>
<h2>Sign Up to Our Newsletter!</h2>

</div>

Any help is greatly appreciated. Thank you very much in advance!!!


Hmm! Looking at your code does not directly point to any errors but the most obvious issue seems to be in the INSERT query that the email address isn't enclosed in single-quotes. Please draft your query like:

$subsql = "INSERT INTO subscribe(email) VALUES('" . $_POST['add'] . "');";

Hopefully the above INSERT should work fine. If the issue still exists though, try finding out if the query might be generating any errors:

$subsql = "INSERT INTO subscribe(email) VALUES('" . $_POST['add'] . "');";
mysql_query($subsql);
if (mysql_error()) {
    echo mysql_error();
}

If no errors are found and the issue still exists, it might make sense to look into other basics, like:

  1. Is $_POST['add'] available in "newsletter.php"?
  2. Is MySQL connection correctly established in "newsletter.php"?
  3. Is there any JavaScript issue that the POST request isn't send at all or perhaps send with an empty email address?

Hope this helps!

0

精彩评论

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