Welcome to WebmasterWorld Guest from 54.145.39.186

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Comparing two values

     

Jamier101

11:29 am on May 15, 2012 (gmt 0)



I've been writing an e-mail contact form and I'd like to check that two address are the same, I'd ideally like to do this on-the-fly.

So far I have the following code but I keep getting an error.

Use of undefined constant StatementEmail - assumed 'StatementEmail'


Does anyone know where I'm going wrong?

<tr>
<td><div align="right">Address for statements:</div></td>
<td><input name="StatementEmail" type="text" size="40" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><div align="right">Please re-enter address:</div></td>
<td><input name="ValStatementEmail" type="text" size="40" />
<td>
<?php
$var1 = StatementEmail;
$var2 = ValStatementEmail;

if ($var1 == $var2){
echo 'True';
} else {
echo 'False';
}
?>
</td>

incrediBILL

11:53 am on May 15, 2012 (gmt 0)

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



You have to get those variables from the $_POST[] array, assuming you're using POST to submit the form as your code is incomplete and I can't tell for sure.

Such as:
$var1 = $_POST['StatementEmail'];

Jamier101

12:00 pm on May 15, 2012 (gmt 0)



I've updated the code to read:

 <tr>
<td><div align="right">Address for statements:</div></td>
<td><input name="StatementEmail" type="text" size="40" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><div align="right">Please re-enter address:</div></td>
<td><input name="ValStatementEmail" type="text" size="40" />
<td>
<?php
$var1 = $_POST['StatementEmail'];
$var2 = $_POST['ValStatementEmail'];

if ($var1 == $var2){
echo 'True';
} else {
echo 'False';
}
?>
</td>
</tr>


but still no luck.

The full error I get is:

Notice: Undefined index: StatementEmail in D:\home\web\myaddress.co.uk\htdocs\email_collection.php on line 223

Notice: Undefined index: ValStatementEmail in D:\home\web\myaddress.co.uk\htdocs\email_collection.php on line 224
True


Those lines relate to the following lines of code:

Line 223: $var1 = $_POST['StatementEmail'];
Line 224: $var2 = $_POST['ValStatementEmail'];

httpwebwitch

3:37 pm on May 15, 2012 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



here's how I'd do it. you can clean up the <table> HTML later

first, your form needs to be in a <form> so it submits to somewhere, with method=post

then, only do the comparison if the form is a postback, ie it's not going to happen unless someone pressed submit. There are many ways to do that but I like to put a hidden input in the form that you can check if it exists


<form action="" method="post"/>
<input type="hidden" name="action" value="processform" />
<table>
<tr>
<td><div align="right">Address for statements:</div></td>
<td><input name="StatementEmail" type="text" size="40" /></td>
<td></td>
</tr>
<tr>
<td><div align="right">Please re-enter address:</div></td>
<td><input name="ValStatementEmail" type="text" size="40" />
<td>
<?php
if (isset($_POST['action'])){
if ($_POST['StatementEmail'] == $_POST['ValStatementEmail']){
echo 'True';
} else {
echo 'False';
}
}
?>
</td>
</tr>
<tr><td><input type="submit" value="SUBMIT"/></td></tr>
</table>
</form>

Jamier101

3:48 pm on May 15, 2012 (gmt 0)



Is it possible to make it check straight after a value has been entered into the ValStatementEmail field without someone pressing submit?

httpwebwitch

3:48 pm on May 15, 2012 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



you can further enhance the form in a many ways...

1) client-side validation. Use javascript to check if the fields match "onsubmit" without actually posting to the server

2) populate the fields with the _POST values, so if they mess up they can correct what they did, and don't have to re-type everything again. This is especially important since it's a contact form, and they might have spent 20 minutes writing something which would be lost if they did a dumb typo in the email address

3) trim and lowercase the comparison, so "me@example.com " will match "Me@Example.Com " - it's more forgiving in case they do something weird in the form

4) output something more sensible than "true" and "false"

5) do your validation at the top of the script, not inline... do something different if validation passes, eg write the stuff into a database or send an email, and show a confirmation message instead of showing the same form again

6) ... etc

I get that this is a work in progress, and you'll enhance it l8r to make it user friendly & functional

Forms are complex things, but once you've done it a dozen times you'll get the knack.

httpwebwitch

3:50 pm on May 15, 2012 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Is it possible to make it check straight after a value has been entered into the ValStatementEmail


yeah, you can do that using client-side JavaScript. The server (and PHP) won't get anything until the form is submitted. But JavaScript can react to the form while it's being filled out.

Jamier101

3:54 pm on May 15, 2012 (gmt 0)



Thanks hhtpwebwitch I'll have a look do a search for JavaScript to see what I can do simply and take it from there. As you clearly pointed out, this is a work in progress!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month