ASP Form submission Page Not grabbing All Information
Hello, Im creating a page in ASP with javascript (which grabs form details) for an insurance company contact form and the form doesnt seem to be grabbing ALL the information and sending it to an email. It successfully sends all the information to the test email upon submission EXCEPT for the drop down menu options towards the bottom. All it shows in the email is the check boxed options and not what was selected in the drop down menu WITH the check boxes (towards the bottom). Below is the code behind the page currently:
Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Net.Mail;
public partial class contactus : System.Web.UI.Page
{
string _devServerList = ConfigurationSettings.AppSettings["devHosts"];
protected void Page_Load(object sender, EventArgs e)
{
site mp = (site)this.Master;
mp.activateNav("contact");
}
protected void emailForm(object sender, EventArgs e)
{
SmtpClient mailer = new SmtpClient();
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("test@test.com", "Test")); //Test account
msg.From = new MailAddress("test@test.com", "Contact Request Test"); //Test Account
msg.Subject = "Company Contact Form Request";
msg.IsBodyHtml = false;
msg.Body = getBody();
if (!isOnDevServer()) mailer.Send(msg);
mailer = null;
contactForm.Visible = false;
contactTY.Visible = true;
}
private string getBody()
{
StringBuilder returnValue = new StringBuilder();
if (clientYes.Checked) returnValue.AppendLine("Existing Client: Yes");
if (clientNo.Checked) returnValue.AppendLine("Existing Client: No");
if (name.Value != "") returnValue.AppendLine("Name: " + name.Value);
if (title.Value != "") returnValue.AppendLine("Title: " + title.Value);
if (companyName.Value != "") returnValue.AppendLine("Company/Institution Name: " + companyName.Value);
if (address.Value != "") returnValue.AppendLine("Address: " + address.Value);
if (city.Value != "") returnValue.AppendLine("City: " + city.Value);
if (state.Value != "") returnValue.AppendLine("State: " + state.Value);
if (zipCode.Value != "") returnValue.AppendLine("Zip Code: " + zipCode.Value);
if (email.Value != "") returnValue.AppendLine("Email Address: " + email.Value);
if (phone.Value != "") returnValue.AppendLine("Phone: " + phone.Value);
if (question.Value != "")
{
returnValue.AppendLine("");
returnValue.AppendLine("Their question or request is: " + question.Value);
}
if (learnAdvertisement.Checked || learnTrade.Checked || learnMail.Checked || learnReferral.Checked || learnPrint.Checked || learnDigital.Checked )
{
returnValue.AppendLine("How did they learn about Us?: ");
if (learnAdvertisement.Checked) returnValue.AppendLine(" Advertisement");
if (learnTrade.Checked) returnValue.AppendLine(" Trade Show");
if (learnMail.Checked) returnValue.AppendLine(" Direct Mail");
if (learnReferral.Checked) returnValue.AppendLine(" Referral");
if (learnPrint.Checked) returnValue.AppendLine(" Print");
if (learnDigital.Checked) returnValue.AppendLine(" Digital");
}
return returnValue.ToString();
}
public bool isOnDevServer()
{
if (_devServerList.IndexOf(",") > 0)
{
foreach (string r in _devServerList.Split(",".ToCharArray()))
if (Request.Url.Host.ToString() == r) return true;
}
else if (_devServerList.Length > 0)
{
if (Request.Url.Host.ToString() == _devServerList) return true;
}
return false;
}
}
That allowed the list to populate, but when I submitted the form it didnt show the options in the drop down list. Do you know why the chosen option in the drop down wouldnt show up in the submitted email? I tried to adjust it myself but then got compilation errors. With the edits you suggested, it seems that the code-behind .cs file needs to be changed too, Im just not sure what.
For reference, I think the error occurs at the following part:
Code:
if (learnPrint.Checked || learnDigital.Checked || learnAdvertisement.Checked || learnTrade.Checked || learnMail.Checked || learnReferral.Checked)
{
returnValue.AppendLine("How did they learn about Insuritas?: ");
if (learnAdvertisement.Checked) returnValue.AppendLine(" Advertisement");
if (learnTrade.Checked) returnValue.AppendLine(" Trade Show");
if (learnMail.Checked) returnValue.AppendLine(" Direct Mail");
if (learnReferral.Checked) returnValue.AppendLine(" Referral");
if (learnPrint.Checked) returnValue.AppendLine(" Print");
if (learnDigital.Checked) returnValue.AppendLine(" Digital");
}
Is there something above that says "if DROPDOWN" or something, which allows the form to populate the email with what was chosen in the drop down menu?
Last edited by aaronrusso; 02-19-2013 at 07:16 PM..
Love the graphic explanation Regardless, I did use two unique IDs (the first was learnPrintBox and the other was learnPrint). Code is now set up like this:
So there should be two drop downs with check boxes next to them, and when you hit submit on the page, it should populate what was checked AND chosen in the drop down, and show up on the email sent. It doesnt. With the above code being used, it does compile, but the options are just not showing up when selected and submitted.
Im not sure what you are doing, but Im not using VB to write this. Here is the entire code behind file, named contactus.aspx.cs:
Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Net.Mail;
public partial class contactus : System.Web.UI.Page
{
string _devServerList = ConfigurationSettings.AppSettings["devHosts"];
protected void Page_Load(object sender, EventArgs e)
{
site mp = (site)this.Master;
mp.activateNav("contact");
}
protected void emailForm(object sender, EventArgs e)
{
SmtpClient mailer = new SmtpClient();
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("test@test.com", "To Test")); //Test account
msg.From = new MailAddress("test2@test.com", "From Test2"); //Test account
msg.Subject = "Contact Form Request";
msg.IsBodyHtml = false;
msg.Body = getBody();
if (!isOnDevServer()) mailer.Send(msg);
mailer = null;
contactForm.Visible = false;
contactTY.Visible = true;
}
private string getBody()
{
StringBuilder returnValue = new StringBuilder();
if (clientYes.Checked) returnValue.AppendLine("Existing Client: Yes");
if (clientNo.Checked) returnValue.AppendLine("Existing Client: No");
if (name.Value != "") returnValue.AppendLine("Name: " + name.Value);
if (title.Value != "") returnValue.AppendLine("Title: " + title.Value);
if (companyName.Value != "") returnValue.AppendLine("Company/Institution Name: " + companyName.Value);
if (address.Value != "") returnValue.AppendLine("Address: " + address.Value);
if (city.Value != "") returnValue.AppendLine("City: " + city.Value);
if (state.Value != "") returnValue.AppendLine("State: " + state.Value);
if (zipCode.Value != "") returnValue.AppendLine("Zip Code: " + zipCode.Value);
if (email.Value != "") returnValue.AppendLine("Email Address: " + email.Value);
if (phone.Value != "") returnValue.AppendLine("Phone: " + phone.Value);
if (question.Value != "")
{
returnValue.AppendLine("");
returnValue.AppendLine("Their question or request is: " + question.Value);
}
if (learnPrint.Checked || learnAdvertisement.Checked || learnTrade.Checked || learnMail.Checked || learnReferral.Checked || learnDigital.Checked)
{
returnValue.AppendLine("How did they learn about Insuritas?: ");
//if (learnWeb.Checked) returnValue.AppendLine(" Web Search");
if (learnAdvertisement.Checked) returnValue.AppendLine(" Advertisement");
if (learnTrade.Checked) returnValue.AppendLine(" Trade Show");
if (learnMail.Checked) returnValue.AppendLine(" Direct Mail");
if (learnReferral.Checked) returnValue.AppendLine(" Referral");
if (learnPrint.Checked) returnValue.AppendLine(" Print");
if (learnDigital.Checked) returnValue.AppendLine(" Digital");
}
return returnValue.ToString();
}
public bool isOnDevServer()
{
if (_devServerList.IndexOf(",") > 0)
{
foreach (string r in _devServerList.Split(",".ToCharArray()))
if (Request.Url.Host.ToString() == r) return true;
}
else if (_devServerList.Length > 0)
{
if (Request.Url.Host.ToString() == _devServerList) return true;
}
return false;
}
}
As you can see, there are no places calling "text" or anything of the sort. The above code works with the form and allows the check boxes chosen to show up in the email. Using the above code however, I am trying to get drop down values to also show up in the email.
Finally got it to work! Thanks. I had to jerry-rig a combination of asp form data and html input/label fields for it to work with the current code-behind file though. No I didnt make the form, some "professional" web company made it and left as I came into the company. Id make a new one but the company doesnt want to change it for some reason. Oh well... thanks again!