Prestashop send the values from smarty to remote server - smarty

I am doing a custom module in Prestashop. In the module frontend I have the smarty form. Inside the smarty form there are some input fields with the submit button. I want that when user enters the values and click on the submit button then those values should be submit to one remote server same like the php curl does. So can someone tell me how to send those values to the remote server and show the response from the server?
Any help and suggestions will be really appreciable. Thanks

To do such a thing your best call is probably to use Ajax with a controller which will be used as your Ajax endpoint url.
There's many examples on how to do this in PrestaShop, here's one:
The JS: https://github.com/PrestaShop/gamification/blob/master/views/js/gamification_bt.js#L167-L184)
The controller:
https://github.com/PrestaShop/gamification/blob/master/controllers/admin/AdminGamificationController.php#L302-L312
You pass all the values of your form through ajax to your controller, then - from this controller - you call your remote server with php Curl and finally you send a response back to your ajax callback, and display everything you want to your user.
Related reading: http://api.jquery.com/jquery.ajax/
Another option would be that your Remote server accept "Cross Domain Origin" (Related reading: Cross domain xmlhttp), but that imply that you can yourself your API endpoint, if you can't first option is the way to go.
Hope this helps !

Related

Eloqua From Submit Via Ajax

I have an Eloqua from with one simple e-mail field on it but I would like to send the data via Ajax to prevent the page from refreshing. I've been looking around on the internet but didn't find anything that's working so far..
Anyone has experience with this?
Thanks.
Ajax probably won't work because of domain restrictions.
You could have a look at blind form links, though:
http://img.en25.com/Web/McAfee/blind-form-link-generator-v2.html
This page generates a link for you, parameters are basically the form field names as set on the form.
This is a simple http request, which will post your data to the form specified.
You can use it as an iframe src, or even an img src. Data will be submitted, page won't reload.
Edit regarding Seb's complaints:
The link was provided as a helper. Please note that the generator creates a link which uses &e= for the email address field value.
I have senn this fail many times, so please use the original form field name set on the eloqua form.
If you are using Eloqua Landing pages or external pages, they will most likely be hosted on a subdomain (Eloqua) or the main domain (external page) of your organization, while all form submits go to a subdomain of eloqua.com.
This means ajax will be blocked. Hence the workaround using the "blind form link" in an iframe, for example.
This link is always the same structure. Example:
http://now.eloqua.com/e/f2.aspx?elqFormName=elq-form-name&elqSiteID=123456&emailAddress=EmailAddress&firstName=value&lastName=value&checkbox=1
You can use now.eloqua.com/e/f2.aspx for all blind form submits.
The parameter "elqFormName" specifies what form the data is being sent to.
"elqSiteID" is necessary to identify your Eloqua instance.
All following parameters refer to the HTML name given for the field on the Eloqua form.
If you send a blind form submit, all processing steps in Eloqua will be triggered, but used in an iframe or img tag for "fake ajax" will not trigger any redirection set on the form in Eloqua.

ajax in wordpress: avoid wp-admin

I need to trigger ajax requests on wordpress frontend in order to get some custom response. lets say to get next/previous post-ID in JSON format.
In production environment the wp-admin directory is inacccessible for http requests by htaccess.
Whats the best practice to solve this problem?
Because AJAX is already used in WordPress’ back end, it has been basically implemented for you. All you need to do is use the functions available. Let’s look at the process in general before diving into the code.
Every AJAX request goes through the admin-ajax.php file in the wp-admin folder. That this file is named admin-ajax might be a bit confusing. I quite agree, but this is just how the development process turned out. So, we should use admin-ajax.php for back-end and user-facing AJAX.
Each request needs to supply at least one piece of data (using the GET or POST method) called action. Based on this action, the code in admin-ajax.php creates two hooks, wp_ajax_my_action and wp_ajax_nopriv_my_action, where my_action is the value of the GET or POST variable action.
Adding a function to the first hook means that that function will fire if a logged-in user initiates the action. Using the second hook, you can cater to logged-out users separately.

Using securesocial services without using its views

