Leesome
09-25-2011, 12:44 PM
Hi all,
I'm having some issues with getting a html form to send data through to my MYSQL database. JS is the one part of the script that I'm least sure about, so I assume the issue lies there...
All the data from the text fields in the form go through to the MySQL table fine, but the data from the drop down box and checkbox doesn't make it...
So, the html dropdown and checkbox code from the form is:
<label for="lettertype" id="lettertype_label">I want my letter to be: </label><br>
<select id="lettertype" name="lettertype">
<option value="0"></option>
<option value="public">Public, though Anonymous</option>
<option value="private">Private</option>
</select><br>
<label class="error" for="lettertype" id="lettertype_error">Would you like your letter to be public or private?</label> <br>
<label for="terms" id="terms_label">I understand thde terms and agreements...</label><br>
<input type="checkbox" name="terms" value="Yes" /><br>
<label class="error" for="terms" id="terms_error">We know no one ever reads these things, but we need to you at least pretend...</label>
When submit is clicked, it calls the following javascript:
$(function() {
$('.error').hide();
$('input.text-input').css({backgroundColor:"#F6F6F6"});
$('input.text-input').focus(function(){
});
$('input.text-input').blur(function(){
$(this).css({backgroundColor:"#F6F6F6"});
});
$(".button").click(function() {
// validate and process form
// first hide any error messages
$('.error').hide();
var letter = $("textarea#letter").val();
if (letter == "") {
$("label#letter_error").show();
$("textarea#letter").focus();
return false;
}
var firstname = $("input#firstname").val();
if (firstname == "") {
$("label#firstname_error").show();
$("input#firstname").focus();
return false;
}
var surname = $("input#surname").val();
if (surname == "") {
$("label#surname_error").show();
$("input#surname").focus();
return false;
}
var email = $("input#email").val();
if (email == "") {
$("label#email_error").show();
$("input#email").focus();
return false;
}
var lettertype = $("select#lettertype").val();
if (lettertype == "0") {
$("label#lettertype_error").show();
$("select#lettertype").focus();
return false;
}
var terms = $("input#terms").val();
if (terms == "") {
$("label#terms_error").show();
$("input#terms").focus();
return false;
}
var dataString = '&letter=' + letter + '&firstname='+ firstname + '&surname=' + surname + '&email=' + email + '&lettertype' + lettertype + '&terms' + terms;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "../php/emailform.php",
data: dataString,
success: function() {
$('#replace').html("<div id='message'></div>");
$('#message').html("<br>")
.append("")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='mailconfirm' src='images/mailconfirm.png' /><br><br>");
});
}
});
return false;
});
});
runOnLoad(function(){
$("input#firstname").select().focus();
});
Then in terms of the PHP script, this is the excerpt:
$letter = $_POST['letter'];
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$lettertype = $_POST['lettertype'];
$terms = $_POST['terms'];
$timestamp = date( 'Y-m-d');
$sql = "INSERT INTO ToBeEmailed (letter, firstname, surname, email, lettertype, terms, timestamp) VALUES ('$letter', '$firstname', '$surname', '$email', '$lettertype', '$terms', '$timestamp')";
If anyone could offer any advice that'd be great! Cheers in advance...:)
I'm having some issues with getting a html form to send data through to my MYSQL database. JS is the one part of the script that I'm least sure about, so I assume the issue lies there...
All the data from the text fields in the form go through to the MySQL table fine, but the data from the drop down box and checkbox doesn't make it...
So, the html dropdown and checkbox code from the form is:
<label for="lettertype" id="lettertype_label">I want my letter to be: </label><br>
<select id="lettertype" name="lettertype">
<option value="0"></option>
<option value="public">Public, though Anonymous</option>
<option value="private">Private</option>
</select><br>
<label class="error" for="lettertype" id="lettertype_error">Would you like your letter to be public or private?</label> <br>
<label for="terms" id="terms_label">I understand thde terms and agreements...</label><br>
<input type="checkbox" name="terms" value="Yes" /><br>
<label class="error" for="terms" id="terms_error">We know no one ever reads these things, but we need to you at least pretend...</label>
When submit is clicked, it calls the following javascript:
$(function() {
$('.error').hide();
$('input.text-input').css({backgroundColor:"#F6F6F6"});
$('input.text-input').focus(function(){
});
$('input.text-input').blur(function(){
$(this).css({backgroundColor:"#F6F6F6"});
});
$(".button").click(function() {
// validate and process form
// first hide any error messages
$('.error').hide();
var letter = $("textarea#letter").val();
if (letter == "") {
$("label#letter_error").show();
$("textarea#letter").focus();
return false;
}
var firstname = $("input#firstname").val();
if (firstname == "") {
$("label#firstname_error").show();
$("input#firstname").focus();
return false;
}
var surname = $("input#surname").val();
if (surname == "") {
$("label#surname_error").show();
$("input#surname").focus();
return false;
}
var email = $("input#email").val();
if (email == "") {
$("label#email_error").show();
$("input#email").focus();
return false;
}
var lettertype = $("select#lettertype").val();
if (lettertype == "0") {
$("label#lettertype_error").show();
$("select#lettertype").focus();
return false;
}
var terms = $("input#terms").val();
if (terms == "") {
$("label#terms_error").show();
$("input#terms").focus();
return false;
}
var dataString = '&letter=' + letter + '&firstname='+ firstname + '&surname=' + surname + '&email=' + email + '&lettertype' + lettertype + '&terms' + terms;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "../php/emailform.php",
data: dataString,
success: function() {
$('#replace').html("<div id='message'></div>");
$('#message').html("<br>")
.append("")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='mailconfirm' src='images/mailconfirm.png' /><br><br>");
});
}
});
return false;
});
});
runOnLoad(function(){
$("input#firstname").select().focus();
});
Then in terms of the PHP script, this is the excerpt:
$letter = $_POST['letter'];
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$lettertype = $_POST['lettertype'];
$terms = $_POST['terms'];
$timestamp = date( 'Y-m-d');
$sql = "INSERT INTO ToBeEmailed (letter, firstname, surname, email, lettertype, terms, timestamp) VALUES ('$letter', '$firstname', '$surname', '$email', '$lettertype', '$terms', '$timestamp')";
If anyone could offer any advice that'd be great! Cheers in advance...:)