display two values on page [message #181130] |
Thu, 18 April 2013 09:44 |
newbie php
Messages: 3 Registered: April 2013
Karma: 0
|
Junior Member |
|
|
Hi all,
I'm an absolute newbie to PHP, but I need to send two values to a php-page and multiplay to calculate costs for an order.
Simplified, i have a form that sends number and price to calculate.php.
code:
<form name="form" method="post" action="calculate.php">
<table border="0" cellspacing="5" cellpadding="10">
<tr>
<td>tickets</td>
<td><select name="number">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="calculate"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table><input name="price" type="hidden" value=6>
</form>
So the form sends the chosen number and the hidden value of 6 to calculate.php
Calculate php maltiplies both values. In code:
<html>
<head>Cost calculated</head>
<body>
<?php
$p=$_POST[price];
$n=$_POST[number];
$kosten = $p * $n;
echo $a "<br>"
echo $p "<br>"
echo $kosten "<br>"
?>
</body>
</html>
However, this results in a server-error.
Please, anybody, where do I go wrong?
Sybolt
|
|
|
Re: display two values on page [message #181131 is a reply to message #181130] |
Thu, 18 April 2013 10:01 |
Tim Streater
Messages: 328 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
In article <e154c2d7-ccdf-4058-81d5-cbfbfec20264(at)googlegroups(dot)com>,
newbie php <s(dot)hoitinga(at)gmail(dot)com> wrote:
> <html>
> <head>Cost calculated</head>
> <body>
1) You probably mean:
<head>
<title >Cost calculated</title>
</head>
> <?php
> $p=$_POST[price];
> $n=$_POST[number];
2) For forward compatibility, better put:
$p=$_POST['price'];
$n=$_POST['number'];
> $kosten = $p * $n;
>
> echo $a "<br>"
> echo $p "<br>"
3) You really *must* put:
echo $a . "<br>"
echo $p . "<br>"
--
Tim
"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
|
|
|
|
|
|
|
Re: display two values on page [message #181136 is a reply to message #181133] |
Thu, 18 April 2013 12:05 |
Tim Streater
Messages: 328 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
In article <kkoh1h$v37$1(at)speranza(dot)aioe(dot)org>,
Christoph Becker <cmbecker69(at)gmx(dot)de> wrote:
> Tim Streater wrote:
>> 3) You really *must* put:
>> echo $a . "<br>"
>> echo $p . "<br>"
>
> Don't forget the terminating semicolon:
>
> echo $a . "<br>";
Ha yes, you're quite right. My excuse: I was chasing a problem with
SQLite at the time.
--
Tim
"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
|
|
|
Re: display two values on page [message #181137 is a reply to message #181130] |
Thu, 18 April 2013 13:08 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
In addition to what the others have said:
On 4/18/2013 5:44 AM, newbie php wrote:
<snip>
$p=isset($_POST['price']) ? $_POST['price'] : 0;
$n=isset($_POST['number']) ? $_POST['number'] : 0;
The reason being - your page may be called from pages other than the
form (i.e., someone typing the URL into the browser bar), in which case
$_POST['price'] and $_POST['number'] won't be set. This code will set
0's into $p and $n should the values not be set.
Better yet - don't put things like prices in hidden fields. It would be
very easy for me to create a form on my own machine which sets a price
of 1. You can display the price to the user, but keep critical values
like this on the server.
Remember - NOTHING from the user is ever safe and EVERYTHING needs to be
validated.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: display two values on page [message #181140 is a reply to message #181137] |
Thu, 18 April 2013 16:46 |
Luuk
Messages: 329 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 18-04-2013 15:08, Jerry Stuckle wrote:
> It would be very easy for me to create a form on my own machine which
> sets a price of 1.
I would set it to zero.....
;)
|
|
|