#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0

    Red face + Shipping Fee (QUESTION FOR PHP)


    Hello PHP Community,

    I have been eagerly searching the web for clues on how to implement this seemlingly easy function. I have attempted it myself many times but now I must resort to my final option, which whould be to ask you guys!

    I have a very simple shopping cart I made for my website, but I can't seem to get this extra shipping fee (drop down menu) to add to the total when selected.

    Here is a picture to better visualize:
    http://imageshack.us/photo/my-images/811/helpmg.jpg/

    If anyone knows how I can add the $15.00 to the TOTAL when selected (via drop down menu), please let me know. It would be greatly appreciated!
  2. #2
  3. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    we'd need to see your code to be more precise, but in a nutshell you would use javascript to modify the page (or use an ajax call) when the dropdown is changed to add or subtract the $15 from the total. I assume that there is a processing page that is called by your form, on that page you would check to see which shipping method was selected and adjust the total accordingly.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by sir_drinxalot
    we'd need to see your code to be more precise, but in a nutshell you would use javascript to modify the page (or use an ajax call) when the dropdown is changed to add or subtract the $15 from the total. I assume that there is a processing page that is called by your form, on that page you would check to see which shipping method was selected and adjust the total accordingly.
    Sure thing, here is my codes:

    PHP:

    PHP Code:
    <?php 
    session_start
    ();

    //include "scripts/mysql.php"; 

    if ((!isset($_SESSION['step'])) || ($_SESSION['step'] < 3)) {
          
    header("location:x.php");
        exit(); 
    }

    $_SESSION['step'] = 4;

    define('STEP'3);

    $step '';
    $step $_SESSION['step'];

    $_SESSION['plan'];

    if ((!isset(
    $_SESSION['fullname'])) || ($_SESSION['email']) || ($_SESSION['phone'])) {

        } else {
        
    $_SESSION['fullname'] = $_POST['fullname'];
        
    $_SESSION['email'] = $_POST['email'];
        
    $_SESSION['phone'] = $_POST['phone'];
        }
        

    $pname='';
    $pcost='';
    $psong='';
    $total='';
    $nxtday="0";

            if (
    $_SESSION['plan'] == 'plan1') {
                
    $pname "Single";
                
    $pcost "89";
                
    $psong "1";
            } else if (
    $_SESSION['plan'] == 'plan2') {
                
    $pname "Demo";
                
    $pcost "240";
                
    $psong "3";
            } else if (
    $_SESSION['plan'] == 'plan3') {
                
    $pname "EP";
                
    $pcost "415";
                
    $psong "4-6";
            } else if (
    $_SESSION['plan'] == 'plan4') {
                
    $pname "Full";
                
    $pcost "575";
                
    $psong "10-15";
            } else {
                
    $pname "ERROR";
                
    $pcost "ERROR";
                
    $psong "ERROR";
            }


    $total $pcost $nxtday;

    ?>
    HTML:

    Code:
                                            
    						<div style="padding-top: 120px;">
    							<table width='100%' border='1' bordercolor="#eaeaea" cellspacing='0' cellpadding='6'>
    							<tr>
    							<td width='12%' bgcolor='#e0e0e0'><div align='center' class='cart'>Product</div></td>
    							<td width='25%' bgcolor='#e0e0e0'><div align='center' class='cart'>Service Name</div></td>
    							<td width='10%' bgcolor='#e0e0e0'><div align='center' class='cart'>Songs</div></td>
    							<td width='10%' bgcolor='#e0e0e0'><div align='center' class='cart'>Quantity</div></td>
    							<td width='10%' bgcolor='#e0e0e0'><div align='center' class='cart'>Price</div></td>
    							<td width='10%' bgcolor='#e0e0e0'><div align='center' class='cart'>Change</div></td>
    							<tr>
    							<td bgcolor='#e5e5e5'><div align='center' class='cart'><img src="images/white_r.png" alt="" width="51" height="52" /></div></td>
    							<td bgcolor='#e5e5e5'><div align='center' class='cart'><?php echo $pname; ?> Mastering Service</div></td>
    							<td bgcolor='#e5e5e5'><div align='center' class='cart'><?php echo $psong; ?></div></td>
    							<td bgcolor='#e5e5e5'><div align='center' class='cart'>1</div></td>
    							<td bgcolor='#e5e5e5'><div align='center' class='cart'>$<?php echo $pcost; ?></div></td>
    							<td bgcolor='#e5e5e5'><div align='center' class='cart'><a href="s2.php"><img src="images/changeicon.png" alt="" width="52" height="52" /></a></div></td>
    							</tr>
    							</tr>
    							</table>
    						</div>
    
    							<div style="float:right;margin-top: 5px;">         
    								<div style="float:right;width: 720px; height:16px; background-image:url(images/total_bg.png);"><font style="float:right; margin-right: 5px;">Subtotal: $<?php echo $pcost; ?></font></div><br /></div> 
    									<div style="margin-top: 10px; margin-bottom: 10px; float:right; width: 720px;"> 
    										<select id="" style="float:right;" name='Delivery Speed'> 
    										<option selected="selected">FREE - Standard (2-3 business days)</option> 
    										<option value="nxtday" id="nxtday">$15.00 - Next Day</option> 
    										</select><br />
    									</div>
        
    									<div style="float:right;width: 720px; height:16px; background-image:url(images/total_bg.png);"><font style="float:right; margin-right: 5px;">Total: $<?php echo $total; ?></font></div><br /></div>
  6. #4
  7. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    so in your php you have $nxtday="0";

    you should be able to set that to either 0 or 15 depending on what was selected in the drop down. If that drop down changes you can use ajax to make a call back to the server to update everything and seamlessly change the html to reflect the new total.

    hope that makes sense.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by sir_drinxalot
    so in your php you have $nxtday="0";

    you should be able to set that to either 0 or 15 depending on what was selected in the drop down. If that drop down changes you can use ajax to make a call back to the server to update everything and seamlessly change the html to reflect the new total.

    hope that makes sense.
    Is there a way to do it with PHP? Because I do not know of Java.
  10. #6
  11. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    yes, very easily. From the look of the image you provided in your first post, paypal is the next step. You might be able to get paypal to handle it (i don't know enough about paypal to say for certain).

    The other way would be to add an extra step in, so that users select their shipping service, the click a 'next' button which will send the value of the drop down to the php script, then you can access it as part of the $_POST[] array. I can't see any <form> tag in your html either, so if it is not in there, you will need to add something like <form action="myscriptname.php" method="post"> so that the browser knows what to do with the data.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by sir_drinxalot
    yes, very easily. From the look of the image you provided in your first post, paypal is the next step. You might be able to get paypal to handle it (i don't know enough about paypal to say for certain).

    The other way would be to add an extra step in, so that users select their shipping service, the click a 'next' button which will send the value of the drop down to the php script, then you can access it as part of the $_POST[] array. I can't see any <form> tag in your html either, so if it is not in there, you will need to add something like <form action="myscriptname.php" method="post"> so that the browser knows what to do with the data.
    Ok, I am having a bit of trouble here... Here is my code in short form:

    <form action="https://www.paypal.com/cgi-bin/webscr" method="POST">
    <select id="delivery_speed" style="float:right;" name='delivery_speed'>
    <option selected="selected">FREE - Standard (2-3 business days)</option>
    <option>$15.00 - Next Day</option>
    </select>
    </form>


    I'd imagine that if the options we're values themselves, it would be much easier to do. For example, <option>15</option>
    And then just add the $POST_['delivery_speed'] to subtotal to get the total. However, Since the option is the words "$15.00 - Next Day" I can't add that as a value. Instead, I would need to some how attach a value to that option. My logic does not seem to be working in my favor.

    Here is what I am thinking...

    <select id="delivery_speed" name='delivery_speed'>
    <option value="free">FREE - Standard (2-3 business days)</option>
    <option value="nxtday">$15.00 - Next Day</option>
    </select>


    and have this in php...

    $free = "0";
    $nxtday = "15";


    Then have this...

    $delivery_speed = $_POST['delivery_speed'];
    $total = $sub + $delivery_speed;


    But this will not work.... All I want to happen is for when the user selects the "$15.00 - Next Day" in the drop down menu. That it would automatically calculate and add $15 to the total, and if the user selects "FREE - Standard (2-3 business days)" it would add $0 to the total...
  14. #8
  15. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    Close... you would keep the html as you have it:

    Code:
    <select id="delivery_speed" name='delivery_speed'> 
    <option value="free">FREE - Standard (2-3 business days)</option>
    <option value="nxtday">$15.00 - Next Day</option>
    </select>
    and then this in the php

    PHP Code:
    if($_POST['delivery_speed'] == "free") {
    $total $sub;
    } else {
    $total $sub 15;

    or

    PHP Code:
    if($_POST['delivery_speed'] == "free") {
    $postage 0;
    } else {
    $postage 15;
    }
    $total $sub $postage
    something like that should do what you want.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by sir_drinxalot
    Close... you would keep the html as you have it:

    Code:
    <select id="delivery_speed" name='delivery_speed'> 
    <option value="free">FREE - Standard (2-3 business days)</option>
    <option value="nxtday">$15.00 - Next Day</option>
    </select>
    and then this in the php

    PHP Code:
    if($_POST['delivery_speed'] == "free") {
    $total $sub;
    } else {
    $total $sub 15;

    or

    PHP Code:
    if($_POST['delivery_speed'] == "free") {
    $postage 0;
    } else {
    $postage 15;
    }
    $total $sub $postage
    something like that should do what you want.
    For some reason, the total keeps echoing out 15 no matter what option i select. I also tried both of your codes. I don't understand why it won't simply add the subtotal with 15?... The subtotal is echoing out just fine though. Here's a pic..
    http://i78.photobucket.com/albums/j89/Savesx/image_zps06dbed4f.png

IMN logo majestic logo threadwatch logo seochat tools logo