Forum Moderators: coopster

Message Too Old, No Replies

A couple of questions about using php in html forms

         

Bud_M

7:09 pm on Jun 7, 2011 (gmt 0)

10+ Year Member



Knowing nothing about php, I cobbled this form together using snippets I found online to create a simple budget calculator.

[moongraf.com...]

A couple of questions:

1) I inserted php into the text fields as values to keep the form from clearing on submit. So in order to clear the form with a reset button, I need to clear all variables. How do I go about this?

2. My total amount doesn't automatically show 2 decimal places. Is there a way to do this?

rocknbil

7:50 pm on Jun 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Bud_M. The reset button doesn't clear because it resets the form to the loaded page state. You can do this with Javascript or with PHP by setting the values to ''. So you'd change the reset button to a submit button, and something like

<input type="submit" name="clearme" value="Clear Form">

if (isset($_POST['clearme'])) {
// for each form field, set to ''
}

Javascript would be similar, except it wouldn't actually submit, but then it becomes Javascript-dependent.

To display two decimal places, use money_format() [php.net] or sprintf, I prefer sprintf:

$total = sprintf("%.2f",$total);

You can also do calculations on the right in either:

$total = sprintf("%.2f",($field1*$field2)+$field3+$field4+$field5);

Bud_M

8:51 pm on Jun 7, 2011 (gmt 0)

10+ Year Member



The decimal part worked great. Thanks!

The other part I'm still wrestling with. For the reset button I put

<input name="clearme" type="submit" value="Clear Form">

<?php
if(isset($_POST['clearme'])) {
$_POST['#1'] = '';
$_POST['#2'] = '';
$_POST['#3'] = '';
}
?>

but it does not have the desired result. It zeroes out the final total but leaves the numbers in the text fields. Where did I get it wrong?

Bud_M

12:33 am on Jun 8, 2011 (gmt 0)

10+ Year Member



This seems like it should do what I need, but it's not working

<?php
if (isset($_POST['clearme'])) {
unset($var1, $var2, $var3);
}
?>

rocknbil

4:12 pm on Jun 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure . . . especially about using a # in a form name, but if it works otherwise I guess it's OK.

First verify you're actually getting a value for "clearme"

if (isset($_POST['clearme'])) {
echo 'OK, got clearme';
}

Next, look down into where the form is generated. You will see **something** like

echo '<input name="#1" type="text" size="7" maxlength="7" value="' . $_POST['#1'] . '" />';

or

<input name="#1" type="text" size="7" maxlength="7" value="<?php echo $_POST['#1']; ?>" />

or

echo "<input name=\"#1\" type=\"text\" size=\"7\" maxlength=\"7\" value=\"$var1\" />";

Anyway whatever that value is . . . you need to set it to '', null, or unset it IF clearme is present.

g1smd

11:43 pm on Jun 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No. You cannot use # as part of the variable name.

It breaks the HTTP specs unless it is encoded.

Bud_M

11:59 pm on Jun 8, 2011 (gmt 0)

10+ Year Member



Thanks again for helping me with this. I have "OK, got clearme" showing when I click the clear button. I don't know how to set the value to null.

I did change the # to a for the input names. Code reads:

<input name="a1" type="text" size="7" maxlength="7" value="<?php echo $_POST['a1']; ?>" />

the number typed into a1 becomes var1, same for a2 and a3

<?php
$var1 = $_POST['a1'];
$var2 = $_POST['a2'];
$var3 = $_POST['a3'];
$var12 = sprintf("%.2f",($var1 * $var2) + $var3);
echo "$var12";
?>

to clear the value with the clearme button, I tried

<?php
if (isset($_POST['clearme'])) {
echo 'OK, got clearme';
$a1 = '';
$a2 = '';
$a3 = '';
}
?>

and

<?php
if (isset($_POST['clearme'])) {
echo 'OK, got clearme';
$var1 = '';
$var2 = '';
$var3 = '';
}
?>

Neither worked. How should it be done?

Uploaded at [moongraf.com...]

Bud_M

3:22 pm on Jun 9, 2011 (gmt 0)

10+ Year Member



I got some help with this, here's what worked:

</head>
<?php //this is where php goes

if(!isset($_POST['clearme']))
{
$var1 = $_POST['a1'];
$var2 = $_POST['a2'];
$var3 = $_POST['a3'];
$total = sprintf("%.2f",($var1 * $var2) + $var3);
} else {
$a1 = "";
$a2 = "";
$a3 = "";
}
?>
<body>
<br />
<div class="border2">
<div class="border1">
<form action="example16.php" method="post">
<table width="250" border="0" cellspacing="0" cellpadding="7">
<tr>
<td colspan="2" align="center" bgcolor="#996666">Budget Calculator</td>
</tr>
<tr>
<td width="117" align="right">Number 1: </td>
<td width="105" align="left"><input name="a1" type="text" size="7" maxlength="7" value="<?php echo $var1; ?>" /></td>
</tr>
<tr>
<td width="117" align="right">Number 2: </td>
<td width="105" align="left"><input name="a2" type="text" size="7" maxlength="7" value="<?php echo $var2; ?>" /></td>
</tr>
<tr>
<td width="117" align="right">Number 3: </td>
<td width="105" align="left"><input name="a3" type="text" size="7" maxlength="7" value="<?php echo $var3; ?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Calculate" />
<input name="clearme" type="submit" value="Clear Form">
</td>
</tr>
<tr>
<td colspan="2" align="center">Total: $<?php echo $total; ?>
<br />

</tr>
</table>
</form>

Any thoughts on putting the php between /head and body tags? That's a new one to me.

g1smd

11:54 pm on Jun 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The
</head>
and
<body>
tags are used by the browser. For the page that is delivered to the browser there must be nothing between them.

The PHP code is used only on and by the server. Therefore, in theory the PHP code can go wherever you want. However, for many reasons, you should aim to place most of the PHP code BEFORE the DOCTYPE declaration.

Bud_M

2:06 am on Jun 11, 2011 (gmt 0)

10+ Year Member



I do know what the head and body tags are for, I had just never seen code placed between the closing head tag and the body tag. You say there must be nothing between them, and yet it works...

Doing a quick search I don't see anything saying anything about it being ok or not. Do you have a reference saying its not ok?

Leosghost

3:27 am on Jun 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ummm..g1smd is pretty much "a reference". :)

Pazuzu156

3:41 am on Jun 11, 2011 (gmt 0)

10+ Year Member



jQuery would be extremely easy for this. Ex.

$(function() {
$("#resetform").click(function() {
$(".entireFormInputClass").val('');
});
});

If you set a class to all the input, and use jQuery to set all fields to default (empty) that will work.

g1smd

6:33 am on Jun 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I had just never seen code placed between the closing head tag and the body tag. You say there must be nothing between them, and yet it works...

Read my post again. You must not deliver any HTML to the browser between the </head> and <body> tags.

What you do ON the server in PHP code matters not at all to the browser. :)

rocknbil

5:40 pm on Jun 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



HTML to the browser


He's referring to valid html, for example, I can't imagine why you'd want to, but you could do something like this.

</head>
<?php
$total = $_GET['input'] * 25;
?>
<body>
echo "<h1>$total</h1>";

Not this

</head>
<?php
echo $_GET['input'] * 25;
?>
<body>

Bud_M

6:47 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



Yes, I should have read more carefully. Thanks for the clarification.