Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

html form loop with type

I have the following codes to just loop inside of the html form and get the field name and value
it works as long as it is <input type=text>
I have textarea now inside of the form and do not know how to make it works.

Can you help? I tried to 'type' but it shows undefined.



function SubmitAPI(apiurl_value, stagecompleted_value, successmsg_value, emailsubject_value, emailaddress_value, emaildestination_value) {
    var x = $('#formdiv *').serializeArray();
    var data = {};
    var apiurl = apiurl_value;
    var stagecompleted = stagecompleted_value;
    var successmsg = successmsg_value;
    var emailsubject = emailsubject_value;
    $.each(x, function (i, field) {
        data[field.name] = field.value;
    });
    var jsondata = JSON.stringify(data);
    $.ajax({
        url: apiurl,
        type: "POST",
        data: { 'message': jsondata.replace(/\"/g, "'"), 'email': emailaddress_value, 'emailsubject': emailsubject, 'emaildestination': emaildestination_value },
        dataType: 'json',
        success: function (data) {
            if (data.stage === stagecompleted) {
                alert(successmsg);
                document.location.reload();
            }
        },
        error: function (msg) { alert(msg); }
    });
    return false;
}

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi,


If you're trying to send the form contents through your AJAX request, you can just use the serialize() method on the form:


function SubmitAPI(apiurl, stagecompleted, successmsg, emailsubject, email, emaildestination) {  
    $.ajax({
        url: apiurl,
        type: 'post',
        dataType: 'json',

        data: {
            message: $('#formId').serialize(),
            email,
            emailsubject,
            emaildestination
        },

        success: function(data) {
            ...
        }

Open in new window




Avatar of ITsolutionWizard

ASKER

Thanks but how to capture all of the data on the back using controller c#
Hi,

Nothing changes from the server end of things. You do the same as you did previously. This was a question on how to add all your form fields to the AJAX request and I've shown you how to do that.
I did try and it does not work
Please show me how to do it on code behind controller. Thanks
Hi,

I'm a little confused. In your original question, you just asked how to send the form data via AJAX, and you said that what you had currently worked, so I'm assuming you already have the code-behind set up and working. The code I've shown has nothing to do with the backend - it just sends the data to your server in the same way as you previoulsy did.

If you have a question about handling the request in your C# code-behind, then you'll need to open a new question as this one was about HTML and jQuery.

You can post up your controller code if you want me to have a look at it
Thank you. I appreciate your helps

  [HttpPost]
        [ValidateInput(false)]
        public JsonResult ReceiveJson(string message, string email, string emailsubject, string emaildestination)
        {
            #region str object
            string stageCompletedLabel = System.Configuration.ConfigurationManager.AppSettings["StageCompletedLabel"];
            string emailReceiver = emaildestination;
            Domain.Contact c = new Domain.Contact();
            string guidKey = Guid.NewGuid().ToString();
            string fromEmail = email;
            var responseStr = JsonConvert.DeserializeObject<Dictionary<string, string>>(message);
            string dataList = "";
            string breakInHtml = "<br>";
            foreach (var kv in responseStr)
            {
                #region get data                 
                dataList = dataList + (kv.Key.Replace("_", " ") + ": " + kv.Value + breakInHtml);
                #endregion
            }
            #region email
            DomainHelper.SMTP.SendEmailWithOutAttachment(emailsubject, dataList, fromEmail, emailReceiver);
            #endregion

            #endregion
            c.stage = stageCompletedLabel;
            return Json(c, JsonRequestBehavior.AllowGet);
        }

Open in new window

Ahh right.

Your code-behind is expecting JSON, so we'll need to edit the AJAX call a little. My code didn't convert the data to JSON. In my code, just convert the data:

data: {
    message: JSON.stringify( $('#formId').serialize() ),
    email,
    emailsubject,
    emaildestination
},

Open in new window

If that doesn't work, just debug your code behind and see what's being received in the message variable
How?
Or you can show me by some codes sample in the controller
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland 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