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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Location
    Brentwood, TN
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Return values for Checkboxes with same name

    I pulled the following code and created a project cost estimating tool for a carpet cleaning business.

    Each name="num" is labeled with a room for the house and I created a second piece of code so that name="num1" determines if they want carpet protectant. (site is http://www.pcsnashville.com/Estimator.php)

    How can I return which rooms are checked, which rooms want protectant and along with similar info for number of stairs.

    (big time neophite here, thanks.)

    Original post: glenngv03-02-2004, 01:53 AM

    Origianl Reply: Nitro2 wrote on 03-01-2004 11:36 PM:
    ...

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function calculate(f)
    {
    var nums = f.num;
    var ntext = f.numtext;
    var nitem = f.numitem;
    var result = 0;
    var items = '';
    for(var i=0;i<nums.length;i++)
    {
    if(nums[i].checked)
    {
    result+=parseFloat(ntext[i].value);
    items+=nitem[i].value+'\n';
    }
    }
    f.answer.value=result; 
    
    //if you want to fix to 2 decimal places
    //f.answer.value=Number(result).toFixed(2);
    
    f.allitems.value=items;
    }
    </script>
    </head>
    <body>
    <form name="myform">
    Which numbers do you want to add?<br>
    <input type="checkbox" name="num" onclick="calculate(this.form)"><input type="text" name="numtext" value="1.50" onchange="calculate(this.form)"><input type="text" name="numitem" value="pencil" onchange="calculate(this.form)"><br>
    <input type="checkbox" name="num" onclick="calculate(this.form)"><input type="text" name="numtext" value="2.00" onchange="calculate(this.form)"><input type="text" name="numitem" value="pen" onchange="calculate(this.form)"><br>
    <input type="checkbox" name="num" onclick="calculate(this.form)"><input type="text" name="numtext" value="3.20" onchange="calculate(this.form)"><input type="text" name="numitem" value="paper" onchange="calculate(this.form)"><br>
    Total <input type="text" name="answer" size="5"><textarea name="allitems" rows="5"></textarea>
    </form>
    </body>
    </html>

  • #2
    New to the CF scene
    Join Date
    Aug 2008
    Location
    Brentwood, TN
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here's the code I am using

    Code:
    // -->
    
    function calculate(f)
    { 
    var nums = f.num;
    var ntext = f.numtext;
    var res = 0;
    var items = '';
    for(var i=0;i<nums.length;i++)
    {
    if(nums[i].checked)
    {
    res+=parseFloat(ntext[i].value);
    }
    }
    f.answer.value=Number(res).toFixed(2);
    }
    
    function calculate1(f1)
    { 
    var nums1 = f1.num1;
    var ntext1 = f1.numtext1;
    var test = 0;
    var items1 = '';
    for(var j=0;j<nums1.length;j++)
    {
    if(nums1[j].checked)
    {
    test+=parseFloat(ntext1[j].value);
    }
    }
    f1.answer1.value=Number(test).toFixed(2);
    }
    function area(form) {
    a=eval(form.answer.value)
    b=eval(form.answer1.value)
    s=eval(form.steps.value)
    {
    if(b==undefined)
    {
    form.silver.value = a+s*5
    form.gold.value = a*(45/35)+s*5
    form.platinum.value = a*(55/35)+s*5
    }
    else
    {
    
    form.silver.value = a+b+s*5
    form.gold.value = a*(45/35)+b+s*5
    form.platinum.value = a*(55/35)+b+s*5
    
    }
    }
    }
    // End -->
    Last edited by AKmiecik; 08-10-2008 at 12:36 PM.

  • #3
    New Coder
    Join Date
    Aug 2008
    Posts
    13
    Thanks
    1
    Thanked 1 Time in 1 Post
    Easiest way to return what they want is use something like this:

    PHP Code:
    <?php
    if(isset($_POST['submit'])) {
    if((!
    $_POST['checkbox'])) {
    echo (
    'you haven't checked any of the boxes!');
    }
    } else {
    $checkbox = addslashes(htmlspecialchars($_POST['
    checkbox']));
    $insert = mysql_query("INSERT INTO `table` (`checkbox`) VALUES('
    $checkbox')");
    } else {
    echo ('
    <form method='post' action='$_SERVER[PHP_SELF]'>
    <
    input type='checkbox' name='checkbox' value='room1'>
    <
    input type='checkbox' name='checkbox' value='room2'>
    <
    input type='submit' name='submit' value='Submit'>
    </
    form>);
    }
    ?>
    Then for grabbing the info of what they have submitted:
    PHP Code:
    <?php
    $grab 
    mysql_query("SELECT * FROM `table`");
    while (
    $display mysql_fetch_array($grab)) {
    echo (
    'Room number: $display');
    }
    ?>
    Thats how I'd do it anyway in FULL php, you can change the values
    What the first bit does is submit the value of the checkbox to the server so you can view it.
    The second bit loops all of the inputs to the server and displays them using the while() statement.

  • Users who have thanked ljr747 for this post:

    AKmiecik (08-10-2008)

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    a typo:
    PHP Code:
    echo ('you haven't checked any of the boxes!); 
    must be:
    PHP Code:
    echo ('you haven\'t checked any of the boxes!');
    // or:
    echo ("you haven't checked any of the boxes!"); 
    you must check results returned by mysql_query.

    regards

  • #5
    New to the CF scene
    Join Date
    Aug 2008
    Location
    Brentwood, TN
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Changing the code like that is not a great opition since I have added logic to do calculations (i..e. involve big rewrite) and I am currently using the "value'=" to hold the cost of cleaning that room.


    I'll give it a try, maybe I'll learn something.
    Last edited by AKmiecik; 08-10-2008 at 12:45 PM.

  • #6
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    I think the easiest way is

    Code:
    $room1 = $_POST["num1"];
    $room2 = $_POST["num2"]; //etc
    
    //Then if it emails you the results have it say like
    
    echo "Your customer has chosen $room1,$room2";
    That way it'd be blank if they didn't select it, and show a room number if they did.

  • #7
    New to the CF scene
    Join Date
    Aug 2008
    Location
    Brentwood, TN
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How can I return values of a Textarea using PHP

    Thanks for the suggestion masterofollies but it seems like your solution requires each input tag to have a unique name and they do not.

    Progress(?):

    The problem has evolved to the point where, by using another input tag for each checkbox on the form (as I noticed the original code has), I can populate a Textarea (in the form) with the a unique value for each box checked.

    So now I have two Textareas, each with a set of checkboxes that are selected, now I need to send the values in the Textareas via the submit php file.

    Any ideas on how to do that?
    Last edited by AKmiecik; 08-18-2008 at 11:00 PM.


  •  

    Posting Permissions

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