Link to home
Start Free TrialLog in
Avatar of Hidesign
HidesignFlag for United States of America

asked on

How do I dynamically changed a form result layout using php

I have a form using php. Depending on one of the fields entered, I want to add extra info on the form result. I am having trouble with the syntax. I only want the code below printed out in the form result when a the address field has "ny" or "nyc" or "manhattan" entered in it

      <tr><td> This is to certify that I am an authorized officer of</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtCompanyName'] .  "</td></tr>
                        
                                    
                        <tr><td> Date:</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtSignatureDate'] . "</td></tr>
                        <tr><td> Name:</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtCompanyOfficer'] . "</td></tr>
                        <tr><td> Title:</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtCompanyOfficerTitle'] . "</td></tr><br />
                <tr><td> Signature:</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtSignature'] . "</td></tr>
                         <tr><td> Lien Amount $:</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtExactOwed'] . "</td></tr>
                         
                         <tr><td colspan=\"3\">Block:</td></tr>
                         <tr><td colspan=\"3\">Lot:</td></tr>
                        
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

You have not shown us enough of the code to understand where to find ny or nyc, etc.  However the usual design pattern is to use an if() statement.
Also, you might want to consider simplifying your programming life by learning about HEREDOC notation - it will make the syntax much easier to read.
http://www.php.net/manual/en/language.types.string.php

Example...

$str = <<<END_OF_STRING
<td>$var</td>
END_OF_STRING;

No concatenation, no quotes, just variable substitution on $var -- really easy to get right!

HTH, ~Ray
Try
if (preg_match("/(ny|nyc|manhattan)/i", $_POST['addressfieldname'])){
	echo "HTML goes here";
}

Open in new window

Avatar of Hidesign

ASKER

Thanks for yuour quiclk responeses. This is the filed I need to check for ny, nyc and manhattan. I
if ($_POST['ctl00$ContentPlaceHolder1$txtCompanyCity'] == "" )

If this field has any of those values, then I want to diplay the  lines put above in the form result. If these any of those values are not entered, then the form result should not display those fields and text

Thanks
Try
<?
if (preg_match("/(ny|nyc|manhattan)/i",$_POST['ctl00$ContentPlaceHolder1$txtCompanyCity'])){
echo "<tr><td> This is to certify that I am an authorized officer of</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtCompanyName'] .  "</td></tr>
                        
                                    
                        <tr><td> Date:</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtSignatureDate'] . "</td></tr>
                        <tr><td> Name:</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtCompanyOfficer'] . "</td></tr>
                        <tr><td> Title:</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtCompanyOfficerTitle'] . "</td></tr><br />
                <tr><td> Signature:</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtSignature'] . "</td></tr>
                         <tr><td> Lien Amount $:</td><td width=\"" . $width . "\"></td><td>" . $_POST['ctl00$ContentPlaceHolder1$txtExactOwed'] . "</td></tr>
                         
                         <tr><td colspan=\"3\">Block:</td></tr>
                         <tr><td colspan=\"3\">Lot:</td></tr>";
}	
?>

Open in new window

Thanks - I put this code in and got a parsing error on the line below

Parse error: syntax error, unexpected '"' in /home3/speedyli/public_html/test-lien/formatEmail-field.php on line 330

if (preg_match("/(ny|nyc|manhattan)/i",$_POST['ctl00$ContentPlaceHolder1$txtCompanyCity'])){
If the error is on line 330, we may be missing a larger part of the picture.  You have not posted more than a handful of lines of code, so we are guessing about where the error really might be.  The if() with preg match above looks OK to me.
Should attach the whole php page. Thanks so much for your help
ASKER CERTIFIED SOLUTION
Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks - I will try it
Have been away for a few hours, but I just got a look at that code.  It looks very complex for what it appears to be doing -- providing sanity checks and error messages for an HTML form.  I don't have time to rewrite it now, but I can help with a design pattern that might simplify things now, so when the time comes to modify the form you will have a more malleable script.  See if this makes sense.  It goes a little beyond the question of how to use preg_match in an if() statement, but hopefully it will stimulate your thinking about how the overall script structure can be simplified.

best regards, and good luck with the project, ~Ray
<?php // RAY_form_highlight_errors.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO HIGHLIGHT ERRORS IN FORM INPUT
// CLIENT IS ASKED TO PUT IN A VALUE
// IF THE VALUE FAILS OUR TEST WE SHOW AN ERROR MESSAGE
// WE PUT A MARKER NEXT TO THE INPUT CONTROL ON THE FORM
// SEE http://www.w3schools.com/CSS/pr_class_visibility.asp


// THESE CONDITIONS ARE SET FOR THE INITIAL ENTRY
$error_any = 'hidden';
$error_abc = 'hidden';
$error_xyz = 'hidden';

// CAPTURE AND NORMALIZE THE POST VARIABLES, IF ANY
$abc = (isset($_POST["abc"])) ? trim(strtoupper($_POST["abc"])) : NULL;
$xyz = (isset($_POST["xyz"])) ? trim(strtoupper($_POST["xyz"])) : NULL;

// IF ANYTHING WAS POSTED, VALIDATE IT
if (!empty($_POST))
{
    // VALIDATE THE 'abc' FIELD
    if ($abc != 'ABC')
    {
        $error_any = 'visible';
        $error_abc = 'visible';
        $abc       = NULL;
    }

    // VALIDATE THE 'xyz' FIELD
    if ($xyz != 'XYZ')
    {
        $error_any = 'visible';
        $error_xyz = 'visible';
        $xyz       = NULL;
    }

    // DO WE HAVE INPUT FREE FROM ANY ERRORS?
    if ($error_any != 'visible')
    {
        echo "CONGRATULATIONS";
        die();
    }
}

// IF NOTHING WAS POSTED, OR IF THERE ARE ERRORS, WE NEED NEW CLIENT INPUT
$form = <<<ENDFORM
<style type="text/css" media="all">
.error_any { visibility:$error_any; }
.error_abc { visibility:$error_abc; }
.error_xyz { visibility:$error_xyz; }
</style>
<pre>
<form method="post">
<span class="error_any">PLEASE CORRECT THE FOLLOWING ERRORS</span>
<span class="error_abc">YOU MUST ENTER 'abc' IN THIS FIELD</span>
PLEASE ENTER "ABC" HERE: <input name="abc" value="$abc" />
<span class="error_xyz">YOU MUST ENTER 'xyz' IN THIS FIELD</span>
PLEASE ENTER "XYZ" HERE: <input name="xyz" value="$xyz" />
<input type="submit" />
</form>
ENDFORM;

// WRITE THE FORM WITH THE APPROPRIATE CSS STYLES ON THE ERROR MESSAGE FIELDS
echo $form;

Open in new window

Worked Perfectly
Ray_Paseur: - thanks for your answer too. I will also try this logic and I appreciate your time spent on this