SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Enthusiast am_dorky's Avatar
    Join Date
    Sep 2008
    Location
    Bucks
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question on submit, if checkbox ticked pass value php/javascript

    Hi Guys,

    Wasn't sure if I should post within PHP forum or Javascript so let me know if this is in the wrong place.

    I have a form with a checkbox "labour" and button "view summary". The labour is pulled from my database, (php and mysql) as different labour is added to items depending on the item.

    I have a javascript function for the checkbox that says if checkbox ticked add labour to total else don't add labour to total.

    I need something so when I click on the "view summary" button, if checkbox is ticked add labour to next page and add labour to total on next page, else don't.

    I'm not sure how I'd go about doing this as I don't know javascript well. Would I need to add something to the javscript I already have?
    Do I pass the labour value as normal via php using $_POST as this is what I'm doing with other values?
    Do I need some javascript on my summary.php which is where I want to pass the labour cost to?

    Here is my code:
    Code JavaScript:
    var oldTotal=0;
    function showlabour(total,labour) {
    	//alert(total);
    	if(document.getElementById('labour').checked == true){
    		total = parseFloat(total) + labour;
    		oldTotal = total;
    		total = formatNumber(total,'2',',','.','','','','');
    		document.getElementById('total').innerHTML = '<b>&pound;'+total+'</b>';
    	}else{
    		total = parseFloat(oldTotal) - labour;
    		oldTotal = total;
    		total = formatNumber(total,'2',',','.','','','','');
    		document.getElementById('total').innerHTML = '<b>&pound;'+total+'</b>';
    	}
    }
     
    function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
    	var x = Math.round(num * Math.pow(10,dec));
    	if (x >= 0) 
    	n1=n2='';
    	var y = (''+Math.abs(x)).split('');
    	var z = y.length - dec; 
    	if (z<0) 
    	z--; 
    	for(var i = z; i < 0; i++) 
    	y.unshift('0');
    	y.splice(z, 0, pnt); 
    	if(y[0] == pnt) 
    	y.unshift('0'); 
    	while (z > 3) 
    	{
    		z-=3; 
    		y.splice(z,0,thou);
    	}
    	var r = curr1+n1+y.join('')+n2+curr2;
    	return r;
    }

    Code PHP:
    echo '<input id='labour' type='checkbox' onclick='showlabour($jsTotal , $totallabourcost );' />'
    echo ', $totallabour, ';
    echo '<input type="button" name="button1" value="update" onclick="return OnButton1();" />';

    Also, I have 1 form with 2 buttons. an update and view summary so due to this the view summary button has some javascript that tells it to go to the next page.

    Code JavaScript:
    function OnButton2() { 
        document.form1.action = "summary.php" 
        document.form1.submit();
        return true; 
    }

    Any advice and help would be great.

    Many thanks

  2. #2
    SitePoint Enthusiast stef686's Avatar
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would create a hidden form field to hold the total and update the value of that with the javascript when you need to. Then on the next page you can use the value that's been submitted in the hidden field.

  3. #3
    SitePoint Enthusiast am_dorky's Avatar
    Join Date
    Sep 2008
    Location
    Bucks
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    I'm not too sure that I follow. By doing your suggestion that will only allow me to print the total with labour added. On my summary page I want labour showing as seperate cost and total showing with labour included if client has ticked box, otherwise total shows without labour added and labour doesn't show at all.

    Have i missunderstood you?

  4. #4
    SitePoint Enthusiast am_dorky's Avatar
    Join Date
    Sep 2008
    Location
    Bucks
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    I think I need something saying if checked do this else do this. How do I go about doing that?

  5. #5
    SitePoint Enthusiast stef686's Avatar
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK if I follow correctly you pass the total and the labour costs to the javascript from php variables? If so I don't think you need to do anything else with the javascript for the next page.

    If you give your checkbox a name and a value you can use it in your php on the next page to check if it has been clicked or not:

    PHP Code:
    if ( $_POST['labour'] ) {
    //add labour to the total
    } else {
    //dont


  6. #6
    SitePoint Enthusiast am_dorky's Avatar
    Join Date
    Sep 2008
    Location
    Bucks
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by stef686 View Post
    OK if I follow correctly you pass the total and the labour costs to the javascript from php variables?
    Yeah I do. I think I am on the right track with this. Got one problem tho. When checkbox is checked its displaying labour but total value is an empty space. Do you know why this might be?

    Code below:

    labour on estimate.php
    PHP Code:
    echo "<input id='labour' type='checkbox' name='checkbox1' value='checkbox1' onclick='showlabour($jsTotal , $totallabourcost );' />"
    summary.php
    PHP Code:
    if ( $_POST['checkbox1'] ) { 
    //add labour to the total 
    $total=$total+$totallabour;
    echo 
    "labour";
    echo 
    $_POST ["totallabour"];
    echo 
    "total";
    echo 
    $_POST ["total"];
    } else { 
    //dont 
    echo "Total";
    echo 
    $_POST ["total"];


  7. #7
    SitePoint Enthusiast am_dorky's Avatar
    Join Date
    Sep 2008
    Location
    Bucks
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    I think I might know why this isn't working. The checkbox has a sum (written in javascript) attached to it (see below).

    I think I need to pass the total value +total+ back out from the javascript to php and then pass it to the next page. I know this can be done using cookies but not sure how. Can anyone help please?

    Code JavaScript:
    var oldTotal=0;
    function showlabour(total,labour) {
    if(document.getElementById('labour').checked == true) {
    total = parseFloat(total) + labour;
    oldTotal = total;
    total = formatNumber(total,'2',',','.','','','','');
    document.getElementById('total').innerHTML = '<b>&pound;'+total+'</b>';
    }else {
    total = parseFloat(oldTotal) - labour;
    oldTotal = total;
    total = formatNumber(total,'2',',','.','','','','');
    document.getElementById('total').innerHTML = '<b>&pound;'+total+'</b>';
    }
    }
     
    function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
    var x = Math.round(num * Math.pow(10,dec));
    if (x >= 0) 
    n1=n2='';
    var y = (''+Math.abs(x)).split('');
    var z = y.length - dec; 
    if (z<0) 
    z--; 
    for(var i = z; i < 0; i++) 
    y.unshift('0');
    y.splice(z, 0, pnt); 
    if(y[0] == pnt) 
    y.unshift('0'); 
    while (z > 3) 
    {
    z-=3; 
    y.splice(z,0,thou);
    }
    var r = curr1+n1+y.join('')+n2+curr2;
    return r;
    }


Bookmarks

Posting Permissions

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