Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 6 1 2 3 ... LastLast
Results 1 to 10 of 52
Like Tree5Likes

Thread: Can't processing values in HTML PHP Form correctly

  1. #1
    Senior Member
    Join Date
    Mar 2010
    Posts
    104
    Member #
    21316
    Liked
    1 times

    Can't processing values in HTML PHP Form correctly

    This is seriously doing my head in and I can't figure out what I'm doing wrong but I've got an online php form with checkboxes but when I leave the checkboxes empty and submit the form, receive the email ... the very first checkbox option shows up (almost like I've ticked / chosen that option) and the second checkbox option doesn't show up (which seems correct cos I didn't tick it)

    Then when I tick both checkboxes, the same results come back as above.
    I want it to behave normally like: 1) only the ticked checkbox options must show up 2) If I don't tick any checkboxes, they must not show up in the form email submission.


    My PHP Form Processor:



    $title = $_POST['title'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $idnum = $_POST['idnum'];
    $birth = $_POST['birth'];
    $married = $_POST['married'];
    $officephone = $_POST['officephone'];
    $cellphone = $_POST['cellphone'];
    $email = $_POST['email'];
    $company = $_POST['company'];
    $position = $_POST['position'];
    $addressline1 = $_POST['addressline1'];
    $addressline2 = $_POST['addressline2'];
    $city = $_POST['city'];
    $postalcode = $_POST['postalcode'];
    $bookingdate = $_POST['bookingdate'];
    $estimate = $_POST['estimate'];
    $currentpackages = $_POST['currentpackages'];
    $treatment01 = $_POST['treatment01'];
    $specialrequests = $_POST['specialrequests'];

    if (isset($_POST['verify'])) :
    $posted_verify = $_POST['verify'];
    $posted_verify = md5($posted_verify);
    else :
    $posted_verify = '';
    endif;

    // Important Variables
    $session_verify = $_SESSION['verify'];

    if (empty($session_verify)) $session_verify = $_COOKIE['verify'];

    $error = '';

    if(trim($title) == '') {
    $error .= '<li>Your title is required.</li>';
    }

    if(trim($firstname) == '') {
    $error .= '<li>Your first name is required.</li>';
    }

    if(trim($lastname) == '') {
    $error .= '<li>Your last name is required.</li>';
    }

    if(trim($idnum) == '') {
    $error .= '<li>Your ID Number is required.</li>';
    }

    if(trim($birth) == '') {
    $error .= '<li>Your Date of Birth is required.</li>';
    }

    if(trim($cellphone) == '') {
    $error .= '<li>Your cell number is required.</li>';
    } elseif(!is_numeric($cellphone)) {
    $error .= '<li>Your cell number can only contain digits (numbers and no spaces).</li>';
    }

    if(trim($email) == '') {
    $error .= '<li>Your e-mail address is required.</li>';
    } elseif(!isEmail($email)) {
    $error .= '<li>You have entered an invalid e-mail address.</li>';
    }

    if(trim($bookingdate) == '') {
    $error .= '<li>You must enter your booking date.</li>';
    }

    if(trim($estimate) == '') {
    $error .= '<li>You must enter your estimate time of arrival.</li>';
    }

    if(trim($currentpackages) == '') {
    $error .= '<li>You must enter a package.</li>';
    }

    if($session_verify != $posted_verify) {
    $error .= '<li>The verification code you entered is incorrect.</li>';
    }

    if($error != '') {
    echo '<div class="error_title">Attention! Please correct the errors below and try again.';
    echo '<ul class="error_messages">' . $error . '</ul>';
    echo '<div class="closebox"><img src="icon-close.png" /></div>';
    echo '</div>';

    } else {

    if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); }

    The code that refers to the checkbox is:

    Code:
    
    $treatment01 = $_POST['treatment01'];
    MY HTML code:


    <p><input type="checkbox" name="treatment01[]" id="checkboxG5" class="css-checkbox checkboxG5 treatment01" value="Back & Neck Aroma Massage" /><label for="checkboxG5" class="css-label">Back & Neck Aroma Massage</label></p>
    <p><input type="checkbox" name="treatment01[]" id="checkboxG6" class="css-checkbox checkboxG6 treatment01" value="Hello" /><label for="checkboxG6" class="css-label">Hello</label></p>
    and then the javascript that does the form validation (I'm not sure if it's necessary posting this but here it is anyway):


    jQuery(document).ready(function() {
    $('.checkinform').submit(function() {
    var action = $(this).attr('action');
    var form = this;
    $('.submit', this).attr('disabled', 'disabled').after(
    '<img src="assets/ajax-loader.gif" class="loader" />');
    $('.message', this).slideUp(750, function() {
    $(this).hide();
    $.post(action, {
    title: $('.title', form).val(),
    firstname: $('.firstname', form).val(),
    lastname: $('.lastname', form).val(),
    idnum: $('.idnum', form).val(),
    birth: $('.birth', form).val(),
    married: $('.married', form).val(),
    officephone: $('.officephone', form).val(),
    cellphone: $('.cellphone', form).val(),
    email: $('.email', form).val(),
    company: $('.company', form).val(),
    position: $('.position', form).val(),
    addressline1: $('.addressline1', form).val(),
    addressline2: $('.addressline2', form).val(),
    city: $('.city', form).val(),
    postalcode: $('.postalcode', form).val(),
    bookingdate: $('.bookingdate', form).val(),
    estimate: $('.estimate', form).val(),
    currentpackages: $('.currentpackages', form).val(),
    treatment01: $('.treatment01', form).val(),
    specialrequests: $('.specialrequests', form).val(),
    verify: $('.verify', form).val()
    },
    function(data) {
    $('.message', form).html(data);
    $('.message', form).slideDown('slow');
    $('img.loader', form).fadeOut('fast', function() {
    $(this).remove();
    });
    $('.submit', form).removeAttr('disabled');
    });
    });
    return false;
    });
    });

    I though checkboxes would be a walk in the park but I've spent so many hours trying to work this out to the point where I'm making guesses hoping to come right!
    Thank you in advance!
    Last edited by reflex84; Jul 25th, 2014 at 05:41 AM.

  2.  

  3. #2
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    I don't want to be a pain, but could you clean up the code a bit? It's really hard to troubleshoot when it's all mushed together like that.

    Also, are there any PHP errors on the page, or JS errors in the console?
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  4. #3
    Senior Member
    Join Date
    Mar 2010
    Posts
    104
    Member #
    21316
    Liked
    1 times
    To be honest... I don't know why the code has been wrapped in an x-overflow instead of a y-overflow if you know what I mean... I'll see what I can do

  5. #4
    Senior Member
    Join Date
    Mar 2010
    Posts
    104
    Member #
    21316
    Liked
    1 times
    I cannot believe this website ... it's "mushed" up my coding!

  6. #5
    Senior Member
    Join Date
    Mar 2010
    Posts
    104
    Member #
    21316
    Liked
    1 times
    Ok well there's something wrong with this website ... I wrap the my code in brackets and other brackets ... no matter what it MINIMIZES my code automatically which is really stupid! I have posted this question here: jquery - Can&#39;t processing values in HTML PHP Form correctly - Stack Overflow ...here you can see the coding better.

    I've tried everything to get checkboxes working in my form but it looks like NO ONE knows what to do and checkboxes needs someone with a degree in the checkboxes subject! I hate checkboxes!! I am going to pull my hair out anytime soon!

  7. #6
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,693
    Member #
    5580
    Liked
    717 times
    Your form has this, name="treatment01[ ]"

    That signifies an array (an array of checkboxes) ...

    In your PHP processing script ...

    Let's look at this line:
    $treatment01 = $_POST['treatment01'];

    $treatment01 is now an array.
    It will only contain values of the checkboxes that are checked.
    If no checkboxes are checked, that array will be empty.

    So when you generate your email, you need to do this:

    foreach($treatment01 as $treatment){
    echo $treatment."\n";
    }

    That loops through the array and outputs whatever has been checked, then adds a newline if you need that for the email message.


    EDIT:
    Now remember what I said about checkboxes? The array will ONLY contain the values of the checkboxes that were checked.

    If you use an array for textboxes ... <input type="text" name="addresses[]"> ...

    $addresses=$_POST['addresses'];

    Again, $addresses will be an array ...

    BUT! and this is a BIG BUT!

    That array will contain ALL of the textboxes with the name "addresses[]", even if they have no value.

    If you have 120 of these ... <input type="text" name="addresses[]">, your array will have 120 elements.

    Not with checkboxes ....
    You only have as many elements as you have "checked" checkboxes. The unchecked boxes don't exist.

    That's F'ed up isn't it.


    EXAMPLE:

    <input type="checkbox" name="pickme[]" value="1"> Enter Name: <input type="text" name="myname[]"><br />
    <input type="checkbox" name="pickme[]" value="2"> Enter Name: <input type="text" name="myname[]"><br />
    <input type="checkbox" name="pickme[]" value="3"> Enter Name: <input type="text" name="myname[]"><br />
    <input type="checkbox" name="pickme[]" value="4"> Enter Name: <input type="text" name="myname[]"><br />
    <input type="checkbox" name="pickme[]" value="5"> Enter Name: <input type="text" name="myname[]"><br />
    <input type="checkbox" name="pickme[]" value="6"> Enter Name: <input type="text" name="myname[]"><br />

    $picks=$_POST['pickme'];
    $names=$_POST['myname'];

    If you check 2 of those,
    $picks will only have 2 array elements (only the two that you checked) ...

    But $names will have all of them (6 elements) whether you enter anything or not, all of them will be in the array.
    Last edited by mlseim; Jul 24th, 2014 at 03:36 PM.


  8. #7
    Senior Member
    Join Date
    Mar 2010
    Posts
    104
    Member #
    21316
    Liked
    1 times
    @Msleim

    First off, thank you for elaborating and not responding as if I've got a degree in PHP (because I wouldn't have this problem in the first place).

    It will only contain values of the checkboxes that are checked.
    If no checkboxes are checked, that array will be empty.
    That is EXACTLY what I want ... I mean isn't that the purpose of checkboxes in most cases?

    So when you generate your email, you need to do this:

    foreach($treatment01 as $treatment){
    echo $treatment."\n";
    }
    Okay so you're saying that I'm missing this bit of php to execute the ticked checkboxes in an array?
    Is there a specific place where I must put this code?
    Must I replace $treatment01 = $_POST['treatment01']; with that code? or simply add that code in above?
    In that code, I also don't understand why you've got $treatment and not $treatment01 ... (you're missing the '01') ... because I haven't got $treatment in any of my code for it to relate to if you know what I mean...

    That loops through the array and outputs whatever has been checked, then adds a newline if you need that for the email message.
    Great ... I've seen this word "loop" quite often whilst trying to sort my problem out... what does this mean exactly? Loop as in cycle?

    If you check 2 of those,
    $picks will only have 2 array elements (only the two that you checked) ...

    But $names will have all of them (6 elements) whether you enter anything or not, all of them will be in the array.
    Understood

    I see in your example:

    $picks=$_POST['pickme'];
    $names=$_POST['myname'];
    Why are you not making the ID's (if you could call it that) the same, eg:

    $pickme=$_POST['pickme'];
    $myname=$_POST['myname'];
    Last edited by reflex84; Jul 25th, 2014 at 04:04 AM.

  9. #8
    Senior Member
    Join Date
    Mar 2010
    Posts
    104
    Member #
    21316
    Liked
    1 times
    I have updated my answer reply above, so just refresh the page incase you haven't read the update...

  10. #9
    Senior Member
    Join Date
    Mar 2010
    Posts
    104
    Member #
    21316
    Liked
    1 times
    Oh and just so you know ... I've spend like 3 days in total trying to get this to work and I'm literally just copying pasting hoping for the best

  11. #10
    Senior Member
    Join Date
    Mar 2010
    Posts
    104
    Member #
    21316
    Liked
    1 times
    Here's a snippet of how I am outputting this into email:

    <td width="375" valign="middle"><span style="font-family:Arial, Helvetica, sans-serif; font-size:14px;"> '.$_POST['treatment01'].' </span></td>
    I am not sure if this is right?
    Last edited by reflex84; Jul 25th, 2014 at 04:31 AM.


Page 1 of 6 1 2 3 ... LastLast

Remove Ads

Posting Permissions

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

Tags for this Thread

All times are GMT -6. The time now is 05:01 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com