[SOLVED] Ajax subscribe api

Hi can you specify which instructions, thanks

Sorry. Introduction is the wrong word. I use the subscription_form_example.html. I load this file to my server. I edit the url and the ids.

Everythings seems correct. But when I click the subscribe Button I get the Error Message.

So I test my modified html script in my sub domain where phplist runs. It works! So what i’m doing wrong? I added

define('ACCESS_CONTROL_ALLOW_ORIGIN', 'www.mydomain.de');
define('PUBLIC_PROTOCOL','https');

to my config file.

Now it works! The Problem was that i have include the jquery via http.

2 Likes

Hi all,

I’ve used the example above and my form works great. There is however no message to the user once he/she submit the form and they don’t know if it works.
Does the code below suppose to throw a message on success? If yes, it doesn’t.

Sign up

It is working however the whole form fine.
Any idea how I can throw a success message on submit?

Thanks a lot

@mrdebian You might need to use your browser’s web tools (in Firefox it is web console) to see exactly what is being returned by phplist.

Do you need to enable ACCESS_CONTROL_ALLOW_ORIGIN (if your form is on a different domain to phplist).

@duncanc thanks for posting your code. I was able to get it work after enabling ACCESS_CONTROL_ALLOW_ORIGIN.

The problem I have now is that the confimation is returning the complete thank you page and not displaying just the message defined in the code. It’s not even displaying the code set in the “Text to display when subscription with an AJAX request was successful” section of the subscribe page config.

Any idea why that might be happening?

thx

@jpbedoya are you using the correct phplist page in the url? It needs to be “asubscribe” not “subscribe”.

@duncanc that was it. didn’t realize that a piece. Thank you!

Now, is it expected that it will show the success message from the subscribe page and not the one in the code?

1 Like

Hello

I am very glad to have found this thread! I have tried to implement this type of ajax subscribe functionality on my own many times with various hacky homecooking methods not realizing that the functionality was built in!

However I am having the same FAIL results as other people who had difficulty with this at first.

For testing I am using the same (localhost) domain for the my phplist and the page serving the form so my ajax url is: url: '/lists/index.php?p=asubscribe&id=4', (subscribe page 4 is a subscribe page that defaults to html email and does not display email address confirmation field).

Just in case it would help I added define('ACCESS_CONTROL_ALLOW_ORIGIN', '*'); to /lists/config/config.php.

I changed the hidden list input to a list that I have have set up <input type="hidden" name="list[3]" value="signup" />

Other then these changes I am using the code as supplied (generously!) by @duncanc

I am using, PhpList v3.3.1 and PHP v7.0.22

Inspecting the request with dev tools reveals the following:
Query string
p: asubscribe id: 4
Form data
email: me@mydomain.com attribute1: myname htmlemail: 1 list[3]: signup subscribe: subscribe VerificationCodeX:

Response Payload
FAIL

Is there any way to get more detail on what causes the FAIL response?

Thanks in advance for any suggestions!

1 Like

@SuprMan You can make a small change to the phplist code so that it returns hopefully more information
In file lists/index.php change line 88 from

echo 'FAIL';

to

echo $result;

Thanks @duncanc, that helped me solve my dilemma!

It turned out that it was actually working correctly, but since I don’t have a mail server configured on my localhost, phplist was responding: Sorry, sending the message to request your confirmation failed, please click "Reload" to try again. If it still does not work, please contact the administrator.

When I uploaded the code to my server it worked as expected.

So, for anyone else having trouble with this:
You need a working mail server in your development environment to test this.
(Which I should have remembered from reading the phplist development guide…)

2 Likes

I have a new problem. I’m now trying to pass additional parameters by they aren’t being recorded.
I enabled the parameter in the subscribirían page in config and its being passed back in the url (I see it in console log)

Any idea why this could be happening?

If a wrong subscription page id is given, phpList will use the default subscription page, which probably is not good for Ajax.
This my change in index.php around line 120:
// make sure the subscribe page still exists
$id_save = $id;
$req = Sql_fetch_row_query(sprintf(‘select id from %s where id = %d’, $tables[‘subscribepage’], $id));
$id = $req[0];
if ($id == null) {
echo sprintf($GLOBALS[‘strSubscriPageMissing’],$id_save);
return;
}

english.inc:
$strSubscriPageMissing = ‘Subscription page (%s) missing’;

1 Like

Limit this to asubscribe only:

if ($id == null && (isset($_GET[‘p’]) && $_GET[‘p’] == ‘asubscribe’)) {

1 Like

Belatedly many thanks for this @veltsu. Now a Mantis issue here: https://mantis.phplist.org/view.php?id=19525

Hi,

I know this is an old post - but I’m having fun trying to get the form to sub via an ajax request. The normal signup form is:

https://test.com/newsletter/lists/?p=subscribe&id=1

The list ID is 2 ( https://test.com/newsletter/lists/admin/?page=editlist&id=2 for the admin area)

my Javascript looks like:

window.reqwest({
        url: '/newsletter/lists/',
        type: 'json',
        method: 'post',
        data: { 'id': 1, 'subscribe': 'subscribe', 'p': 'asubscribe', 'email': 'andy.newby@gmail.com', 'list': {'2': 'signup'} },
        error: function (err) {
            alert("There was an error: " + err)
        },
        success: function (data) {


        }
    });

When I run it, I get the following request:

id 1
subscribe subscribe
p asubscribe
email andy.newby@gmail.com
list[2] signup

This returns a page like:

What am I doing wrong?

Thanks

Andy

Revisiting this old thread. Just wanted to say thanks for posting your solution, and also to link to our solution. Here’s the html and js (jquery) that we use for users subscribing via an ajax form on one of our domains (in wordpress, mediawiki, etc) without having to visit our actual phplist site:

1 Like

I got this working, however am disappointed to discover that it does not work on Safari mobile devices…for example in iPhones running Safari. Does anyone have a solution for this?

Correction, I had a small bug in my CSS that was breaking it in Safari, I got it working.

Sorry about all of the posts. Actually there does seem to be an issue with Safari on iPhones when using this form. I can get this working on the desktop with no issues, but have only got it to work sporadically on Safari. Any ideas?

1 Like