I started integrating SecureSocial in my play/scala app, but I don't really like all the redirects it does between it's different views.
example - try to login from it's default login page and if you put in a wrong pass you will be redirected to a different page (url) but with the same login form. the only thing that is different is that there is an error message...
I want a simple login form (user/password provider) at the corner of my main page that submits it's data using ajax, this data is validated on the server and a response is made to either display error message/s or change the window.location.
Next to this form I will put a link to go to a more advanced login page that adds the option to use other providers like fb/twitter etc..
But from that page I also want to use ajax to submit the details and get the response.
I tried to browse into the SecureSocial source but got a little lost in there.
Can any one give me an idea how to use SecureSocial's but without using any of it's views?
NOTE: I'm not interested in customizing their views, It's not just a CSS/design issue, I want to handle the login details Ajaxly and not with normal form submission followed by redirects...
After some more rummaging around in SecureSocial code I got a better understanding of how it operates.
You can use any of the providers you listed in the play.plugins file seperatly to authenthicate the user's info from your own login/auth code. just make sure you send the right parameters that the provider needs.
I liked the way SecureSocial's ProviderController class dynamically decided what provider to use, based on a parameter. But I didn't like the responses it made - redirect.. I wanted to respond to an ajax request with some data and let the client side js handle it.
This is my solution:
pretty much copy all of ProviderController code to my own Auth.scala file (a Controller).
Changed the redirects related to "case ex, case _", kept the redirect on successful auth as it adds the SecureSocial session key related to the user.
Removed all the SecureSocial related routes from my routes file.
Put an additional hidden field with the logintype (userpass/google/fb/etc...) and configured my login ajax post to sent this along with the post to my Auth controller.
If you need more info comment here and I'll edit the answer.

How do you convert a form with method="get" to a mod_rewrite url?

I have a PHP MVC Web App and Apache mod_rewrite rules already working fine, but when I create forms using method="get", the submitted URL looks like
contact/submit?a=b&c=d
I would like my form to submit to
contact/submit/a/b/c/d
Both posting and getting the form work fine on the server side, but when using post method, the back button always asks for reposting the form values and furthermore I want the strings in the URL for SEO. I think JQuery might let me intercept the form submit event and refresh to the url dynamically, but it it seems there must be an easier way to do it that I am missing.
You could use the POST->REDIRECT->GET pattern that Spring Web Flow utilizes. This would allow you to post as you wish and then redirect to contact/submit/a/b/c/d. It would also solve the problem with the back button asking you if you want to resubmit your form data. See this related article.
The GET method uses standard query string arguments to pass form data via an HTTP GET request.
The HTTP GET request is not intended to modify any data on the server. POST is designed for modifying data on the server.
GET may be cached. POST will not.
/a/b/c/d is not a standard format (as in RFC) for passing data. However, for requesting data or URLs to post to, that has become popular.
So, if you are updating server data, just use a POST -> REDIRECT -> /a/b/c/d.
If you are just reading data from the server, then you will need to use a bit of Javascript to read your form values and construct a query string, and then go to it with window.location = ...
Have fun!

Appropriate redirection of forms that use AJAX

I have many forms that use AJAX (w/ jQuery) for validation and data submission. When a form is filled out correctly, I use window.location to redirect the page after I get an acceptable response from the PHP script. On the new page, I use a session variable (set after the AJAX calls) to display the appropriate content. Please tell me if this is standard practice or please give me some suggestions.
Thanks!
Is there a reason you would use a $_SESSION variable to store the post-submission content? Standard practice would be to validate the form via AJAX but submit it in the standard way (i.e. via $_GET or $_POST) after validation. This way you don't need to store anything to a session and you'll likely have less to debug as you'll be submitting the form and displaying its results in the most widely-accepted way.
The benefit of AJAX is typically so that you can submit the form without actually having to do the redirect/refresh. You could get the same functionality by simply having your form POST to the destination URL, redirect to the appropriate place from there or send them back to the form displaying any errors that may have occurred. You could use AJAX to validate the form before the submission to save your users a redirection back to the form to fix their errors, but this is really just a convenience for them. Also, you will have to validate any user data on the server side once it has been submitted, as you can't rely on client-side validation, so you might as well forget the AJAX validation.

Resources