Jump to content

radio button option posted to a backend php program


helen11

Recommended Posts

I would like the radio button option from a html form posted to a backend php program along with a simple calculation, I can get the calculator to work but I cant get it to post the radio button value. Any help would be greatly appreciated.

 

<head>
<title></title>
</head>
<body>
<h1>Simple Calculator</h1>


<h2>Please Select an Option</h2>
<form method="post" action="calculation.php">
<div>
<input type="radio" name="calcu" value="Highest"> Highest<br>
<input type="radio" name="calcu" value="Lowest" checked> Lowest<br>
<input type="radio" name="calcu" value="Average"> Average<br>
<input type="radio" name="calcu" value="Sort in ascending order"> Sort in ascending order<br>
</div>
</form>

<h2>Insert five numbers in the form and hit submit button</h2>

<form method="post" action="calculation.php">

<p>Value 1: <input type="text" name="val1" size="10"></p>
<p>Value 2: <input type="text" name="val2" size="10"></p>
<p>Value 3: <input type="text" name="val3" size="10"></p>
<p>Value 4: <input type="text" name="val4" size="10"></p>
<p>Value 5: <input type="text" name="val5" size="10"></p>
<p>Calculation:<br>
<input type="radio" name="calc" value="add"> add<br>
<input type="radio" name="calc" value="subtract">  subtract<br>
<input type="radio" name="calc" value="multiply"> multiply<br>
<input type="radio" name="calc" value="divide"> divide</p>
<p><input type="submit" name="submit" value="Calculate"></p>
</form>
</body>
</html>

 

<head>
<title>Simple Calculator</tite>
</head>
<body>
<?php

if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[val3] == "") || ($_POST[val4] == "") || ($_POST[val5] == "") || ($_POST[calc] =="")) {
  header("Location: form.html");
  exit;
}
if ($_POST[calc] == "add") {
  $result = $_POST[val1] + $_POST[val2] + $_POST[val3] + $_POST[val4] + $_POST[val5];
} else if ($_POST[calc] == "subtract") {
  $result = $_POST[val1] - $_POST[val2] - $_POST[val3] - $_POST[val4] - $_POST[val5];
} else if ($_POST[calc] == "multiply") {
  $result = $_POST[val1] * $_POST[val2] * $_POST[val3] * $_POST[val4] * $_POST[val5];
} else if ($_POST[calc] == "divide") {
  $result = $_POST[val1] / $_POST[val2] / $_POST[val3] / $_POST[val4] / $_POST[val5];
}
echo "<title>Calculation Result</title>";
echo "<p>The result of the calculation is: $result</p>";
echo "<p><a href=\"form.html\" target=\"_self\">Do Another</a></p>";
?>



</body>
</html>

Link to comment
Share on other sites

See if calc is an element in the $_POST array.

 

echo '<br>Start POST Array:<pre>';
print_r($_POST);
echo '</pre>End POST array<br>';

 

And while developing, you should always have error reporting on. Do so by editing the proper php.ini file directives to:

error_reporting = -1

display_errors = On

 

Then restart Apache.

Link to comment
Share on other sites

I'm not talking about the calculation text boxes that part works, when 5 numbers are put in the text boxes and calculated it returns the answer. What I want to know is how do I get the chosen radio button option posted to the backend php program?

Link to comment
Share on other sites

See if calc is an element in the $_POST array.

 

echo '<br>Start POST Array:<pre>';
print_r($_POST);
echo '</pre>End POST array<br>';

 

And while developing, you should always have error reporting on. Do so by editing the proper php.ini file directives to:

error_reporting = -1

display_errors = On

 

Then restart Apache.

 

Did you do this? Was 'calc' an element in the $_POST array? Did you compare the error notice to what was actually in the $_POST array?

Link to comment
Share on other sites

Sounds like homework.

 

