[SOLVED] Ajax subscribe api

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…)

1 Like

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

Tried everything in this thread but signup doesn’t sign up. The only differences I can spot is the PHPlist install dir:

url: ‘https://responder.lytica.com/index.php?p=asubscribe&id=3’,

and:
define(‘ACCESS_CONTROL_ALLOW_ORIGINS’, [‘https://responder.lytica.dk’]);
define(‘PUBLIC_PROTOCOL’,‘https’);

Checked the list and subscribe page number:

< input type="hidden" name="list[1]" value="signup" />

<input type=text name="attribute3" id="attribute3" class="attributeinput" size="40" value="">

and confirmation mails are not sent even then phpmail is working on sending domain.

I’m out of ideas…

Your .com domain doesn’t exist… where is hosted your ajax form ?

Really @RigPa are you using attribute? Not seen… on your phpList subscribe page
https://responder.lytica.dk/index.php?p=subscribe&id=3