Forum Moderators: coopster

Message Too Old, No Replies

If/Else Help

I need a row to not appear IF...

         

Uban

9:58 pm on Feb 20, 2008 (gmt 0)

10+ Year Member



Hello...

I'm totally new to PHP. However, I've managed a lot so far but am stuck.

I'm building a custom thank you page which passes variables via _Post method and it's working out great.

However, I need help. The thank you page lists customer name, address, totals, etc.

If a customer only enters their day time phone number and not their home phone number, I need the row saying "Home Phone" to just not pull up at all.

Here are the rows:

<tr><td>Work Phone:</td><td>$workphone</td></tr>
<tr><td>Home Phone:</td><td>$homephone</td></tr>
<tr><td>Email:</td><td>$email1</td></tr>
<tr><td>Address 1:</td><td>$address1</td></tr>
<tr><td>City:</td><td>$city</td></tr>
<tr><td>State/Province:</td><td>$state</td></tr>
<tr><td>Country:</td><td>$country</td></tr>
<tr><td>ZIP/Postal:</td><td>$zip</td></tr>

I need the second row to just not even apeark if the Home Phone number wasn't filled out in the cart.

Here's my post command: $homephone = $_POST['homephone'];

This is something I tried for the second row but I don't know if I have the wrong syntax or if I'm just totally off:

if ( $homephone = $_POST['homephone'] ) {
echo '<tr><td>Home Phone:</td><td>$homephone</td></tr>';
} else {
echo '';
}

Any help would be VERY appreciated!

Thanks...

d40sithui

10:22 pm on Feb 20, 2008 (gmt 0)

10+ Year Member



try this:
$homephone = $_POST['homephone'];
//if homephone contains 10 or more digits
if(strlen($homephone)>=10){
echo "<tr><td>Home Phone:</td><td>$homephone</td></tr>";
}

londrum

10:29 pm on Feb 20, 2008 (gmt 0)

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



this depends on how your code is written, but if the variable doesn't get set when they don't include it, then you could just check to see if it is set. something like...

if (isset($_POST['homephone'])) {
// print out homephone
} else {
// print out other phone
}

Uban

10:41 pm on Feb 20, 2008 (gmt 0)

10+ Year Member



I am so new to this, that the last explanation didn't really make sense to me, but I really do appreciate the help! I can copy and paste pretty well though ;)

Here's a quick example of what I have just under the <body>
<?php
$workphone = $_POST['workphone'];
$homephone = $_POST['homephone'];
"and more listed but I won't list them here"

echo"

Then my html code which provides the really nice thank you page design. All the variables pass just fine and it looks great.

Above is listed some of the code which works fine. This is what I've attempted to try in order to remove the home phone row:

<tr><td>Work Phone:</td><td>$workphone</td></tr>
if ( $homephone == $_POST['homephone'] ) {
echo '<tr><td>Home Phone:</td><td>$homephone</td></tr>';
} else {
echo '';
}
<tr><td>Email:</td><td>$email1</td></tr>

However, I now get a Parse Error for the line starting with if

But, are you saying d40, to make the code look like this?

<tr><td>Work Phone:</td><td>$workphone</td></tr>
$homephone == $_POST['homephone'];
//if homephone contains 10 or more digits
if(strlen($homephone)>=10){
echo '<tr><td>Home Phone:</td><td>$homephone</td></tr>';
}
<tr><td>Email:</td><td>$email1</td></tr>

I still get a parse error...but I feel I'm really close.

Thanks!

d40sithui

4:09 pm on Feb 21, 2008 (gmt 0)

10+ Year Member



if thats the real code you have then you should get an error.
your first and last <tr><td> blocks are not in an echo statement.
so it should be
 
echo "<tr><td>Email:</td><td>$email1</td></tr>";

remember to use double quotes when you want the variable to be parsed. if you use single quote with a variable, its name will be echoed, not its value.

this snippet i gave u checks for the lenght of the phone number string that was entered. depending on what you want to do, consider using the isset() function as londrum suggested. this checks if the variable is set. but of course, even if the user left this field empty and hit submit, isset would still return true. so only use this check in addition to other methods of variable validation. when you get more into php, you'll find yourself using several functions to check your data.

Uban

5:51 pm on Feb 21, 2008 (gmt 0)

10+ Year Member



Thanks for all the help.

Yes, the code above the if statement didn't ";. I had forgotten the ;.

So just before the if statement started:
<tr><td>Work Phone:</td><td>$workphone</td></tr>";

I had forgotten the ;

Following:
if ($homephone){
echo "<tr><td>Home Phone:</td><td>$homephone</td></tr>";
} else {
echo "";
}
echo"
<tr><td>Email:</td><td>$email1</td></tr>
and so on to the end....

Thanks everyone!