tags:

views:

106

answers:

1

I hope I can be clear enough in what I need here. What I have is a function that contains some html for an email client app I'm developing. This part of the app uses a common forward, reply and "reply to all" form. Since they are all basically the same, I figured I'd go the lean route and just use a single function to handle this. The only real differences I can see between the 3 actions I mentioned above are that in a reply to all, there will be multiple email addys in the CC part of the form. For a forward, no (cc) and the (to) box should be blank. I need to represent all of this functionality and I'm kind of confused on what the best way to do this is. Can anyone please offer any help? Thanks.

I can certainly post the html is need be, I just wanted to start light.

EDIT: I almost forgot, there will be POST values when the user submits the form in the event that the form validation fails.


function compose($type,$contents=null)
{
    echo'
        <tr>
          <td>
            <tr>
              <td valign="top">
                <form method="post" action="">
                  <table width="100%" cellpadding="0" cellspacing="0" border="0" id="reply">
                    <tr>
                      <td><h2>'.$type.'</h2></td>
                    </tr>
                    <tr>
                      <td width="1%" valign="top" nowrap><b>To:</b><br><input name="to" id="focus" title="Enter a single system user here" value="" type="text" size="64"></td>
                    </tr>
                    <tr>
                      <td nowrap><b>Cc:</b><br><input name="cc"" value="" type="text" size="64"></td>
                    </tr>
                    <tr>
                      <td nowrap><b>Subject:</b><br><input name="subject" title="Enter your subject here" value="" type="text" size="64" maxlength="30"></td>
                    </tr>
                    <tr>
                      <td valign="top"><b>Message:</b><br><textarea name="message" title="Enter your message here" rows="5" cols="50" wrap="virtual"></textarea></td>
                    </tr>
                    <tr>
                      <td>&nbsp;</td>
                    </tr>
                    <tr>
                      <td><input type="hidden" name="id" value=""><input type="submit" name="send" value="Send"></td>
                    </tr>
                  </table>
                </form>
              </td>
            </tr>
          </td>
        </tr>';
}
A: 

EDIT: Example modification of posted code (I haven't added all the different cases or even changed the output really, just showing the concept - all that's here is a check for a type of Reply and a check for the 'to' POST value.)

function compose($type,$contents=null)
{
    $toValue = '';
    if(isset($_POST['to']))
    {
        // Might want to actually validate this to prevent XSS, but this is just a basic example
        $toValue = $_POST['to'];
    }

    echo'
        <tr>
          <td>
            <tr>
              <td valign="top">
                <form method="post" action="">
                  <table width="100%" cellpadding="0" cellspacing="0" border="0" id="reply">
                    <tr>
                      <td><h2>'.$type.'</h2></td>
                    </tr>';

    if($type == "Reply") {
        echo'
                    <tr>
                      <td width="1%" valign="top" nowrap><b>To:</b><br><input name="to" id="focus" title="Enter a single system user here" value="' . $toValue . '" type="text" size="64"></td>
                    </tr>
                    <tr>
                      <td nowrap><b>Cc:</b><br><input name="cc"" value="" type="text" size="64"></td>
                    </tr>';
    }

    echo'
                    <tr>
                      <td nowrap><b>Subject:</b><br><input name="subject" title="Enter your subject here" value="" type="text" size="64" maxlength="30"></td>
                    </tr>
                    <tr>
                      <td valign="top"><b>Message:</b><br><textarea name="message" title="Enter your message here" rows="5" cols="50" wrap="virtual"></textarea></td>
                    </tr>
                    <tr>
                      <td> </td>
                    </tr>
                    <tr>
                      <td><input type="hidden" name="id" value=""><input type="submit" name="send" value="Send"></td>
                    </tr>
                  </table>
                </form>
              </td>
            </tr>
          </td>
        </tr>';
}

(Original inquiry) Is this function processing the results of the form, or is it displaying the form in the first place? Your question is a bit unclear about which point in the process you're talking about.

Amber
Thanks for the reply. This function is echoing the html back into a different part of the form.
I'm already using some if conditions but its getting really ugly. I like clean when I can achive it. :)
I'd definitely suggest posting what you have already then.
Amber
OK, I'll post it above. brb and thanks again.
Based on the code you posted, I'd suggest splitting your HTML up into 3 parts each with its own echo statement: the portion that comes before the To: and Cc: fields; the To: and Cc: fields themselves, and then the portion that comes after the fields. Then you can use basic PHP logic to determine how the To/Cc fields should be echoed depending on the type, without having to duplicate the other HTML.
Amber
Thanks Dav, I'll take that advice. One more thing... How would you go about handling the POST data over what is imported from the database. In other words, say a user clicks on the reply to all button. He will get all of the (cc) values imported into the cc input. Say he changes those values and deletes 1 or 2 of the (cc)s and say that for whatever reason the form fails. I need to give that user the ability to see what he posted and it should *overwrite* the imported values from the database at that point. How might I handle this? Thanks again for the help!
I'd suggest doing some initial logic in the compose() function before you get to any of the echo statements to determine what the contents of each field should be and store the results in variables (something like $valueTo, $valueCc, etc) and then within your echo strings, place those variables within your value="" bits via concatenation. If the corresponding POST variable isn't set, just have your logic set the variable to a blank string and it will be equivalent to your current output.
Amber
Excellent Dav. Thanks for helping me out here. :)
Dav, you might want to update your answer so we can upvote it :)
Yar