Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 14 of 14

Thread: PHP Checkbox code help

  1. #1

    PHP Checkbox code help

    I am having a hard time getting my php to work with my html. I have several sets of checkboxes and I believe that is what is not coded right. Can someone please help???

    Here is the code in html and php for my checkboxes. Right now when I sumbit it sends me to a page that says "blarg!"

    HTML Code:
    <form method="POST" action="mailer.php" ><p><strong>Was this contact (check all that apply)</strong></p>
                
      <label>
                  <input name="contact" type="checkbox" value="In person" />
                  In person</label>
              <br />
                    <label>
                  <input name="contact" type="checkbox" value="By phone" />
                  By phone</label>
              <br />
                    <label>
                  <input name="contact" type="checkbox" value="By email" />
                  By email</label>
              <br />
                    <label>
                  <input name="contact" type="checkbox" value="Fax" />
                  Fax</label>
              <br />
                    <label>
                  <input name="contact" type="checkbox" value="No contact with the Financial Aid Office" />
                  No contact with the Financial Aid Office</label>
              <br />
                    <label>
                  <input name="contact" type="checkbox" value="Other" />
                  Other</label>
              <p><strong>What was the nature of your visit(s)? (check all the apply)</strong></strong><br />
                <label>
                  <input name="nature" type="checkbox" value="Loan question/assistance" />
                  Loan question/assistance</label>
                  <br />
                  <label>
                  <input name="nature" type="checkbox" value="Scholarship question/assistance" />
                  Scholarship question/assistance</label>
                  <br />
                  <label>
                  <input name="nature" type="checkbox" value="Application/FAFSA assistance" />
                  Application/FAFSA assistance</label>
                  <br />
                  <label>
                  <input name="nature" type="checkbox" value="Debt load counseling" />
                  Debt load counseling</label>
                  <br />
                  <label>
                  <input name="nature" type="checkbox" value="HPSP question/counseling" />
                  HPSP question/counseling</label>
                  <br />
                  <label>
                  <input name="nature" type="checkbox" value="Special Circumstances Appeal" />
                  Special Circumstances Appeal</label>
                  <br />
                  <label>
                  <input name="nature" type="checkbox" value="No contact with the Financial Aid Office2" />
                  No contact with the Financial Aid Office</label>
              </p> </form>
    PHP Code:
    <?php
    if(isset($_POST['submit'])) { 
    $to xxx@myemail.com
    $subject "Financial Aid Survey"; foreach($_POST['checkbox'] as $value) { 
    $checkbox_msg .= "Checked: $contact\n";} 
    foreach(
    $_POST['checkbox'] as $value) { 
    $checkbox_msg .= "Checked: $nature\n";} 
    $body =Was this contact$contact\
    What was the nature of your visit
    $nature\n

    echo 
    "Thank you! Your information has been submitted to $to!"
    mail($to$subject$body); 
    } else { 
    echo 
    "blarg!"

    ?>
    Thank you!

  2. #2
    All elements you call from a form you call by their NAME not their TYPE so in your code you're calling by the type not the name.

    PHP Code:
    $_POST['checkbox'// THIS IS WRONG 
    should be
    PHP Code:
    $_POST['contact']  // THIS IS CORRECT 
    AND
    PHP Code:
    $_POST['nature']  // THIS IS CORRECT 
    also in your HTML you need to define that the checkboxes are a series, an array so the NAME would be
    HTML Code:
    <input type="checkbox" name="contact[]" value="foo" />
    <input type="checkbox" name="contact[]" value="bar" />
    <!-- and so forth -->
    
    <input type="checkbox" name="nature[]" value="foo" />
    <input type="checkbox" name="nature[]" value="bar" />
    <!-- and so forth -->
    note the names of each of the inputs.

    this is pseudo code so don't go plopping it in and expecting it to work, you need to make the appropriate changes to your code.
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  3. #3

    Some Changes!!!

    Hello simplistik,

    I have made some changes in both files html & php pls check it I have checked at my end it works file please change to email address to your email address. I am attaching both files with this.

    Get Back To Me If You Have Any Problem.
    Regards,
    Kunjan.
    Attached Files Attached Files

  4. #4
    Thanks for the suggestions. I have tried both fixes and it still does not work. Still gives me "blarg!" below I have put my entire html and php. I thought it was just the checkboxes that was messing me up but I guess not. Please help!!

    HTML Code:
    <form method="POST" action="mailer.php" >
              <label>
              <strong>What class are you in?</strong>
              <input name="Class2" type="text" id="Class2" maxlength="4" />
              <br />
              <br />
              <strong>How many times have you had contact with the Financial Aid Office within the past 12 months?</strong></label>
              </br>
            <label> <br />
            <input type="radio" name="times" value="0-2" /> 
            0-2</label>
              <br />
                <label>
                  <input type="radio" name="times" value="3-5" />
                  3-5</label>
                <br />
                <label>
                  <input type="radio" name="times" value="5-10" />
                  5-10</label>
                <br />
                <label>
                  <input type="radio" name="times" value="Over 10" />
                  Over 10 </label>
                <p><strong>Was this contact (check all that apply)</strong></p>
                
      <label>
                  <input name="contact[]" type="checkbox" value="In person" />
                  In person</label>
              <br />
                    <label>
                  <input name="contact[]" type="checkbox" value="By phone" />
                  By phone</label>
              <br />
                    <label>
                  <input name="contact[]" type="checkbox" value="By email" />
                  By email</label>
              <br />
                    <label>
                  <input name="contact[]" type="checkbox" value="Fax" />
                  Fax</label>
              <br />
                    <label>
                  <input name="contact[]" type="checkbox" value="No contact with the Financial Aid Office" />
                  No contact with the Financial Aid Office</label>
              <br />
                    <label>
                  <input name="contact[]" type="checkbox" value="Other" />
                  Other</label>
              <p><strong>What was the nature of your visit(s)? (check all the apply)</strong></strong><br />
                <label>
                  <input name="nature[]" type="checkbox" value="Loan question/assistance" />
                  Loan question/assistance</label>
                  <br />
                  <label>
                  <input name="nature[]" type="checkbox" value="Scholarship question/assistance" />
                  Scholarship question/assistance</label>
                  <br />
                  <label>
                  <input name="nature[]" type="checkbox" value="Application/FAFSA assistance" />
                  Application/FAFSA assistance</label>
                  <br />
                  <label>
                  <input name="nature[]" type="checkbox" value="Debt load counseling" />
                  Debt load counseling</label>
                  <br />
                  <label>
                  <input name="nature[]" type="checkbox" value="HPSP question/counseling" />
                  HPSP question/counseling</label>
                  <br />
                  <label>
                  <input name="nature[]" type="checkbox" value="Special Circumstances Appeal" />
                  Special Circumstances Appeal</label>
                  <br />
                  <label>
                  <input name="nature[]" type="checkbox" value="No contact with the Financial Aid Office2" />
                  No contact with the Financial Aid Office</label>
              </p>
              <p><strong>Were you treated courteously and professionally?</strong><br />
                <label>
    <input type="radio" name="treated" value="Strongly agree" />
    Strongly agree</label>
                <br />
                <label>
                <input type="radio" name="treated" value="Agree" />
    Agree</label>
                <br />
                <label>
                <input type="radio" name="treated" value="No opinion/no contact with the Financial Aid Office" />
    No opinion/no contact with the Financial Aid Office</label>
                <br />
                <label>
                <input type="radio" name="treated" value="Disagree" />
    Disagree</label>
                <br />
                <label>
                <input type="radio" name="treated" value="Strongly disagree" />
    Strongly disagree</label>
                <br />
                <br />
                <label></label>
                <strong>Were your questions/concerns adequately addressed?</strong><br />
                  <label>
                  <input type="radio" name="addressed" value="Strongly agree" />
    Strongly agree</label>
                  <br />
                  <label>
                  <input type="radio" name="addressed" value="Agree" />
    Agree</label>
                  <br />
                  <label>
                  <input type="radio" name="addressed" value="No contact with the Financial Aid Office" />
    No contact with the Financial Aid Office</label>
                  <br />
                  <label>
                  <input type="radio" name="addressed" value="Disagree" />
    Disagree</label>
                  <br />
                  <label>
                  <input type="radio" name="addressed" value="Strongly disagree" />
    Strongly disagree</label>
                  <br />
                <label><br />
                </label>
                <strong>Was the Financial Aid Office staff knowledgeable with accurate and up-to-date information?</strong><br />     
                <label>
                <input type="radio" name="knowledgeable" value="Strongly agree" />
    Strongly agree</label>
                <br />
                <label>
                <input type="radio" name="knowledgeable" value="Agree" />
    Agree</label>
                <br />
                <label>
                <input type="radio" name="knowledgeable" value="No contact with the Financial Aid Office" />
    No contact with the Financial Aid Office</label>
                <br />
                <label>
                <input type="radio" name="knowledgeable" value="Disagree" />
    Disagree</label>
                <br />
                <label>
                <input type="radio" name="knowledgeable" value="Strongly disagree" />
    Strongly disagree</label>
                <br />
                <label></label>
              </p>
              <p>  <strong>Was the Financial Aid Office efficient and timely in getting all required information &amp; forms to you regarding financial aid?</strong><br />
                <label>
                <input type="radio" name="efficient" value="Strongly agree" />
    Strongly agree</label>
                <br />
                <label>
                <input type="radio" name="efficient" value="Agree" />
    Agree</label>
                <br />
                <label>
                <input type="radio" name="efficient" value="No contact with the Financial Aid Office" />
    No contact with the Financial Aid Office</label>
                <br />
                <label>
                <input type="radio" name="efficient" value="Disagree"  />
    Disagree</label>
                <br />
                <label>
                <input type="radio" name="efficient" value="Strongly disagree" />
    Strongly disagree</label>
                <br />
                <label></label>
              </p>
              <p><strong>Was the Financial Aid Office thorough in explaining your financial aid options?</strong><br />
                <label>            
                <input type="radio" name="explaining" value="Strongly agree" />
    Strongly agree</label>
                <br />
                <label>
                <input type="radio" name="explaining" value="Agree" />
    Agree</label>
                <br />
                <label>
                <input type="radio" name="explaining" value="No contact with the Financial Aid Office" />
    No contact with the Financial Aid Office</label>
                <br />
                <label>
                <input type="radio" name="explaining" value="Disagree" />
    Disagree</label>
                <br />
                <label>
                <input type="radio" name="explaining" value="Strongly disagree" />
    Strongly disagree</label>
                <br />
                <label></label>
              </p>
              <p><strong>If you received assistance through the Financial Aid website, was there sufficient information available to answer your questions?</strong><br />
                <label>            
                <input type="radio" name="information" value="Yes"/>
    Yes</label>
                <br />
                <label>
                <input type="radio" name="information" value="No" />
    No</label>
                <br />
                <label>
                <input type="radio" name="information" value="Have not received assistance through website" />
    Have not received assistance through website</label>
                <br />
                <label></label>
              </p>
              <p><strong>How satisfied were you with your overall visit to the Financial Aid website?</strong><br />
                <label>            
                <input type="radio" name="satisfied" value="Very satisfied" />
    Very satisfied</label>
                <br />
                <label>
                <input type="radio" name="satisfied" value="Satisfied" />
    Satisfied</label>
                <br />
                <label>
                <input type="radio" name="satisfied" value="Have not visited the website" />
    Have not visited the website</label>
                <br />
                <label>
                <input type="radio" name="satisfied" value="Dissatisfied" />
    Dissatisfied</label>
                <br />
                <label>
                <input type="radio" name="satisfied" value="Very dissatisfied" />
    Very dissatisfied</label>
                <br />
                <label></label>
              </p>
            <p><strong>Do you feel informed on the availability of financial aid resources?</strong><br />
                  <label>
                  <input type="radio" name="informed" value="Strongly agree" />
    Strongly agree</label>
                  <br />
                  <label>
                  <input type="radio" name="informed" value="Agree" />
    Agree</label>
                  <br />
                  <label>
                  <input type="radio" name="informed" value="No opinion/not interested in financial aid" />
    No opinion/not interested in finanical aid</label>
                  <br />
                  <label>
                  <input type="radio" name="informed" value="Disagree" />
    Disagree</label>
                  <br />
                  <label>
                  <input type="radio" name="informed" value="Strongly disagree" />
    Strongly disagree</label>
               <br />
                <label></label>
            </p>
              <p>
                <label><strong>Please include any comments or suggestions</strong>.<br />
                <textarea name="suggestions" id="Please include any comments or suggestions." cols="45" rows="5"></textarea>
                </label>
              </p>
              <p>
                <label><strong>If you would like us to contact you about any of your responses, please provide your name and email address.
                </strong>
                <textarea name="reply" cols="45" rows="5"></textarea>
                </label>
              </p>
              <p align="right">
                <label>
                <input type="submit" name="Submit" value="Submit" />
                <input type="reset" name="Reset" value="Reset" />
                </label>
                <br /> 
              </p>
            </form>
    PHP Code:
    <?php 
    if(isset($_POST['submit'])) { 
    $checkbox_msg "";
    $to xxx@myemail.com
    $subject "Financial Aid Survey"
    $class2_field $_POST['Class2']; 
    $option $_POST['times'];
    foreach(
    $_POST['contact'] as $value) { 
    $checkbox_msg .= "Checked: $value\n";} 
    foreach(
    $_POST['nature'] as $value) { 
    $checkbox_msg .= "Checked: $value\n";} 
    $option2 $_POST['treated'];
    $option3 $_POST['addressed'];
    $option4 $_POST['knowledgeable'];
    $option5 $_POST['efficient'];
    $option6 $_POST['explaining'];
    $option7 $_POST['information'];
    $option8 $_POST['satisfied'];
    $option9 $_POST['informed'];
    $suggestions $_POST['suggestions'];
    $reply $_POST['reply']; 
      
    $body "Class: $class2_field\n 
    How many times have you had contact with the Financial Aid Office within the past 12 months: 
    $option\n 
    $checkbox_msg
    Were you treated courteously and professionally: 
    $option2\n 
    Were your questions/concerns adequately addressed: 
    $option3\n 
    Was the Financial Aid Office staff knowledgeable with accurate and up-to-date information: 
    $option4\n 
    Was the Financial Aid Office efficient and timely in getting all required information & forms to you regarding financial aid: 
    $option5\n 
    Was the Financial Aid Office thorough in explaining your financial aid options: 
    $option6\n 
    If you received assistance through the Financial Aid website, was there sufficient information available to answer your questions: 
    $option7\n 
    How satisfied were you with your overall visit to the Financial Aid website: 
    $option8\n 
    Do you feel informed on the availability of financial aid resources: 
    $option9\n 
    Suggestions:\n 
    $suggestions\n
    If you would like us to contact you about any of your responses, please provide your name and email address:\n 
    $reply\n";   
      
    echo 
    "Thank you! Your information has been submitted to $to!"
    mail($to$subject$body); 
    } else { 
    echo 
    "blarg!"

    ?>

  5. #5
    @rubberduckie

    your isset is wrong ... you have
    PHP Code:
    isset($_POST['submit']) 
    when your HTML shows that the NAME attribute of your submit button is Submit notice the capital 'S'
    HTML Code:
    <input type="submit" name="Submit" value="Submit" />
    so you either need to change the NAME attribute to a lowercase 's' or change the isset to a capital 'S'

    off topic:
    -------------------------


    Quote Originally Posted by kunjan View Post
    Hello simplistik,
    And you my friend need to pay attention to who the original poster is ...

    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  6. #6
    Oh also your $to address needs to be in quotes, I assume it is in your actual code but just didn't make it in the code you posted as you prolly edited it out
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  7. #7
    THANK YOU, THANK YOU, THANK YOU!!! It is working now!!! I appreciate the help!

  8. #8
    I am sorry for all the questions but I promise I only have one more. I want to make the first 4 questions required but everything I read seems not to help. Any suggestions??

    Thanks again for all the help!

  9. #9
    Not sure what your first 4 questions are exactly but you can do a simple if statement around the mail() function
    PHP Code:
    if ( !empty($_POST['question1']) && !empty($_POST['question2']) && !empty($_POST['question3']) && !empty($_POST['question4']) ) {
    mail($to$subject$body); 
    } else {
    // do something else here if those 4 questions aren't filled in

    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  10. #10
    Thanks!! That is great!!! So I how do I have a box pop up to say that the field are not filled in without deleteing everything?

  11. #11
    After reading my question it didn't sound very clear I just want to restate it. When someone fills out the form but leaves one of the required fields blank and hits submit I want a popup menu to appear leaving the form how it is but gives attention that the required fields are not filled in. Thanks again for all the help!

  12. #12
    That would be JavaScript, probably.
    Xbox 360 Gamertag: Maqrkk
    Current gamerscore: 20515

  13. #13
    Thanks. By reading everything I have since on the web I think Javascript would be the best but I'm just not sure and how to go about this.

  14. #14
    To check that all fields are completed before the form is posted is Client Side, so you'd need Javascript:
    Code:
    <script language="JavaScript" type="text/javascript">
    <!--
    function checkform ( form )
    {
      if (form.understand.checked == false) { //<-- Field To Be Checked - Can be more than one
        alert( "Please ensure that you complete all fields." ); //<-- Alert Box with Message
        form.understand.focus(); //<-- This will focus on a part of the form
        return false ;
      }
      return true ;
    }
    //-->
    </script>
    Then your form would then have this in the form declaration:
    PHP Code:
    <form method="POST" action="mailer.php" onsubmit="return checkform(this);" 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012