www.webdeveloper.com
Results 1 to 15 of 15

Thread: Need help passing variables to formmail.cgi script

  1. #1
    Join Date
    Sep 2004
    Posts
    18

    Need help passing variables to formmail.cgi script

    I am building a website for my church and I cant figure out how to pass the variable (subject of the message) to the formmail.cgi (matt wright clone) script. I also want to be able to associate the variable with the email recipient and change the email address that the form sends to the script as users change their subjects. I am also having trouble getting my validation script to force the user to select a subject. If anyone can help with any of these bugs, I would be greatly appreciative. The information about my cgi script is here:

    http://www.scriptarchive.com/readme/...ry_form_fields


    My code is below:

    <HTML>

    <HEAD>
    <BASE TARGET="FRAME2">

    <TITLE> Allons Church of Christ </TITLE>

    <style type="text/css">
    </style>

    <Script language="JavaScript" type="text/javascript">

    function validate ()
    {
    if (document.myform.sub.value == "")
    {
    alert('You must select a subject');
    return false;
    }
    else if (document.myform.realname.value == "")
    {
    alert('Name cannot be blank');
    return false;
    }
    else if (document.myform.email.value == "")
    {
    alert('Your email address is required');
    return false;
    }
    else if (document.myform.question.value == "")
    {
    alert('You left the question box blank. Did you have a question?');
    return false;
    }



    return true;
    }
    //-->

    </Script>




    </HEAD>


    <BODY>
    <BR><BR><BR>

    <FORM name="myform" id="myform" ACTION="/cgi-sys/FormMail.cgi" METHOD="POST"
    onsubmit="return validate ()">

    <table align="center" border="2" cellpadding="10" frame="box" rules="none">
    <tr>
    <td>Your Name </td>
    <td>

    <input type="text" size="30" name="realname" />
    </td>
    </tr>

    <tr>
    <td> Your e-mail address </td>
    <td>
    <INPUT TYPE="text" size="20" NAME="email" />
    </td</tr>

    <tr>
    <td> Subject </td>
    <td>


    <Select name = "sub" id="sub">
    <option value==""> Please select one... </option>
    <option value=="1"> Biblical Inquiry </option>
    <option value=="2"> Benevolence Inquiry </option>
    <option value=="3"> Website Inquiry </select> </option>
    </select>



    </td></tr>


    <tr>
    <td> Question </td>
    <td>
    <textarea name="question" id="question" cols="40" rows="10">
    </textarea> </td></tr>

    <tr align="center">
    <td colspan="2">
    <hr width="100% align="center"> </td></tr>

    <tr>
    <td colspan="2">
    <INPUT TYPE= submit value="Submit Question" /> &nbsp;
    <input type= reset value="Reset Form" />
    </td> </tr>

    </table>


    <input type=hidden name="subject" value = 'myform.sub.value'>
    <input type=hidden name="recipient" value ="webmaster@allonschurch.org">
    <input type=hidden name="env_report" value ="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT">


    </FORM>


    </body>

    </html>

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Two things:
    <option value==""> Please select one... </option>
    You hve used the compaison operator in all your options.
    Use:
    <option value=""> Please select one... </option>
    Change them all.

    You must change the subject input, otherwise it will return "".
    So, in validate() the last but one line should be:
    document.myform.subject.value=document.myform.sub.value;

    If it is still not working correctly, show us any errors (also from the server) and the contents of the e-mail if received.

  3. #3
    Join Date
    Sep 2004
    Posts
    18
    Ok, the validation is working, but it is putting the option value as 1,2,or 3 in the subject of the email being sent. I want it to use my function "findsub" to replace the numeric value with the corresponding subject. I also want to be able to change the recipient of the email as the subject changes. If anyone can help I will be very grateful. Thanks for the help!

    Here is my updated code:

    <HTML>
    <HEAD>
    <BASE TARGET="FRAME2">

    <TITLE> Allons Church of Christ </TITLE>

    <style type="text/css">
    </style>

    <Script language="JavaScript" type="text/javascript">

    function validate ()
    {
    if (document.myform.sub.value == "")
    {
    alert('You must select a subject');
    return false;
    }
    else if (document.myform.realname.value == "")
    {
    alert('Name cannot be blank');
    return false;
    }
    else if (document.myform.email.value == "")
    {
    alert('Your email address is required');
    return false;
    }
    else if (document.myform.question.value == "")
    {
    alert('You left the question box blank. Did you have a question?');
    return false;
    }


    document.myform.subject.value=document.myform.sub.value;

    return true;
    }




    function findsub ()

    {
    if (document.myform.subject.value == "1")
    {
    document.myform.subject.value = "Biblical Inquiry";
    }
    else if (document.myform.subject.value =="2")
    {
    document.myform.subject.value = "Benevolence Inquiry";
    }
    else if (document.myform.subject.value =="3")
    {
    document.myform.subject.value = "Website Inquiry";
    }
    }


    //-->
    </Script>






    </HEAD>


    <BODY>
    <BR><BR><BR>

    <FORM name="myform" id="myform" ACTION="/cgi-sys/FormMail.cgi" METHOD="POST"
    onsubmit="return validate ()">

    <table align="center" border="2" cellpadding="10" frame="box" rules="none">
    <tr>
    <td>Your Name </td>
    <td>

    <input type="text" size="30" name="realname" />
    </td>
    </tr>

    <tr>
    <td> Your e-mail address </td>
    <td>
    <INPUT TYPE="text" size="20" NAME="email" />
    </td</tr>

    <tr>
    <td> Subject </td>
    <td>


    <Select name = "sub" id="sub">
    <option value=""> Please select one... </option>
    <option value="1"> Biblical Inquiry </option>
    <option value="2"> Benevolence Inquiry </option>
    <option value="3"> Website Inquiry </select> </option>
    </select>



    </td></tr>


    <tr>
    <td> Question </td>
    <td>
    <textarea name="question" id="question" cols="40" rows="10">
    </textarea> </td></tr>

    <tr align="center">
    <td colspan="2">
    <hr width="100% align="center"> </td></tr>

    <tr>
    <td colspan="2">
    <INPUT TYPE= submit value="Submit Question" /> &nbsp;
    <input type= reset value="Reset Form" />
    </td> </tr>

    </table>



    <input type=hidden name="subject" value = 'document.myform.sub.value'>
    <input type=hidden name="recipient" value ="webmaster@allonschurch.org">
    <input type=hidden name="env_report" value ="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT">


    </FORM>


    </body>

    </html>

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    I've tidied it up a little too:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html lang="en">
    <head>
    <title>Allons Church of Christ</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="keywords" content="Allons Church"> 
    <meta name="description" content="Allons Church of Christ"> 
    <base target="frame2">
    
    <script type="text/javascript">
    <!--
    function validate() {
    if (document.myform.sub.value == "") {
        alert('You must select a subject');
        return false;
        }
    else if (document.myform.realname.value=="") {
        alert('Name cannot be blank');
        return false;
        }
    else if (document.myform.email.value=="") {
        alert('Your email address is required');
        return false;
        }
    else if (document.myform.question.value=="") {
        alert('You left the question box blank. Did you have a question?');
        return false;
        }
    // Change hidden values
    var f=document.myform.sub;
    document.myform.subject.value=f.options[f.selectedIndex].value;
    document.myform.recipient.value=f.options[f.selectedIndex].text;
    return true;
    }
    //-->
    </script>
    
    <style type="text/css">
    <!-- 
    /* cascading style sheet */
    form {margin:60px auto 0 auto; width:70%;} 
    td {white-space:nowrap;}
    -->
    </style>
    
    </head>
    <body>
    
    <form name="myform" id="myform" action="/cgi-sys/FormMail.cgi" method="post" onsubmit="return validate()">
    <table border="2" cellpadding="10" frame="box" rules="none">
        <tr>
            <td><label for="realname">Your Name</label></td>
            <td><input type="text" size="30" name="realname" id="realname" /></td>
        </tr>
        <tr>
            <td><label for="email">Your e-mail address</label></td>
            <td><input type="text" size="20" name="email" id="email" /></td>
        </tr>
        <tr>
            <td><label for="sub">Subject</label></td>
            <td>   
                <select name="sub" id="sub">
                    <option value=""> Please select one... </option>
                    <option value="tom@allonschurch.org">Biblical Inquiry</option>
                    <option value="dick@allonschurch.org">Benevolence Inquiry</option>
                    <option value="harry@allonschurch.org">Website Inquiry</option>
                </select>
            </td>
        </tr>
        <tr>
            <td valign="top"><label for="question">Question</label></td>
            <td><textarea name="question" id="question" cols="40" rows="10"></textarea></td>
        </tr>
        <tr>
            <td colspan="2"><hr width="100%" align="center"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Submit Question" />  
                <input type="reset" value="Reset Form" />
            </td>
        </tr>
    </table>
    
    <input type="hidden" name="subject" value="document.myform.sub.value">
    <input type="hidden" name="recipient" value="webmaster@allonschurch.org">
    <input type="hidden" name="env_report" value="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT">
    </form>
    
    </body>
    </html>

  5. #5
    Join Date
    Sep 2004
    Posts
    18
    Ok, I have put your code in on the server and it wont even send the email now. I then edited my code and have gotten it to display the subject correctly. The only thing left to do is get the "recipient" to change as the user selects from the subject box. The recipient is at the bottom of my code and is being passed to the server script from there. I need to set that = a variable that changes as the subject changes. I can write a function with if statements setting that variable to change to different email addresses as the subject box changes, but how do I call that function from the html?


    Here is my updated code:



    <HTML>
    <HEAD>
    <BASE TARGET="FRAME2">

    <TITLE> Allons Church of Christ </TITLE>

    <style type="text/css">
    </style>

    <Script language="JavaScript" type="text/javascript">

    function validate ()
    {
    if (document.myform.sub.value == "")
    {
    alert('You must select a subject');
    return false;
    }
    else if (document.myform.realname.value == "")
    {
    alert('Name cannot be blank');
    return false;
    }
    else if (document.myform.email.value == "")
    {
    alert('Your email address is required');
    return false;
    }
    else if (document.myform.question.value == "")
    {
    alert('You left the question box blank. Did you have a question?');
    return false;
    }


    document.myform.subject.value=document.myform.sub.value;

    return true;
    }

    //-->
    </Script>






    </HEAD>


    <BODY>
    <BR><BR><BR>

    <FORM name="myform" id="myform" ACTION="/cgi-sys/FormMail.cgi" METHOD="POST"
    onsubmit="return validate ()">

    <table align="center" border="2" cellpadding="10" frame="box" rules="none">
    <tr>
    <td>Your Name </td>
    <td>

    <input type="text" size="30" name="realname" />
    </td>
    </tr>

    <tr>
    <td> Your e-mail address </td>
    <td>
    <INPUT TYPE="text" size="20" NAME="email" />
    </td</tr>

    <tr>
    <td> Subject </td>
    <td>


    <Select name = "sub" id="sub">
    <option value=""> Please select one... </option>
    <option value="Biblical Inquiry"> Biblical Inquiry </option>
    <option value="Benevolence Inquiry"> Benevolence Inquiry </option>
    <option value="Website Inquiry"> Website Inquiry </select> </option>
    </select>



    </td></tr>


    <tr>
    <td> Question </td>
    <td>
    <textarea name="question" id="question" cols="40" rows="10">
    </textarea> </td></tr>

    <tr align="center">
    <td colspan="2">
    <hr width="100% align="center"> </td></tr>

    <tr>
    <td colspan="2">
    <INPUT TYPE= submit value="Submit Question" /> &nbsp;
    <input type= reset value="Reset Form" />
    </td> </tr>

    </table>



    <input type=hidden name="subject" value = 'document.myform.sub.value'>
    <input type=hidden name="recipient" value ="webmaster@allonschurch.org,benevolence@allonschurch.org">
    <input type=hidden name="env_report" value ="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT">


    </FORM>


    </body>

    </html>

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    There should be no problem with the code I gave.
    What was the error message from the browser/server?

  7. #7
    Join Date
    Sep 2004
    Posts
    18
    Your code did not send the email at all. My recipient account did not get the message. It also was putting the email addresses of the recipient into the subject of the email. That is why I changed that. Notice on my latest code. I did not get any errors, but I did not get the desired output. I have got it working now like I want it, but I just need to know how to vary the recipient. You have to notice the "hidden" input at the bottom of my code setting "recipient" to "webmaster@allonschurch.org". This value is submitted as the recipient of the email to the server script when submitted. I just need to be able to check the value of my subject and set this value to the matching recipient.

  8. #8
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Change these lines:
    Code:
    document.myform.subject.value=f.options[f.selectedIndex].text;
    document.myform.recipient.value=f.options[f.selectedIndex].value;

  9. #9
    Join Date
    Sep 2004
    Posts
    18
    I changed those 2 lines and it still did not send the email. I got no errors, but I did not get the email in the recipient account I am using to test it. Is there any way to add to my code so it will vary the recipient of the email. My code does send the message correctly, it just sends it to the email provided in the bottom of my code.
    Last edited by Allonsch; 09-27-2004 at 11:35 AM.

  10. #10
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    This line will change the recipient:
    document.myform.recipient.value="someone@allonschurch.org";

  11. #11
    Join Date
    Sep 2004
    Posts
    18
    I know that, but I need to have it set as a variable so it can change as the subject changes. If I were to add 10 different subjects and sent each one to a different recipient, I need that flexibility. I can do it in a function below my "validate" function, but I am not sure how or where to call it from.

  12. #12
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    The numbers 1 to 3 are the values of the options
    Code:
    if(document.myform.sub.value!="") {
        switch (document.myform.sub.value) {
            case "1" :
              document.myform.recipient.value="biblical@allonschurch.org";
              break;
            case "2" :
              document.myform.recipient.value="benevolence@allonschurch.org";
              break;
            case "3" :
              document.myform.recipient.value="webmaster@allonschurch.org";
              break;
            default :
              document.myform.recipient.value="webmaster@allonschurch.org";
    		}
    	} 
    else {
        alert('You must select a subject');
        return false;
    	}

  13. #13
    Join Date
    Sep 2004
    Posts
    18
    Does this replace my current validation function or does it go underneath it? If it goes underneath it, do I not have to call it somewhere within my html?

  14. #14
    Join Date
    Sep 2004
    Posts
    18
    I have used this case code and tried to set it up in another function and call it with the "onchange" within my <Select>. It is not sending any email, but the validation still works properly. Take a look at my code.


    <HTML>
    <HEAD>
    <BASE TARGET="FRAME2">

    <TITLE> Allons Church of Christ </TITLE>

    <style type="text/css">
    </style>

    <Script language="JavaScript" type="text/javascript">

    function validate ()
    {
    if (document.myform.sub.value == "")
    {
    alert('You must select a subject');
    return false;
    }
    else if (document.myform.realname.value == "")
    {
    alert('Name cannot be blank');
    return false;
    }
    else if (document.myform.email.value == "")
    {
    alert('Your email address is required');
    return false;
    }
    else if (document.myform.question.value == "")
    {
    alert('You left the question box blank. Did you have a question?');
    return false;
    }


    document.myform.subject.value=document.myform.sub.value;
    return true;

    }




    //-->


    </Script>




    <Script language="JavaScript" type="text/javascript">

    function route ()

    if(document.myform.sub.value!="")
    {
    switch (document.myform.sub.value) {
    case "Biblical Inquiry" :
    document.myform.recipient.value=="ajcox@allonschurch.org";
    break;
    case "Benevolence Inquiry" :
    document.myform.recipient.value=="benevolence@allonschurch.org";
    break;
    case "Website Inquiry" :
    document.myform.recipient.value=="webmaster@allonschurch.org";
    break;
    default :
    document.myform.recipient.value=="webmaster@allonschurch.org";
    }
    }
    else {
    alert('You must select a subject');
    return false;
    }


    //-->
    </Script>



    </HEAD>


    <BODY>


    <BR><BR><BR>

    <FORM name="myform" id="myform" ACTION="/cgi-sys/FormMail.cgi" METHOD="POST"
    onsubmit="return validate ()">

    <table align="center" border="2" cellpadding="10" frame="box" rules="none">
    <tr>
    <td>Your Name </td>
    <td>

    <input type="text" size="30" name="realname" />
    </td>
    </tr>

    <tr>
    <td> Your e-mail address </td>
    <td>
    <INPUT TYPE="text" size="20" NAME="email" />
    </td</tr>

    <tr>
    <td> Subject </td>
    <td>


    <Select onchange="route ()" name = "sub" id="sub">
    <option value=""> Please select one... </option>
    <option value="Biblical Inquiry"> Biblical Inquiry </option>
    <option value="Benevolence Inquiry"> Benevolence Inquiry </option>
    <option value="Website Inquiry"> Website Inquiry </select> </option>
    </select>



    </td></tr>


    <tr>
    <td> Question </td>
    <td>
    <textarea name="question" id="question" cols="40" rows="10">
    </textarea> </td></tr>

    <tr align="center">
    <td colspan="2">
    <hr width="100% align="center"> </td></tr>

    <tr>
    <td colspan="2">
    <INPUT TYPE= submit value="Submit Question" /> &nbsp;
    <input type= reset value="Reset Form" />
    </td> </tr>

    </table>



    <input type=hidden name="subject" value = 'document.myform.sub.value'>
    <input type=hidden name="recipient" value = 'document.myform.recipient.value'>
    <input type=hidden name="env_report" value ="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT">
    <input type=hidden name="redirect" value="http://www.allonschurch.org/thankyou.html">



    </FORM>


    </body>

    </html>

  15. #15
    Join Date
    Sep 2004
    Posts
    18
    I got it working. Thanks for all your help. I ended up just using If statements in my "validate" function that set the value of "recipient" for each case. I then set recipient = 'document.myform.recipient.value' at the bottom of my code where it is passed to the server script. It all gets processed at submit so that is the most feasible way to do it anyways.


    Thanks again for all your help. God bless.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles