Issues regarding form

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

jojo0122
New php-forum User
New php-forum User
Posts: 2
Joined: Sun Apr 20, 2014 11:19 am

Issues regarding form

Postby jojo0122 » Sun Apr 20, 2014 11:32 am

I created a form, which requires enter ticket number, if ticket number greater than 4 apply discount, else don't apply discount. Somehow,my code couldn't function well on discount "if" part. Please help.

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>VTA Bus Pass Order Form </title>
    </head>

    <body>
    <h2 style = "text-align:left">VTA Bus Pass Order Form</h2>
    <h3 style="text-align:left"> Please fill the order form, you will receive a 5% group discount if you order 5 or moare passes.</h3>
    <form name="form" action="vta.php"
    method="post">
    <table cellpadding="10" cellspacing="10">
    <thead>
    <tr>
    <th width="450" bgcolor="#999999">Passes</th>
    <th width="50" bgcolor="#999999">Quanity</th>
    </tr>
    </thead>

    <tr>
    <td>Adult($70)</td>
    <td><input type="text" name="Adult"/></td>
    </tr>

    <tr>
    <td>Adult Express($140)</td>
    <td><input type="text" name="Adult_Express" /></td>
    </tr>

    <tr>
    <td>Youth 5-17($45)</td>
    <td><input type="text" name="Youth"/></td>
    </tr>

    <tr>
    <td>Senior 55($25)</td>
    <td><input type="text" name="Senior"/></td>
    </tr>

    <tr>
    <td>Customer Name</td>
    <td><input type="text" name="Name"/></td>
    </tr>


    <td colspan="2" align="center"><input type="submit" name="Submit"value="Submit Order" />
    <td align="center">&nbsp;</td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    <h1 style = "text-align:left">VTA Bus Pass Order Form</h1>
    <?php
    $adult_price = $_POST['Adult'];
    $adult_express_price = $_POST['Adult_Express'];
    $youth_price = $_POST['Youth'];
    $senior_price = $_POST['Senior'];
    $dis = ($adult_express_price*140+$adult_price*70+$youth_price*45+$senior_price*25)*0.95;
    $sum = $adult_express_price*140+$adult_price*70+$youth_price*45+$senior_price*25;
    $name = $_POST['Name'];
    if (isset($_POST['Submit'])!=4)// If form ticket numbers > than 4, apply discount
    {
    echo "$ $dis after discount applied";

    }
    else
    {
    echo "$ $sum before discount applied";// don't apply discount.
    }

    echo "<p>Thank you, $name for ordering. Here is your confirmation</p>";
    echo "<p>$adult_price Adult pass</P>";
    echo "<p>$adult_express_price Adult Express</P>";
    echo "<P>$youth_price Student Pass</P>";
    echo "<p>$senior_price Senior Pass</P>"
    ?>
Attachments
input.PNG
input.PNG (19.92 KiB) Viewed 125 times
output.PNG
output.PNG (11.95 KiB) Viewed 125 times

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Issues regarding form

Postby seandisanti » Sun Apr 20, 2014 11:48 pm

It's your condition, isset($_POST['Submit']) will only return true or false, but you are looking for !=4 which would mean every possible result

jojo0122
New php-forum User
New php-forum User
Posts: 2
Joined: Sun Apr 20, 2014 11:19 am

Re: Issues regarding form

Postby jojo0122 » Mon Apr 21, 2014 9:57 am

I have used switch and case to determine, and also assigned the value to a variable to avoid Boolean, but turns out still not working well. Please help.




<h1 style = "text-align:left">VTA Bus Pass Order Form</h1>
<?php
$adult_price = $_POST['Adult'];
$adult_express_price = $_POST['Adult_Express'];
$youth_price = $_POST['Youth'];
$senior_price = $_POST['Senior'];
$dis = ($adult_express_price*140+$adult_price*70+$youth_price*45+$senior_price*25)*0.95;
$sum = $adult_express_price*140+$adult_price*70+$youth_price*45+$senior_price*25;
$name = $_POST['Name'];
$judge = isset($_POST['Adult'])+isset($_POST['Adult_Express'])+isset($_POST['Youth'])+isset($_POST['Senior']);
switch ($judge){// If form ticket numbers > than 4, applydiscount
case ($judge>4) :

echo "$ $dis after discount applied";
break;

case($judge<=4):

echo "$ $sum before discount applied";// don't apply discount. break;
}
echo "<p>Thank you, $name for ordering. Here is your confirmation</p>";
echo "<p>$adult_price Adult pass</P>";
echo "<p>$adult_express_price Adult Express</P>";
echo "<P>$youth_price Student Pass</P>";
echo "<p>$senior_price Senior Pass</P>"
?>

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Issues regarding form

Postby seandisanti » Mon Apr 21, 2014 11:39 pm

Assuming you are ok with 5 of any type of ticket, the first thing you need to do is to count your tickets. I would handle the count something like:

Code: Select all


$count 
0
foreach ($post as $key=>$value){
     if (
preg_match ('/Adult|Adult_Express|Youth|Senior/',$key) && is_numeric($value)){
          
$count += $value;
}
}
$discount = ($count>=5)?.05:0;

// code to figure total of tickets would go here

if ($discount){
     echo 
'For buying ' $count ' tickets with us today you received a 5% discount of ' round($discount $total,2);
     
$total -= $total $discount;
}

 
I just typed that up without testing it, but barring any typos it should work


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 4 guests

cron