...

View Full Version : Newsletter Signup submit problems



red71chevelle
04-19-2012, 04:36 AM
Ok Guys I am wanting to code a newsletter signup script. What I have so far is just a Box for them to enter their email and a submit button but the submit button doesn't do anything it just sits there. here is the code on the main page


div class="left">
<label class="grey" for="username">Your Email:</label>
<input class="field" type="text" name="email" value="" id="email" size="23" />

<input type="button" name="submit" onclick="submit_me()" value="GO" />
<script type="text/javascript" charset="utf-8">

function submit_me() {

var email_value = $('#email').val();

$.post("ajax_subscribe.php", { email: email_value }, function(response) {

if (response!='') {alert(response)};
alert('Thank You !');

});

}
</script>
</div>

here is the ajax_subscribe.php code


<?php
$host = "****";
$user = "****";
$password = "****";
$database = "****";
$server = mysql_connect($host, $user, $password);
$connection = mysql_select_db($database, $server);

function sql_quote($value) {
$value = str_replace('<?','',$value);
$value = str_replace('script','',$value);
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
} else {
if ((string)$value[0] == '0') {
$value = "'" . mysql_real_escape_string($value) . "'";
}}
return $value;
}
$q = "INSERT INTO newsletter_emails (email,category) VALUES (".sql_quote($_POST['email']).",'1')";

mysql_query($q);

?>

Any help is greatly appreciated!

angst
04-19-2012, 02:55 PM
well, your using JQuery to submit the data via Ajax, what happens when you click on the button? do you have the jquery api included somewhere in the file?

red71chevelle
04-19-2012, 03:42 PM
well, your using JQuery to submit the data via Ajax, what happens when you click on the button? do you have the jquery api included somewhere in the file?

Thanks for the reply.

When I clik to GO button nothing happens. The page doesnt reload no thank you comes up and nothing gets sent to my database.

I do have jquery already called on the page in the <head>

angst
04-19-2012, 03:46 PM
it's an ajax call, so the page won't reload. did you check the database to see if the email was inserted?

red71chevelle
04-19-2012, 03:54 PM
Yea and nothing is getting placed in it.

angst
04-19-2012, 04:02 PM
have you tried testing your php script outside of the ajax, like a direct call to the page?

it looks like your insert is wrong:


$q = "INSERT INTO newsletter_emails (email,category) VALUES (".sql_quote($_POST['email']).",'1')";


sql_quote($_POST['email']) is text and should be wrapped in single quotes like:


$q = "INSERT INTO newsletter_emails (email,category) VALUES ('".sql_quote($_POST['email'])."','1')";

red71chevelle
04-19-2012, 04:11 PM
I changed that but it still didnt change anything thank you for catching it.

How can I try the direct call? Just type the path to the file in manually in the address bar?

angst
04-19-2012, 04:15 PM
to call it directly, just switch your $_POST to $_GET in your Insert statement and send the url & query string in your browser, like;



http://YourDomain.com/ajax_subscribe.php?email=test@test.com

red71chevelle
04-19-2012, 04:28 PM
Wow... Ok I am sorry for wasting your time. I realized I misspelled the file name on the server it is ajax_subcribe.php all I had to do was rename it and also set this line
$q = "INSERT INTO newsletter_emails (email,category) VALUES (".sql_quote($_POST['email']).",'1')";

Back to the way it was and it all worked fine after that...

If you wouldnt mind continuing to help me with this... do you happen to know a easy way to validate that they enter an actual email address? Right now you can type anything in and it sends it to the database.

angst
04-19-2012, 04:42 PM
no problem, I would use both client side and server side validation;

javascript:


function validate(form_id,email) {

var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.forms[form_id].elements[email].value;
if(reg.test(address) == false) {

alert('Invalid Email Address');
return false;
}
}


and the php way: http://php.net/manual/en/filter.examples.validation.php


if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
echo "This (email_a) email address is considered valid.";
}

red71chevelle
04-19-2012, 04:51 PM
Ok so where I have
<script type="text/javascript" charset="utf-8">

function submit_me() {

var email_value = $('#email').val();

$.post("ajax_subscribe.php", { email: email_value }, function(response) {

if (response!='') {alert(response)};
alert('Thank You For Signing Up!');

});

}
</script>


can I just place
function validate(form_id,email) {

var reg = /^([A-Za-z0-9_-.])+@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/;
var address = document.forms[form_id].elements[email].value;
if(reg.test(address) == false) {

alert('Invalid Email Address');
return false;
}
}


Right after it?

angst
04-19-2012, 04:58 PM
you would need to update that function for your script and add it to your ajax call;



function validate(email) {

var reg = /^([A-Za-z0-9_-.])+@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/;
if(reg.test(email) == false) {
alert('Invalid Email Address');
return false;
}
}




function submit_me() {

var email_value = $('#email').val();
if(validate(email_value) == false){
alert("Invalid email address");
} else {

$.post("ajax_subscribe.php", { email: email_value }, function(response) {

if (response!='') {alert(response)};
alert('Thank You For Signing Up!');

});

}

}

red71chevelle
04-19-2012, 05:07 PM
Ok so this is what I need then?

<div class="left">
<label class="grey" for="username">Your Email:</label>
<input class="field" type="text" name="email" value="" id="email" size="23" />

<input type="button" name="submit" onclick="validate(email); submit_me()" value="GO" />
<script type="text/javascript" charset="utf-8">

function validate(email) {

var reg = /^([A-Za-z0-9_-.])+@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/;
if(reg.test(email) == false) {
alert('Invalid Email Address');
return false;
}
}

function submit_me() {

var email_value = $('#email').val();
if(validate(email_value) == false){
alert("Invalid email address");
} else {

$.post("ajax_subscribe.php", { email: email_value }, function(response) {

if (response!='') {alert(response)};
alert('Thank You For Signing Up!');

});
}
}
</script>
</div>

angst
04-19-2012, 05:10 PM
no, you should execute it like this:

onclick="submit_me();"

I added "validate(email);" to your function "submit_me()", so on submit it will first validate the email address, than if successful it will continue with the insertion.

red71chevelle
04-19-2012, 05:21 PM
Ok sorry I am slow at this.. this is what I have. and now when you select go it just sits there again and doesnt do anything no pop up


<div class="left">
<label class="grey" for="username">Your Email:</label>
<input class="field" type="text" name="email" value="" id="email" size="23" />

<input type="button" name="submit" onclick="submit_me()" value="GO" />
<script type="text/javascript" charset="utf-8">

function submit_me() {

var email_value = $('#email').val();
if(validate(email_value) == false){
alert("Invalid email address");
} else {

$.post("ajax_subscribe.php", { email: email_value }, function(response) {

if (response!='') {alert(response)};
alert('Thank You For Signing Up!');

});
}
}
</script>
</div>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum