Jump to content

I'm a beginner who needs help. READING CHECKBOXES and TOTALLING THEM UP.


Recommended Posts

A user is supposed to check the boxes on what they want in their order(checkdemo.html). Then when they submit it, the php file should tell them what they ordered and also it is supposed to total up the cost of all the selected items(checkdemo.php).

 

Screenshot of checkdemo.html: checkhtml.png

 

THIS IS CHECKDEMO.HTML

<html>
<head>
<title>Checkbox Demo</title>
</head>
<body>
<h1>Checkbox</h1>

<h3>Demonstrates Checkboxes</h3>

<form action = "checkDemo.php">

<h3>What would you like with your order?</h3>
<ul>
<li><input type = "checkbox"
name = "chkFries"
value = "1.00">Fries
</li>
<li><input type = "checkbox"
name = "chkSoda"
value = ".85">Soda
</li>

<li><input type = "checkbox"
name = "chkShake"
value = "1.30">Shake
</li>

<li><input type = "checkbox"
name = "chkKetchup"
value = ".05">Ketchup
</li>



</ul>

<input type="submit">
</form>

</body>
</html>

 

THIS IS CHECKDEMO.PHP

<html>
<head>
<title>Checkbox Demo</title>
</head>
<body>
<h3>Demonstrates reading checkboxes</h3>

<?

print <<<HERE

chkFries: $chkFries <br>
chkSoda: $chkSoda <br>
chkShake: $chkShake <br>
chkKetchup: $chkKetchup <br>
<hr>

HERE;


$total = 0;

if (!empty($chkFries)){
print ("You chose Fries <br> \n");
$total = $total + $chkFries;
} // end if

if (!empty($chkSoda)){
print ("You chose Soda <br> \n");
$total = $total + $chkSoda;
} // end if

if (!empty($chkShake)){
print ("You chose Shake <br> \n");
$total = $total + $chkShake;
} // end if

if (!empty($chkKetchup)){
print ("You chose Ketchup <br> \n");
$total = $total + $chkKetchup;
} // end if

print "The total cost is \$$total \n";

?>
</body>
</html>

 

Here is what comes up when I submit the html file (having all checkboxes checked: checkphp.png

1. In the checkdemo file

<?

$chkFries = $_POST['chkFries'];

/// do this for every checkbox or any other form widgets you choose to make.

print <<<HERE

 

2. to your form tag add:

method="POST"

 

3. examine some form tutorials. You need to understand the logic and various methods used to process forms.

 

 

HTH

Teamatomic

I believe your form checkboxes should have the same name like name="order" and your value should define the value of the checkbox, value="fries".

<form method="post" action="do_form.php>
<input type="checkbox" name="order[]" value="fries"> Fries
<input type="checkbox" name="order[]" value="hamburger"> Hamburger
<input type="checkbox" name="order[]" value="coke"> Coke
<input type="submit" name="submit" value="SUBMIT ORDER">

This form is very simple html. Then you should create a php script that would process the order searching a database table for the field of fries, then selecting a field in that row which would have the price of that product.

 

See if this gets you started in the right direction. Good Luck.

1. In the checkdemo file

<?

$chkFries = $_POST['chkFries'];

/// do this for every checkbox or any other form widgets you choose to make.

print <<<HERE

 

2. to your form tag add:

method="POST"

 

3. examine some form tutorials. You need to understand the logic and various methods used to process forms.

 

 

HTH

Teamatomic

 

When I do all that and rerun it, I get an error:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in E:\indigoampp\apache-2.2.11\htdocs\ph03\checkDemo.php on line 12

hello...i have created a PHP script where you can sum up accordingly and display the result ....without using a database....it's workin for me juss check am giving you the page created using dreamweaver...

 

?php

$x=$_POST;

$t=0;

if(isset($_POST[take])){

if($x['dosa']){

$p=25;

$t=$t+$p;

}

if($x['vada']){

$q=30;

$t=$t+$q;

}

if($x['paw']){

$r=10;

$t=$t+$r;

}

echo "your total is".$t ;

}

 

?>

 

<!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=iso-8859-1" />

<title>Untitled Document</title>

</head>

<body>

 

<div align="center">

  <form id="form1" name="form1" method="post" action="">

    <p> </p>

    <p>

      <label></label>

    </p>

    <table width="323" border="0.2">

      <tr>

        <td width="317">What will you like to have </td>

      </tr>

    </table>

    <table width="325" height="94" border="0.2">

     

        <tr><td width="97">Masala dosa </td>

        <td width="218"><input type="checkbox" name="dosa" value="checkbox" /></td>

      </tr>

      <tr>

        <td>Vada</td>

        <td><input type="checkbox" name="vada" value="checkbox" /></td>

      </tr>

      <tr>

        <td>Paw Bhaji </td>

        <td><input type="checkbox" name="paw" value="checkbox" /></td>

      </tr>

    </table>

    <p>

      <input type="submit" name="take" value="Submit" />

    </p>

    <p> </p>

  </form>

</div>

</body>

</html>

 

i don't think is going to work for large number of check boxes because in that case you will have to write multiple if else statements which is very time consuming does anybody have any other way of doing it.....?/???

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.