eZ Community » Forums » Developer » ajax collected info legacy stack
expandshrink

ajax collected info legacy stack

ajax collected info legacy stack

Sunday 09 November 2014 10:06:37 am - 15 replies

Hi guys, can you help me?

I've a form to collect information from anonymous frontend, I want to do this via ajax call.

In layout.ini I've created this 

[ajax]

PageLayout=ajax_contact_form.tpl

UseAccessPass=false

and in the tpl file of form this

    <script>

        function newComment()

 {ldelim}

       var _params = 

       {ldelim}

             ClassIdentifier:"feedback_form",

             NodeID:"{$formNode.node_id}",

             ContentLanguageCode:"{ezini( 'RegionalSettings', 'ContentObjectLocale', 'site.ini')}",

             NewButton:"Send"

       {rdelim};

       loadAjaxContent({"layout/set/ajax/content/action/"|ezurl()},'footer_contact',_params);

       return false;

 {rdelim}

    </script>

 

the loadAjaxContent function here

function loadAjaxContent(_url,_el,_params,_method){

    var params = (_params==null)? {} : _params;

    var method = (_method==null)? 'post' : _method;

 

    $.ajax({

       type: method,

       url: _url,

       data: _params,

       dataType: "html",

      cache: false,

       success: function(response){

       //console.log(response);

       //$('#'+_el).hide();

       //$('.message-form-success').show();

       $('#'+_el).html(response);

       },

     error:function (xhr, ajaxOptions, thrownError){

         console.log(xhr.status);

     }    

 });

}

 

but the ajax call return me a 401 error

Modified on Sunday 09 November 2014 10:11:28 am by Paolo Gasola

Sunday 09 November 2014 12:06:10 pm

Hello Paolo,

I'm fairly certain your ajax call goes no where.

You can tail your web server access log to see that when you submit your javascript call it is not hitting your web server at all.

Meaning that you should as a best practice at least include the protocol and hostname in your ajax call url.

Once it is hitting your web server it should work much better.

I hope this helps!

Cheers,
Heath 

Modified on Sunday 09 November 2014 12:11:03 pm by // Heath

Sunday 09 November 2014 12:23:17 pm

Yes, in my access log there are first a 404 after a 401 error.

How can I resolve it? 

If I call layout/set/ajax/ the request work correctly if I call layout/set/ajax/content/action/

the request give error.

This is my ajax_contact_form.tpl 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="{$site.http_equiv.Content-language|wash}" xml:lang="{$site.http_equiv.Content-language|wash}">

<head>

</head>

<body>

 

{$module_result.content}

 

</body>

</html>

Modified on Sunday 09 November 2014 12:23:47 pm by Paolo Gasola

Sunday 09 November 2014 12:56:12 pm

Hello Paolo,

Well first you have so many problems it is hard to know where to start first.

Lets start with http error codes, http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error

You say your getting 401, that sounds like your webserver requires authentication to access the site. This would cause problems for your ajax requests I think. If your webserver does not require authentication I begin to wonder if your anonymous role policy permissions are set correctly to permit anonymous users to create new comment content objects.

You say you get 404 after 401. Could you provide a copy of your web server logs so we can review them. I'm not sure how to troubleshoot this error without additional information. Have you tried clearing all caches?

As for your layout/set/ajax pagelayout template it looks fine. I doubt the problems your having are with it.

I re-read your first post and I wonder if your not sending enough required html form variables to actually create new content objects. The form submission could be erroring internally because of this.

I think you have your work cut out for you blunk.gif Emoticon

Try first perfecting the setup of adding this content without the ajax part and then extending it with ajax afterwards. 

Best of luck.

Cheers,
Heath 

Monday 10 November 2014 2:09:07 am

Hello Paolo,

Please try to refrain from cross posting in the forums, especially to very old forum threads.

You may be borrowing code from that thread (bad idea) but those people have prolly all moved on at this point so you prolly won't be able to get much help there.

 

One other thing you could clarify for us is what are you trying to do exactly. Are you trying to post collected information or create new object?

Your source code is all from a source trying to create a new comment which is completely different than trying to store collected information. But in the forum thread you talk about collected information which is more than confusing.

 

Like I said before you need to get the collected information setup working first before you try to make the form submission ajax based. Especially as a new user of eZ Publish.

You need to re-write all your javascript code and use the form variables required by the collected information system because what you have now is written the wrong way and confusing to anyone trying to help. Please store your source code in literal text blocks using the editor toolbar or as gists.

Also you would be very wise to read and learn from the kernel source code on this topic as it contains all the answers you would need really (i've done this myself several times when working with the information collection system). 

Let me know what you think ... and please answer the questions I've asked in this thread.

Cheers,
Heath 

Monday 10 November 2014 6:58:20 am

Hello Heath,

sorry for cross posting.

I've tried to clear all caches and I've tried to send to my ajax.tpl directly $('#form').serialize();

and now there are not error and the ajax call works but, not store the collected information.

Without the ajax call the collected information works fine.

Monday 10 November 2014 7:26:41 am

Hello Paolo,

Thank you for stoping the cross posting.

Now, lets start again.

First can you please rewrite your javascript to remove all notion of adding a new comment.

Second lets remove the notion of serialize from your javascript code as I think that is a poor solution (I've not used this myself) and replace it with static post variable assignments. I think this is key to why your collected information record is not being stored correctly. 

Third please post your rewritten javascript source code as a gist and embed it here in the forum so we can clearly review it in detail and see just what part of your javascript is not working.

Fourth, Please compare the html form variables required to store a collected information object record to your ajax code, your missing a lot of required variables by the collected information storage system! Here are some missing post variables:

  • ActionCollectInformation = 'Send'
  • ContentNodeID = your_node_id
  • ContentObjectID = your_object_id
  • ViewMode = 'full'

Thank you for your continued support!

Cheers,
Heath 

Modified on Monday 10 November 2014 7:35:25 am by // Heath

Monday 10 November 2014 7:42:24 am

Hi Heath,

how I put inputs of the forms? For example Name, mail, subject, message?

Modified on Monday 10 November 2014 7:50:09 am by Paolo Gasola

Monday 10 November 2014 8:01:52 am

Hello Paolo,

Remember that the content object attributes are not named in the simple manner your using in your last message. The attributes are all named uniquely like 'ContentObjectAttribute_ezstring_data_text_9390'. This is the style post form variables which must be used. 

Now I'm not a javascript expert so I don't think I'll be of much help there. Remember that the javascript aspects of this solution are not really eZ Publish problems. There are many other forums and resources to help you in search of javascript solutions to building your ajax post back to eZ.

I did do a quick google and found these resources:

 

Cheers,
Heath 

Modified on Monday 10 November 2014 8:02:10 am by // Heath

Monday 10 November 2014 8:11:51 am

Hello Heath,

I've rewrite my JS code

 

    {literal}

    <script>

        function newComment()

{

      var _params = 

      {

            ActionCollectInformation: 'Send',

            ContentNodeID: {/literal}{$formNode.node_id}{literal},

            ContentObjectID: {/literal}{$formNode.object.id}{literal},

            ViewMode: 'full',

            ContentObjectAttribute_ezstring_data_text_639: $('input[name="ContentObjectAttribute_ezstring_data_text_639"]').val(),

            ContentObjectAttribute_ezstring_data_text_640: $('input[name="ContentObjectAttribute_ezstring_data_text_640"]').val(),

            ContentObjectAttribute_ezstring_data_text_641: $('input[name="ContentObjectAttribute_ezstring_data_text_641"]').val(),

            ContentObjectAttribute_data_text_642: $('textarea[name="ContentObjectAttribute_data_text_642"]').text()

      };

      loadAjaxContent({/literal}{"layout/set/ajax/content/action/"|ezurl()}{literal},'footer_contact',_params);

      return false;

}

    </script>

    {/literal}

 

now I see that in the ajax.tpl create a form with the inputs compiled with the data but not store information.

Monday 10 November 2014 8:45:32 am

Hello Paolo,

Please use the source code literal text buttons in the forum message tool bar when posting source code! Yes it takes more time and care to post source code to use this feature than without using this feature but without using it your source code is painful to try to read.

Also please change your javascript source code to properly quote (double quote recommended) your param values (for each field) or else your ajax post is invalid as there is not way for javascript to know where one param value ends and another begins.

Also remember you must escape quote characters your javascript param values to ensure that quotes in the values of the form fields do not break your form submission post via ajax.

Cheers,
Heath 

Modified on Monday 10 November 2014 8:46:08 am by // Heath

Monday 10 November 2014 9:09:48 am

Hello, excuse me for not use it happy.gif Emoticon

Now, I've escaped the strings, the ajax post works but I've the problem of not store infos. The response give me a form with compiled inputs

In your opinion how can I resolve it?

Monday 10 November 2014 9:32:14 am

Hello Paolo,

I think that the form in the response would also contain an error message explaining what was wrong with the submission.

Can you save the response to disk and view it in the browser to see exactly what error messages are being displayed.

Also at this point could you please re-post your current source code as a Gist and embed it here in the forum message.

Cheers,
Heath 

Monday 10 November 2014 3:08:19 pm

Hi Heath, you're true there was an error in my Javascript.

This is the correct an "up & running" version happy.gif Emoticon

 {literal}             function newContact(){          var noerror=0;          if ($('input[name="ContentObjectAttribute_ezstring_data_text_639"]').val() == ''){           $('input[name="ContentObjectAttribute_ezstring_data_text_639"]').addClass('error');           return false;          }else{           $('input[name="ContentObjectAttribute_ezstring_data_text_639"]').removeClass('error');          }          if ($('input[name="ContentObjectAttribute_data_text_640"]').val() == ''){           $('input[name="ContentObjectAttribute_data_text_640"]').addClass('error');           return false;          }else{           if(validateEmail($('input[name="ContentObjectAttribute_data_text_640"]').val()) != 0){            $('input[name="ContentObjectAttribute_data_text_640"]').addClass('error');            return false;           }else{            $('input[name="ContentObjectAttribute_data_text_640"]').removeClass('error');           }           $('input[name="ContentObjectAttribute_data_text_640"]').removeClass('error');          }          if ($('input[name="ContentObjectAttribute_ezstring_data_text_641"]').val() == ''){           $('input[name="ContentObjectAttribute_ezstring_data_text_641"]').addClass('error');           return false;          }else{           $('input[name="ContentObjectAttribute_ezstring_data_text_641"]').removeClass('error');          }          if ($('textarea[name="ContentObjectAttribute_data_text_642"]').val() == ''){           $('textarea[name="ContentObjectAttribute_data_text_642"]').addClass('error');           return false;          }else{           $('textarea[name="ContentObjectAttribute_data_text_642"]').removeClass('error');          }       var _params =         {            ActionCollectInformation: 'Send',               ContentNodeID: {/literal}{$formNode.node_id}{literal},               ContentObjectID: {/literal}{$formNode.object.id}{literal},               ViewMode: 'full',               ContentObjectAttribute_ezstring_data_text_639: $('input[name="ContentObjectAttribute_ezstring_data_text_639"]').val(),               ContentObjectAttribute_data_text_640: $('input[name="ContentObjectAttribute_data_text_640"]').val(),               ContentObjectAttribute_ezstring_data_text_641: $('input[name="ContentObjectAttribute_ezstring_data_text_641"]').val(),               ContentObjectAttribute_data_text_642: $('textarea[name="ContentObjectAttribute_data_text_642"]').val()       };    loadAjaxContentCollectedInfo({/literal}{"layout/set/ajax/content/action/"|ezurl()}{literal},'footer_contact',_params);       return false;   }        {/literal}

Modified on Monday 10 November 2014 6:00:30 pm by Paolo Gasola

Monday 10 November 2014 5:41:08 pm

Hello Paolo,

I'm glad to hear you were able to get your javascript errors settled and working.

Can you re-edit your last message and add line breaks to your source code literal text to make it easier to read? This would go a long way towards helping others in the future who read this forum thread.

Also remember with the way you have written your javascript (seems static), when you change the form attributes (Add/Remove) in the content class you will have to rewrite your javascript code to mirror these changes.

If this thread is finally solved please check the check box at the top of the thread.

Best wishes!

Cheers,
Heath 

Modified on Monday 10 November 2014 5:42:31 pm by // Heath

Monday 10 November 2014 6:09:46 pm

It is static , but I thank eZpublish that has the policies that allow the admin user to inhibit the elimination of the form to editor user happy.gif Emoticon

Thank you very much for help!

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from