开发者

Passing a parameter from Radio Button & Check Box, to an email, using an HTML page

开发者 https://www.devze.com 2023-02-18 21:19 出处:网络
I have a form that is HTML, it involves a few string questions, a radio button, and a check box question.After the form is filled out, the info is then passed to an aspx page which sends out an email

I have a form that is HTML, it involves a few string questions, a radio button, and a check box question. After the form is filled out, the info is then passed to an aspx page which sends out an email with the info. I am able to pass the string questions to the email, but am having trouble passing the radio button answer and the check box answers to the aspx and to th开发者_StackOverflow社区en to the email. I have the code for the HTML set, I need help with the code for the ASPX page.(it is VB) Here is the code I have so far.

HTML

<form id="form" method="post" action="SendEmail.aspx" enctype="multipart/form-data" name="questions">

                                            <div class="Qblock">Name:     <input type="text" class="input" value="" name="contact_name" /></div>
                                            <div class="Qblock">Phone #:     <input type="text" class="input" value="" name="contact_phone" /></div>
                                            <div class="Qblock">E-Mail: <input type="text" class="input" value=""  name="contact_email" /></div>

<div class="Qblock">How many years have you been in this industry?<input type="text" class="input" value="" name="contact_long" /></div>


<div class="Qblock">What is your specialty?<input type="text" class="input" value="" name="contact_special" /></div>

<div class="Qblock">Do you have a cell phone? 
<input type="radio" name="P1" value="Yes" /> Yes <input type="radio" name="p1" value="No" /> No <br /></div>

<div class="Qblock">Can you do any of the following? (check all that apply)<br />
                                            <input type="checkbox" name="ckBox" value="CustSer" /> Handle Customer Service<br />

<input type="checkbox" name="ckBox" value="ReadForm" /> Read Expense Reports<br />

<input type="checkbox" name="ckBox" value="NewCust" /> Sign Up New Customers<br /></div>

ASPX

Protected Sub RetrieveValues()
        Dim sTemp As String = String.Empty

        sFromName = "asMark@gmail.com"
        sToName = "asMarkContent@gmail.com"

        sSubject = "Web - Contact Us Inquiry"
        sTemp = String.Format("{0}{1}{2}", "<<< Marketing Opportunities >>>", vbCrLf, vbCrLf)
        sTemp = String.Format("{0}{1}{2}{3}", sTemp, "Name:         ", Request.Params("contact_name"), vbCrLf)
        sTemp = String.Format("{0}{1}{2}{3}", sTemp, "Phone:        ", Request.Params("contact_phone"), vbCrLf)
        sTemp = String.Format("{0}{1}{2}{3}", sTemp, "Email:        ", Request.Params("contact_email"), vbCrLf)
        sTemp = String.Format("{0}{1}{2}{3}", sTemp, "How many years have you been in this industry?      ", Request.Params("contact_long"), vbCrLf)
        sTemp = String.Format("{0}{1}{2}{3}", sTemp, "What is your specialty?        ", Request.Params("contact_special"), vbCrLf)


    End Sub


First, let me introduce you to the StringBuilder which makes code a little easier to read.

Second, radio buttons will come over as a normal name/value pair just like regular text fields so you can process them the same way. Whatever you put in the value on the HTML side will be what's submitted and what you have is good. I would recommend that you make sure the name values both use the same case, however. On the ASPX side you should be fine but its a good practice to assume some things might be case-sensitive. (You have P1 and p1 right now.) You might also want to default one of the radio button to checked, probably the no one. If you don't and someone fills out the form without checking either the email will be empty for that field.

Checkboxes will come over as a comma-separated list so if someone checks all three options you'll get the string CustSer, ReadForm, NewCust. You might want to give the values something a little more descriptive but that's up to you. But you can treat the checkbox the same way as any other form field and just use it by name.

Protected Sub RetrieveValues()
    Dim sTemp As New System.Text.StringBuilder()
    sTemp.AppendFormat("{0}{1}{1}", "<<< Marketing Opportunities >>>", vbCrLf)
    sTemp.AppendFormat("{0}{1}{2}", "Name:         ", Request.Params("contact_name"), vbCrLf)
    sTemp.AppendFormat("{0}{1}{2}", "Phone:        ", Request.Params("contact_phone"), vbCrLf)
    sTemp.AppendFormat("{0}{1}{2}", "Email:        ", Request.Params("contact_email"), vbCrLf)
    sTemp.AppendFormat("{0}{1}{2}", "How many years have you been in this industry?      ", Request.Params("contact_long"), vbCrLf)
    sTemp.AppendFormat("{0}{1}{2}", "What is your specialty?        ", Request.Params("contact_special"), vbCrLf)
    sTemp.AppendFormat("{0}{1}{2}", "Do you have a cell phone?        ", Request.Params("P1"), vbCrLf)
    sTemp.AppendFormat("{0}{1}{2}", "Can you do any of the following?        ", Request.Params("ckBox"), vbCrLf)
End Sub

Lastly, I would recommend using Request.Form over Request.Params. If someone went to your page and manually added any of the form fields to the query string the form field itself would get ignored. For instance, if they navigated to Form.aspx?contact_name=Bob Dole and filled out Bob Smith in the name field you would see Bob Dole on the server side. This is because the query string is searched for before the form fields. In this case it doesn't really matter that much but in future forms some malicious website could link to your website and pass weird values to the form. I'd recommend just always using Request.Form and Request.QueryString.

Okay, the last paragraph was "lastly" so I guess this is "one more thing". The method you are using is only half of the way to ASP.Net. What you have is more of the conversion from ASP classic/PHP to .Net. It's not wrong at all but you are missing the full power of ASP.Net web controls. There's way more than I can write but to give you an idea you would replace:

<input type="text" class="input" value="" name="contact_special" />

With:

<asp:Textbox runat="server" id="contact_special" />

This would allow you to use a RequiredFieldValidator which would use both javascript and server-side code to make sure that the field was filled out:

<asp:RequiredFieldValidator runat="server" ControlToValidate="contact_special" ErrorMessage="Required" />

Don't make this change for this form probably but going forward I would encourage you to investigate the web controls.

0

精彩评论

暂无评论...
验证码 换一张
取 消