Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    HTML to JS to PHP to mySQL Problem///

    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:

    Code:
      
    <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:

    Code:
    $(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:

    Code:
    $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...

  • #2
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    0
    Thanked 2 Times in 2 Posts
    There hare 3 errors in your jquery javascript:

    1)In this line of code:
    Code:
    var dataString = '&letter=' + letter + '&firstname='+ firstname + '&surname=' + surname + '&email=' + email + '&lettertype' + lettertype + '&terms' + terms;
    you missed some '=' . Must be:
    Code:
    var dataString = '&letter=' + letter + '&firstname='+ firstname + '&surname=' + surname + '&email=' + email + '&lettertype=' + lettertype + '&terms=' + terms;
    2) For the checkbox if you want to use this selector
    Code:
    $("input#terms")
    than in the html part you need to use the id="terms" for checkbox.
    Then, this:
    Code:
    <input type="checkbox" name="terms" value="Yes" />
    Becomes:
    Code:
    <input type="checkbox" id="terms" name="terms" value="Yes" />
    3)To control if chekbox is checked is necessary to replace this lines of code:
    Code:
    var terms = $("input#terms").val();
    		if (terms == "") {
          $("label#terms_error").show();
          $("input#terms").focus();
          return false;
        }
    With:
    Code:
    var terms="";
    		if($("input#terms").is(":checked")){
    		    terms = $("input#terms").val();
    		}
    		if (terms == "") {
          $("label#terms_error").show();
          $("input#terms").focus();
    		return false;
    		}
    Elsewhere terms is always Yes.

    I think the rest of your code is correct but I'm not 100% sure.

    Bye
    Antonio

  • Users who have thanked antonioatt for this post:

    Leesome (09-26-2011)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    You are an absolute superstar! Thank you so much indeed!

  • #4
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hello, sorry me again.... I have a quick follow up question...

    At the end of the script, I need a redirect to happen. What I'm after is: If they've selected either "Australia" or "New Zealand" it goes to one page, if it's any other country, it goes to another.

    The code I have is:

    Code:
    if (countryp == "Australia" || "New Zealand"){
    		
    		$.ajax({
          type: "POST",
          url: "../php/postform.php",
          data: dataString,
          success: function() {
           window.location = "WEBSITE1.COM"
          }
         });
    	 }
    		
    	else {
    	$.ajax({
          type: "POST",
          url: "../php/postform.php",
          data: dataString,
          success: function() {
           window.location = "WEBSITE2.COM"
          }
         });
    	}
    I assume there's something wrong with the first line of code... Originally, I only had one variable in the if statement: "Australia". Now that I've added New Zealand in, it does some strange things....

    Again, any help would be really appreciated!

    Cheers


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •