Page 1 of 1

please help me i am new to PHP

Posted: Mon Jan 15, 2007 4:42 pm
by suhail_anwar_khan
hi,
i am new to php and i reffred a book to get the below written code but when i run it it gives notices.

calculate_form.html ================>

Code: Select all


<HTML>
<HEAD>
<TITLE>Calculation Form</TITLE>
</HEAD>
<BODY>


<FORM METHOD="post" ACTION="calculate.php">



<P>Value 1: <INPUT TYPE="text" NAME="val1" SIZE=10></P>


<P>Value 2: <INPUT TYPE="text" NAME="val2" 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>







calculate.php======================>



<HTML>
<HEAD>
<TITLE>Calculation Result</TITLE>
</HEAD>
<BODY>

<?
if (($_POST[val1] == "") || ($_POST[val2] == "")|| ($_POST[calc] =="")) {
header("Location: calculate_form.html");
exit;
}
if ($_POST[calc] == "add") {
    $result = $_POST[val1] + $_POST[val2];


} else if ($_POST[calc] == "subtract") {
    $result = $_POST[val1] - $_POST[val2];
} else if ($_POST[calc] == "multiply") {
    $result = $_POST[val1] * $_POST[val2];
} else if ($_POST[calc] == "divide") {
    $result = $_POST[val1] / $_POST[val2];
}



?>

<P>The result of the calculation is: <? echo "$result"; ?></P>


</BODY>
</HTML>





and when i access   calculate.php  from calculate_form.html  i get the following notices in browser===========>


Notice: Use of undefined constant val1 - assumed 'val1' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\calculate.php on line 8

Notice: Use of undefined constant val2 - assumed 'val2' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\calculate.php on line 8

Notice: Use of undefined constant calc - assumed 'calc' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\calculate.php on line 8

Notice: Use of undefined constant calc - assumed 'calc' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\calculate.php on line 12

Notice: Use of undefined constant val1 - assumed 'val1' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\calculate.php on line 13

Notice: Use of undefined constant val2 - assumed 'val2' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\calculate.php on line 13

The result of the calculation is: 10




but the script works wel when i use $_POST['val1'] intead of $_POST[val1] can any one help me with this

Posted: Mon Jan 15, 2007 4:51 pm
by hrubos
I think you should use $_POST['val1'] .....


http://www.tizag.com/phpT/postget.php

Posted: Mon Jan 15, 2007 4:51 pm
by John Cartwright
but the script works wel when i use $_POST['val1'] intead of $_POST[val1] can any one help me with this
Seems you already know what we are going to say. Quoting your array indices is best practice.

Posted: Mon Jan 15, 2007 4:52 pm
by afbase
in your php.ini file your error setting is set to "E All"
;error_reporting = E_ALL & ~E_NOTICE
;
; - Show all errors, except for notices
;
;error_reporting = E_ALL & ~E_NOTICE | E_STRICT
;
; - Show only errors
;
;error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR
;
; - Show all errors except for notices and coding standards warnings
;
error_reporting = E_ALL
just correct it to E_ALL & ~E_NOTICE

Posted: Mon Jan 15, 2007 4:57 pm
by John Cartwright
I don't consider that the best route, considering php is complaining for a reason. I prefer to fix the issue rather than applying a bandaid.

Posted: Mon Jan 15, 2007 5:17 pm
by afbase
ya i have to agree with that.