In any case, your two radio button sets need to processed as either/or because you can't for example show the highest while also getting the sum.  For this reason, I would name them all the same.  You have radio buttons above and below text inputs and so to make the display less confusing, I would place them all at the top and label them as a group side-by-side.  A few floating divisions should do the job.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<h2>Please Select an Option</h2>
<form method="post" action="calculation.php">
	<div style="float:left;">
		<div style="float:left; padding-right:10px">
			<h3>Sort Options</h3>
			<input type="radio" name="calc" value="Highest" /> Highest<br />
			<input type="radio" name="calc" value="Lowest" /> Lowest<br />
			<input type="radio" name="calc" value="Average" /> Average<br />
			<input type="radio" name="calc" value="Sort in ascending order" /> Sort in ascending order<br />
		</div>
		<div style="float:left;">
		<h3>Calculation Options</h3>
			<input type="radio" name="calc" value="add" /> add<br />
			<input type="radio" name="calc" value="subtract" />  subtract<br />
			<input type="radio" name="calc" value="multiply" /> multiply<br />
			<input type="radio" name="calc" value="divide" /> divide<br />
		</div>
	</div>
	<div style="float:left; clear:both;">
		<h2>Insert five numbers in the form and hit submit button</h2>

		<p>Value 1: <input type="text" name="val1" size="10" /></p>
		<p>Value 2: <input type="text" name="val2" size="10" /></p>
		<p>Value 3: <input type="text" name="val3" size="10" /></p>
		<p>Value 4: <input type="text" name="val4" size="10" /></p>
		<p>Value 5: <input type="text" name="val5" size="10" /></p>
		<p><input type="submit" name="submit" value="Calculate" /></p>
	</div>
</form>
</body>
</html>

 

For processing, it's better to use

if (empty($_POST['val1'])

rather than

if ($_POST[val1] == "")

Also note, as already mentioned how the $_POST keys are in single quotes, i.e. ['val1'].

 

When dealing with radio buttons, unless you've added a default selection as blank...

<input type="radio" name="calc" value="" checked="checked" /> Choose

...then checking for blank is pointless.

$_POST[calc] ==""

Instead check if the value isset().

if (isset($_POST['calc'])){

 

header() should always be called before anything is sent to the browser, ex. <head> etc. and in general it's a good practice to always do any post-processing before output, so get in the habit of setting up your pages this way.  In fact in a case like this project, I would put processing above <html> on the same page and make the form fields sticky (echo post values back) so it the user forgets to make a selection, the values are still in the form and you can display a message like "Please select a processing option".

 

As far as the sorting options, I think adding posted values to an array then sorting is the way to go.

 

Because I have processing above <body> tag, I will format result output as a variable and echo within body.

 

So in any case, here's my version.

<?php
if (empty($_POST['val1']) || empty($_POST['val2']) || empty($_POST['val3']) || empty($_POST['val4']) || empty($_POST['val5'])) {
  header("Location: form.html");
  exit;
}
if (!isset($_POST['calc'])){
  header("Location: form.html");
  exit;
}else{
if (isset($_POST['calc'])){
	if ($_POST['calc'] == "add") {
	  $result = $_POST['val1'] + $_POST['val2'] + $_POST['val3'] + $_POST['val4'] + $_POST['val5'];
	} elseif ($_POST['calc'] == "subtract") {
	  $result = $_POST['val1'] - $_POST['val2'] - $_POST['val3'] - $_POST['val4'] - $_POST['val5'];
	} elseif ($_POST['calc'] == "multiply") {
	  $result = $_POST['val1'] * $_POST['val2'] * $_POST['val3'] * $_POST['val4'] * $_POST['val5'];
	} elseif ($_POST['calc'] == "divide") {
	  $result = $_POST['val1'] / $_POST['val2'] / $_POST['val3'] / $_POST['val4'] / $_POST['val5'];
	} elseif ($_POST['calc'] == "Highest") {
		  $total= array();
		  $total[] =$_POST['val1'];
		  $total[] =$_POST['val2'];
		  $total[] =$_POST['val3'];
		  $total[] =$_POST['val4'];
		  $total[] =$_POST['val5']; 
		  $result = max($total);
	} elseif ($_POST['calc'] == "Lowest") {
		  $total= array();
		  $total[] =$_POST['val1'];
		  $total[] =$_POST['val2'];
		  $total[] =$_POST['val3'];
		  $total[] =$_POST['val4'];
		  $total[] =$_POST['val5']; 
		  $result = min($total);
	} elseif ($_POST['calc'] == "Average") { 			  
		  $result = (($_POST['val1'] + $_POST['val2'] + $_POST['val3'] + $_POST['val4'] + $_POST['val5'])/5);
	} elseif ($_POST['calc'] == "Sort in ascending order") {
		  $numbers= array();
		  $numbers[] =$_POST['val1'];
		  $numbers[] =$_POST['val2'];
		  $numbers[] =$_POST['val3'];
		  $numbers[] =$_POST['val4'];
		  $numbers[] =$_POST['val5'];
		  asort($numbers); 
		  $result = implode(', ',$numbers);
	}
}
}
$display ="";
$display .="<p>The result of the calculation is: $result</p>";
$display .="<p><a href=\"form.html\" target=\"_self\">Do Another</a></p>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Calculation Result</title>
</head>
<body>
<?php 
if (isset($display)){ echo "$display"; }
?>
</body>
</html>

Link to comment
Share on other sites

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.