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 11 of 11
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    62
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Inserting Multiple Checkbox Values Into Database

    Hi All, I have this form with checkboxes, and would like the values of all the checkboxes ticked to be interted into a single field into the database, however it does not do this correctly, can anyone see where I am going wrong with this?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Market Research</title>
    </head>
    <body>
    <form method="post" action="process.php">
    <p>Please indicate which industrial sector you conduct business within:</p>
    <input type="radio" name="1" value="Primary" />Primary<br />
    <input type="radio" name="1" value="Secondary" />Secondary<br />
    <input type="radio" name="1" value="Tertiary" />Tertiary<br />
    <input type="radio" name="1" value="Quaternary" />Quaternary
    
    <p>1) On a scale of 1 to 5, how much do you value your website as a business asset?</p>
    <input type="radio" name="2" value="1" />1
    <input type="radio" name="2" value="2" />2
    <input type="radio" name="2" value="3" />3
    <input type="radio" name="2" value="4" />4
    <input type="radio" name="2" value="5" />5
    
    <p>2) On a scale of 1 to 5, how well do you understand the role of your company's website?</p>
    <input type="radio" name="3" value="1" />1
    <input type="radio" name="3" value="2" />2
    <input type="radio" name="3" value="3" />3
    <input type="radio" name="3" value="4" />4
    <input type="radio" name="3" value="5" />5
    
    <p>3) Do you create, plan and execute a strategically thought out marketing plan for your company at least once a year?</p>
    <input type="radio" name="4" value="Yes" />Yes
    <input type="radio" name="4" value="No" />No
    
    <p>4) On a scale of 1 to 5, how much business do you think your website generates for you?</p>
    <input type="radio" name="5" value="1" />1
    <input type="radio" name="5" value="2" />2
    <input type="radio" name="5" value="3" />3
    <input type="radio" name="5" value="4" />4
    <input type="radio" name="5" value="5" />5
    
    <p>5) Which of the following do you think should be your websites number one task?</p>
    <input type="radio" name="6" value="To act as an accurate visual presentation of your company" />To act as an accurate visual presentation of your company<br />
    <input type="radio" name="6" value="Attract as many new leads as possible" />Attract as many new leads as possible<br />
    <input type="radio" name="6" value="Inform your target market of the servies/products you offer" />Inform your target market of the servies/products you offer
    
    <p>6) On a scale of 1 to 5, how relevant to your business do you think Search Engine Optimisation will become in the next 12 months?</p>
    <input type="radio" name="7" value="1" />1
    <input type="radio" name="7" value="2" />2
    <input type="radio" name="7" value="3" />3
    <input type="radio" name="7" value="4" />4
    <input type="radio" name="7" value="5" />5
    
    <p>7) On a scale of 1 to 5, how relavent to your business' success will your website be during the next 12 months?</p>
    <input type="radio" name="8" value="1" />1
    <input type="radio" name="8" value="2" />2
    <input type="radio" name="8" value="3" />3
    <input type="radio" name="8" value="4" />4
    <input type="radio" name="8" value="5" />5
    
    <p>8) Please indicate which products below you think you would most benefit from:</p>
    <input type="radio" name="9" value="Search Engine Optimisation" />Search Engine Optimisation<br />
    <input type="radio" name="9" value="A complete new website" />A complete new website<br />
    <input type="radio" name="9" value="Consultancy on how to better use your online presence" />Consultancy on how to better use your online presence<br />
    <input type="radio" name="9" value="Consultancy on Search Engine Optimisation and Internet Marketing" />Consultancy on Search Engine Optimisation and Internet Marketing
    
    <p>9) On a scale of 1 to 5, to what degree would you say you understand the term "digital marketing"?</p>
    <input type="radio" name="10" value="1" />1
    <input type="radio" name="10" value="2" />2
    <input type="radio" name="10" value="3" />3
    <input type="radio" name="10" value="4" />4
    <input type="radio" name="10" value="5" />5
    
    <p>10) Please indicate, by ticking the appropriate boxes, the elements you think that makes a strong web design firm:</p>
    <input type="checkbox" name="11[]" value="Strong portfolio" />Strong portfolio<br />
    <input type="checkbox" name="11[]" value="Wide range of services" />Wide range of services<br />
    <input type="checkbox" name="11[]" value="Rapid turnaround of projects" />Rapid turnaround of projects<br />
    <input type="checkbox" name="11[]" value="Design awards for their websites" />Design awards for their websites<br />
    <input type="checkbox" name="11[]" value="Revavant accreditations and awards" />Revavant accreditations and awards<br />
    <input type="checkbox" name="11[]" value="Evidence of results" />Evidence of results<br />
    <input type="checkbox" name="11[]" value="Frequent communication with you" />Frequent communication with you<br />
    <input type="checkbox" name="11[]" value="Strong differentiation from completion" />Strong differentiation from completion
    
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
    </body>
    </html>
    PHP Code:
    <?php
    $connection 
    mysql_connect("localhost","username","password") or die ('Could not connect: ' mysql_error());
    $question1 $_POST['1'];
    $question2 $_POST['2'];
    $question3 $_POST['3'];
    $question4 $_POST['4'];
    $question5 $_POST['5'];
    $question6 $_POST['6'];
    $question7 $_POST['7'];
    $question8 $_POST['8'];
    $question9 $_POST['9'];
    $question10 $_POST['10'];
    $question11 serialize($_POST['11']); 

    mysql_select_db("database_name"$connection);
    mysql_query("INSERT INTO entries (question1, question2, question3, question4, question5, question6, question7, question8, question9, question10, question11) VALUES ('$question1', '$question2', '$question3', '$question4', '$question5', '$question6', '$question7', '$question8', '$question9', '$question10', '$question11')");
    print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
    ?>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    You have to use arrays ...

    Example:

    <p>Please indicate which industrial sector you conduct business within:</p>
    <input type="radio" name="r1[]" value="Primary" />Primary<br />
    <input type="radio" name="r1[]" value="Secondary" />Secondary<br />
    <input type="radio" name="r1[]" value="Tertiary" />Tertiary<br />
    <input type="radio" name="r1[]" value="Quaternary" />Quaternary

    Then in PHP, you read the array

    I don't have time now to post more, but you can find examples using Google,
    or someone else will provide some examples.

  • #3
    New Coder
    Join Date
    Nov 2009
    Posts
    62
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Thank you for your reply, I have now done this, however the word Array appears in the database field, any ideas on how to solve this?

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    Reprint your script ... what you have now.

  • #5
    New Coder
    Join Date
    Nov 2009
    Posts
    62
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Market Research</title>
    </head>
    <body>
    <form method="post" action="process.php">
    <p>Please indicate which industrial sector you conduct business within:</p>
    <input type="radio" name="1[]" value="Primary" />Primary<br />
    <input type="radio" name="1[]" value="Secondary" />Secondary<br />
    <input type="radio" name="1[]" value="Tertiary" />Tertiary<br />
    <input type="radio" name="1[]" value="Quaternary" />Quaternary
    
    <p>1) On a scale of 1 to 5, how much do you value your website as a business asset?</p>
    <input type="radio" name="2[]" value="1" />1
    <input type="radio" name="2[]" value="2" />2
    <input type="radio" name="2[]" value="3" />3
    <input type="radio" name="2[]" value="4" />4
    <input type="radio" name="2[]" value="5" />5
    
    <p>2) On a scale of 1 to 5, how well do you understand the role of your company's website?</p>
    <input type="radio" name="3[]" value="1" />1
    <input type="radio" name="3[]" value="2" />2
    <input type="radio" name="3[]" value="3" />3
    <input type="radio" name="3[]" value="4" />4
    <input type="radio" name="3[]" value="5" />5
    
    <p>3) Do you create, plan and execute a strategically thought out marketing plan for your company at least once a year?</p>
    <input type="radio" name="4[]" value="Yes" />Yes
    <input type="radio" name="4[]" value="No" />No
    
    <p>4) On a scale of 1 to 5, how much business do you think your website generates for you?</p>
    <input type="radio" name="5[]" value="1" />1
    <input type="radio" name="5[]" value="2" />2
    <input type="radio" name="5[]" value="3" />3
    <input type="radio" name="5[]" value="4" />4
    <input type="radio" name="5[]" value="5" />5
    
    <p>5) Which of the following do you think should be your websites number one task?</p>
    <input type="radio" name="6[]" value="To act as an accurate visual presentation of your company" />To act as an accurate visual presentation of your company<br />
    <input type="radio" name="6[]" value="Attract as many new leads as possible" />Attract as many new leads as possible<br />
    <input type="radio" name="6[]" value="Inform your target market of the servies/products you offer" />Inform your target market of the servies/products you offer
    
    <p>6) On a scale of 1 to 5, how relevant to your business do you think Search Engine Optimisation will become in the next 12 months?</p>
    <input type="radio" name="7[]" value="1" />1
    <input type="radio" name="7[]" value="2" />2
    <input type="radio" name="7[]" value="3" />3
    <input type="radio" name="7[]" value="4" />4
    <input type="radio" name="7[]" value="5" />5
    
    <p>7) On a scale of 1 to 5, how relavent to your business' success will your website be during the next 12 months?</p>
    <input type="radio" name="8[]" value="1" />1
    <input type="radio" name="8[]" value="2" />2
    <input type="radio" name="8[]" value="3" />3
    <input type="radio" name="8[]" value="4" />4
    <input type="radio" name="8[]" value="5" />5
    
    <p>8) Please indicate which products below you think you would most benefit from:</p>
    <input type="radio" name="9[]" value="Search Engine Optimisation" />Search Engine Optimisation<br />
    <input type="radio" name="9[]" value="A complete new website" />A complete new website<br />
    <input type="radio" name="9[]" value="Consultancy on how to better use your online presence" />Consultancy on how to better use your online presence<br />
    <input type="radio" name="9[]" value="Consultancy on Search Engine Optimisation and Internet Marketing" />Consultancy on Search Engine Optimisation and Internet Marketing
    
    <p>9) On a scale of 1 to 5, to what degree would you say you understand the term "digital marketing"?</p>
    <input type="radio" name="10[]" value="1" />1
    <input type="radio" name="10[]" value="2" />2
    <input type="radio" name="10[]" value="3" />3
    <input type="radio" name="10[]" value="4" />4
    <input type="radio" name="10[]" value="5" />5
    
    <p>10) Please indicate, by ticking the appropriate boxes, the elements you think that makes a strong web design firm:</p>
    <input type="checkbox" name="11[]" value="Strong portfolio" />Strong portfolio<br />
    <input type="checkbox" name="11[]" value="Wide range of services" />Wide range of services<br />
    <input type="checkbox" name="11[]" value="Rapid turnaround of projects" />Rapid turnaround of projects<br />
    <input type="checkbox" name="11[]" value="Design awards for their websites" />Design awards for their websites<br />
    <input type="checkbox" name="11[]" value="Revavant accreditations and awards" />Revavant accreditations and awards<br />
    <input type="checkbox" name="11[]" value="Evidence of results" />Evidence of results<br />
    <input type="checkbox" name="11[]" value="Frequent communication with you" />Frequent communication with you<br />
    <input type="checkbox" name="11[]" value="Strong differentiation from completion" />Strong differentiation from completion
    
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
    </body>
    </html>
    PHP Code:
    <?php
    $connection 
    mysql_connect("localhost","username","password") or die ('Could not connect: ' mysql_error());
    $question1 $_POST['1'];
    $question2 $_POST['2'];
    $question3 $_POST['3'];
    $question4 $_POST['4'];
    $question5 $_POST['5'];
    $question6 $_POST['6'];
    $question7 $_POST['7'];
    $question8 $_POST['8'];
    $question9 $_POST['9'];
    $question10 $_POST['10'];
    $question11 serialize($_POST['11']); 

    mysql_select_db("database_name"$connection);
    mysql_query("INSERT INTO entries (question1, question2, question3, question4, question5, question6, question7, question8, question9, question10, question11) VALUES ('$question1', '$question2', '$question3', '$question4', '$question5', '$question6', '$question7', '$question8', '$question9', '$question10', '$question11')");
    print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
    ?>

  • #6
    Regular Coder
    Join Date
    Jul 2009
    Posts
    136
    Thanks
    0
    Thanked 7 Times in 7 Posts
    As the variables (e.g. $question1) are arrays PHP is returning that fact. I don't understand why you would want the data in a single field because that's not what databases do. Fields should hold one piece of data only. However if you really want to swap:

    Code:
    $question1 = $_POST['1'];
    with something like:

    Code:
    $question1 = implode(",",$_POST['1']);
    That will give you the values in one variable separated by commas (you could use any character).

  • Users who have thanked DaiLaughing for this post:

    ben1390 (03-17-2010)

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    oh ... my bad.

    The array only needs to be for your checkboxes (not the radio buttons).

    Your radio buttons will look like this:
    <p>Please indicate which industrial sector you conduct business within:</p>
    <input type="radio" name="1" value="Primary" />Primary<br />
    <input type="radio" name="1" value="Secondary" />Secondary<br />
    <input type="radio" name="1" value="Tertiary" />Tertiary<br />
    <input type="radio" name="1" value="Quaternary" />Quaternary


    You checkboxes will look like:
    <p>10) Please indicate, by ticking the appropriate boxes, the elements you think that makes a strong web design firm:</p>
    <input type="checkbox" name="11[]" value="Strong portfolio" />Strong portfolio<br />
    <input type="checkbox" name="11[]" value="Wide range of services" />Wide range of services<br />
    etc...


    Then, in your script, you will have to process the check boxes, as they will be an array.

    Here's a way to test it ... only the boxes checked will appear in the array.

    $checkboxes=$_POST['11'];
    while (list ($key,$val) = @each ($checkbox)) {
    echo "$val,";
    }

  • Users who have thanked mlseim for this post:

    ben1390 (03-17-2010)

  • #8
    New Coder
    Join Date
    Nov 2009
    Posts
    62
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Thank you this has worked, however where I have the radio buttons and text areas with the same name, the value of the radio buttons is not being inserted into the database, any ideas why this is happening?

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    Form variables can't have the same name.
    But I don't see any text boxes on your form (post #5)

  • #10
    New Coder
    Join Date
    Nov 2009
    Posts
    62
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Sorry, here is the new code, questions 5 and 9 have radio buttons as well as text areas, however the values of the radio buttons are not being entered into the database

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Market Research</title>
    </head>
    <body>
    <form method="post" action="process.php">
    <p>Please indicate which industrial sector you conduct business within:</p>
    <input type="radio" name="1" value="Primary" />Primary<br />
    <input type="radio" name="1" value="Secondary" />Secondary<br />
    <input type="radio" name="1" value="Tertiary" />Tertiary<br />
    <input type="radio" name="1" value="Quaternary" />Quaternary
    
    <p>1) On a scale of 1 to 5, how much do you value your website as a business asset?</p>
    <input type="radio" name="2" value="1" />1
    <input type="radio" name="2" value="2" />2
    <input type="radio" name="2" value="3" />3
    <input type="radio" name="2" value="4" />4
    <input type="radio" name="2" value="5" />5
    
    <p>2) On a scale of 1 to 5, how well do you understand the role of your company's website?</p>
    <input type="radio" name="3" value="1" />1
    <input type="radio" name="3" value="2" />2
    <input type="radio" name="3" value="3" />3
    <input type="radio" name="3" value="4" />4
    <input type="radio" name="3" value="5" />5
    
    <p>3) Do you create, plan and execute a strategically thought out marketing plan for your company at least once a year?</p>
    <input type="radio" name="4" value="Yes" />Yes
    <input type="radio" name="4" value="No" />No
    
    <p>4) On a scale of 1 to 5, how much business do you think your website generates for you?</p>
    <input type="radio" name="5" value="1" />1
    <input type="radio" name="5" value="2" />2
    <input type="radio" name="5" value="3" />3
    <input type="radio" name="5" value="4" />4
    <input type="radio" name="5" value="5" />5
    
    <p>5) Which of the following do you think should be your websites number one task?</p>
    <input type="radio" name="6" value="To act as an accurate visual presentation of your company" />To act as an accurate visual presentation of your company<br />
    <input type="radio" name="6" value="Attract as many new leads as possible" />Attract as many new leads as possible<br />
    <input type="radio" name="6" value="Inform your target market of the servies/products you offer" />Inform your target market of the servies/products you offer<br />
    Other (please specify) <textarea name="6"></textarea>
    
    <p>6) On a scale of 1 to 5, how relevant to your business do you think Search Engine Optimisation will become in the next 12 months?</p>
    <input type="radio" name="7" value="1" />1
    <input type="radio" name="7" value="2" />2
    <input type="radio" name="7" value="3" />3
    <input type="radio" name="7" value="4" />4
    <input type="radio" name="7" value="5" />5
    
    <p>7) On a scale of 1 to 5, how relavent to your business' success will your website be during the next 12 months?</p>
    <input type="radio" name="8" value="1" />1
    <input type="radio" name="8" value="2" />2
    <input type="radio" name="8" value="3" />3
    <input type="radio" name="8" value="4" />4
    <input type="radio" name="8" value="5" />5
    
    <p>8) Please indicate which products below you think you would most benefit from:</p>
    <input type="radio" name="9" value="Search Engine Optimisation" />Search Engine Optimisation<br />
    <input type="radio" name="9" value="A complete new website" />A complete new website<br />
    <input type="radio" name="9" value="Consultancy on how to better use your online presence" />Consultancy on how to better use your online presence<br />
    <input type="radio" name="9" value="Consultancy on Search Engine Optimisation and Internet Marketing" />Consultancy on Search Engine Optimisation and Internet Marketing
    
    <p>9) On a scale of 1 to 5, to what degree would you say you understand the term "digital marketing"?</p>
    <input type="radio" name="10" value="1" />1
    <input type="radio" name="10" value="2" />2
    <input type="radio" name="10" value="3" />3
    <input type="radio" name="10" value="4" />4
    <input type="radio" name="10" value="5" />5
    Other (please specify) <textarea name="10"></textarea>
    
    <p>10) Please indicate, by ticking the appropriate boxes, the elements you think that makes a strong web design firm:</p>
    <input type="checkbox" name="11[]" value="Strong portfolio" />Strong portfolio<br />
    <input type="checkbox" name="11[]" value="Wide range of services" />Wide range of services<br />
    <input type="checkbox" name="11[]" value="Rapid turnaround of projects" />Rapid turnaround of projects<br />
    <input type="checkbox" name="11[]" value="Design awards for their websites" />Design awards for their websites<br />
    <input type="checkbox" name="11[]" value="Revavant accreditations and awards" />Revavant accreditations and awards<br />
    <input type="checkbox" name="11[]" value="Evidence of results" />Evidence of results<br />
    <input type="checkbox" name="11[]" value="Frequent communication with you" />Frequent communication with you<br />
    <input type="checkbox" name="11[]" value="Strong differentiation from completion" />Strong differentiation from completion
    
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
    </body>
    </html>

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    You'll have to change these:

    <textarea name="6"></textarea>
    <textarea name="10"></textarea>


    To these (put a 't' in front of the number) ...

    <textarea name="t6"></textarea>
    <textarea name="t10"></textarea>

    Then, add those variable names to your PHP form ...
    $t6=$_POST['t6'];
    $t10=$_POST['t10'];

    And work with them as the names you've given them.


  •  

    Posting Permissions

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