Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Steve 472 posts 1216 karma points
    Jan 22, 2016 @ 19:46
    Steve
    0

    Post Form Data Without Contour

    I have some Javascript that checks for a users cookie when they land on our page, and if they haven't been on the page before, a form appears to log their name and other data as additional cookies. I would like to also post this form data to a file on the Umbraco server just don't know how, any suggestions?

    Thanks!

  • Marc Goodson 2157 posts 14435 karma points MVP 9x c-trib
    Jan 24, 2016 @ 20:57
    Marc Goodson
    0

    Hi Steve

    An Umbraco Surface Controller is designed to handle the posting of a form:

    https://our.umbraco.org/documentation/reference/routing/surface-controllers

    So if you have a simple form to post name, email etc, you'd create a plain model class eg

    public class ContactModel
    {
        public string Name { get; set; }
        public string Email { get; set; }
    }
    

    Then put your form in a partial view like so:

    @model ContactModel 
    @using (Html.BeginUmbracoForm("Submit", "Contact"))
    {
        <div>
            @Html.TextBoxFor(m => m.Name)
        </div>
        <div>
            @Html.TextBoxFor(m => m.Email)
        </div>
        <input type="submit" value="Submit" />
    }
    

    When the form posts it will look for a surfacecontroller called ContactController and call it's submit action...

    eg

    public class ContactController : SurfaceController {
    public ActionResult Submit(ContactModel model)
            {
                if (!ModelState.IsValid)
                   return CurrentUmbracoPage();
    
    // do stuff with posted values...
    return RedirectToCurrentUmbracoPage();
    }
    }
    

    If I've got the wrong end of the stick, and you'd be posting the data asynchronously via javascript, then create an UmbracoApiController to receive the posted data:

    https://our.umbraco.org/documentation/reference/routing/webapi/

    public class TrackUserController : UmbracoApiController
    
    [System.Web.Http.HttpPost]
    public HttpResponseMessage TrackUser(ContactModel postedData){
    
    // do something to persist tracked data
    
    return new HttpResponseMessage(System.Net.HttpStatusCode.OK);
    
    
    
    }
    
  • Steve 472 posts 1216 karma points
    Jan 25, 2016 @ 14:17
    Steve
    0

    We are using webforms not MVC unfortunatly.

    I've seen some Umbraco sites that wrap the form inputs in a div that calls a Javascript function, I am assuming something to post the form data.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 25, 2016 @ 14:34
    Dennis Aaen
    0

    Hi Steve.

    Could this package maybe be an option for you depending on which Umbraco version that you are using.

    https://our.umbraco.org/projects/website-utilities/cultiv-razor-contact-form/

    Hope this can help you.

    /Dennis

  • Steve 472 posts 1216 karma points
    Jan 25, 2016 @ 14:41
    Steve
    0

    Thanks Dennis, but I really need more of a way to post to a data base or save to a file on the server. I think there would be too many form submissions to keep track of with just emails.

  • Thomas Beckert 193 posts 469 karma points
    Jan 25, 2016 @ 14:44
    Thomas Beckert
    0

    Hi, Steve,

    you could either use an UserControl (userControl.ascx + Codebehind, created in Visual Studio) and integrate it by a Macro into your umbraco-page or you just use an PartialView, that handles the post.

    Here is an example:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage 
    
    @{  
    
    if(String.IsNullOrEmpty(Request["submit"]))     {
         <form id="formKontakt" class="form-box register-form contact-form" role="form" data-toggle="validator">            <h3 class="title">@Model.MacroParameters["Headline"].ToString()</h3>
                    <div class="form-group">
                        <label>@Umbraco.Field("#Name"): <span class="required">*</span></label>
                        <input id="name" name="name" type="text" class="input-md form-control"  required>
                        <div class="help-block with-errors"></div>
                    </div>
                    <div class="form-group">
                        <label>Ihre Email Addresse: <span class="required">*</span></label>
                        <input id="mail" name="mail" type="email" class="input-md form-control" required>
                        <div class="help-block with-errors"></div>          </div>                          <div class="form-group">
                        <label>Ihre Nachricht:</label>
                        <textarea id="inhalt" name="inhalt" class="input-md form-control" rows="8" required></textarea>            
                        <div class="help-block with-errors"></div>
                     </div>                         <input type="hidden" value="sent" id="submit" name="submit" />
                   <button class="btn btn-mod btn-border btn-large btn-round">submit</button>
                </form>  
    
          }  else        {
            @* Handle Form *@   
    
        <div class="col-md-12">
    
                        <h3>Model.MacroParameters["HeadlineSend"].ToString()</h3>
                        @Html.Raw(@umbraco.library.ReplaceLineBreaks(Model.MacroParameters["bodyTextSend"].ToString()))
                 </div>          } }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies