Jump to content

Recommended Posts

Ok I got everything set up but when I click the calculate button, it doesn't do the calculating.  Also, should I make the last elseif statement the else statement? 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
if ($i = 2) {
	$price = 3.50; 
}
elseif ($i = 3) {
 	$price = 4.70; 
}
if ($i = 4) {
	$price = 5.90; 
}
elseif ($i = 5) {
 	$price = 7.00; 
}
elseif ($i = 6) {
	$price = 8.20; 
}
else {

}
function calculate($TotalSqFt, $price) {
	$total = $TotalSqFt + $price; 
	return $total; 	
}
?>  
<form action="" "" method="post">
<table>
    	<tr>  
		<td> total sq ft </td><td> <input name="TotalSqFt" type="text" size="4"></td><br />  
            <td>  number of cuts </td><td><select name="cuts" id="cuts">  <br>
<?php
  for ($i =2; $i <=6; $i ++)
	{
	echo "<option value=\"$i\"";
	if($cut == $i){
		echo ' SELECTED';
	}
	echo ">$i</option>"; 
}
?>   
</select>
</td>            
	</tr>
    <tr>  
    	<td> <input name="submit" type="submit" value="calculate"></td><td>  <?php calculate($TotalSqFt, $price); ?>  </td>		
    </tr>
    </table>
</form>
</body>
</html>

You are not defining $i, $TotalSqFt, or $price. This tells me that you either have register_globals on (BAD!) or you do not have error reporting on. So for starters, put this in the top of your script:

ini_set('display_errors', 1);
error_reporting(-1);

Only if you have the register_globals directive turned on, which, like I said, is BAD! Leaving register_globals on is a big security risk, which is why it is deprecated and being removed from future versions of PHP.

 

The proper way to capture user input is to use the appropriate PHP superglobal. In this case, you'd use $_POST.

Yes, but if you haven't submitted the form yet you will get an "undefined index" warning (because $_POST has not yet been populated). So, usually scripts will check to see if the form has been submitted before trying to work with it.

 

I (quickly) rewrote part of your script:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
function calculate($TotalSqFt = null, $price = null) {
	return $TotalSqFt + $price;
}

// initialize variables so we dont get undefined errors later on
$price     = null;
$TotalSqFt = null;

// check if the form was submitted
if (!empty($_POST)) {
	$cut_types = array(
		2 => '3.50',		
		3 => '4.70',
		4 => '5.90',
		5 => '7.00',
		6 => '8.20'
	);

	$price     = $cut_types[$_POST['cuts']];	
	$TotalSqFt = $_POST['TotalSqFt'];
}
?>  
<form action="" "" method="post">
<table>
    	<tr>  
		<td> total sq ft </td><td> <input name="TotalSqFt" type="text" size="4"></td><br />  
            <td>  number of cuts </td><td><select name="cuts" id="cuts">  <br>
<?php
  for ($i =2; $i <=6; $i ++)
	{
	echo "<option value=\"$i\"";
	if($cut == $i){
		echo ' SELECTED';
	}
	echo ">$i</option>"; 
}
?>   
</select>
</td>            
	</tr>
    <tr>  
    	<td> <input name="submit" type="submit" value="calculate"></td><td>  <?php calculate($TotalSqFt, $price); ?>  </td>		
    </tr>
    </table>
</form>
</body>
</html>

it is working!  While I was waiting for help I tried a different code that I felt more comfortable with and I was wonder if I could get help understanding this error. 

Notice: Undefined index: cut in /home/content/04/8884504/html/jorge/calculator.php on line 6

 

<?php
ini_set('display_errors', 1);
error_reporting(-1);
if(isset($_POST['submit'])) {
$TotalSqFt = $_POST['TotalSqFt']; 
$cut = $_POST['cut']; 
}
?>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
if ($i = 2) {
	$price = 3.50; 
}
elseif ($i = 3) {
 	$price = 4.70; 
}
if ($i = 4) {
	$price = 5.90; 
}
elseif ($i = 5) {
 	$price = 7.00; 
}
elseif ($i = 6) {
	$price = 8.20; 
}
else {

}
function calculate($TotalSqFt, $price) {
	$total = $TotalSqFt + $price; 
	return $total; 	
}
?>  
<form action="" "" method="post">
<table>
    	<tr>  
		<td> total sq ft </td><td> <input name="TotalSqFt" type="text" size="4"></td><br />  
            <td>  number of cuts </td><td><select name="cuts" id="cuts">  <br>
<?php
  for ($i =2; $i <=6; $i ++)
	{
	echo "<option value=\"$i\"";
	if($cut == $i){
		echo ' SELECTED';
	}
	echo ">$i</option>"; 
}
?>   
</select>
</td>            
	</tr>
    <tr>  
    	<td> <input name="submit" type="submit" value="calculate"></td><td>  <?php echo calculate($TotalSqFt, $price); ?>  </td>		
    </tr>
    </table>
</form>
</body>
</html>

Undefined Index refers to the array key of the superglobal you're requesting.  In your situation, you are requesting the key 'cut' for $_POST i.e. $_POST['cut']

 

Your form MUST have that input being submitted if you wish to retrieve it on the opposite side.  Back to what we've been saying repeatedly here: define what you request and request what you define.

<?php
if ($i = 2) {
	$price = 3.50; 
}
elseif ($i = 3) {
 	$price = 4.70; 
}
if ($i = 4) {
	$price = 5.90; 
}
elseif ($i = 5) {
 	$price = 7.00; 
}
elseif ($i = 6) {
	$price = 8.20; 
}
else {

}

 

That block of IF statements does not make sense where it is, as $i is not defined at that point.  From what it looks like you probably want to be using $cut instead of $i in your conditions.  The conditions themselves are also incorrect as the = operator does an assignment not a comparison.  You need to use == (two equal signs) to do a comparison check.  The way it is right now, your price would always be 3.50 because the first condition would assign the value 2 to your variable, and then set the price to 3.50

 

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.