[SOLVED] Ajax subscribe api

@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

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

Hi

I apologize yf I add a question to a solved post. Shoud I post a new one instead?

I am new here. I read this topic and got the duncanc file and suggestion regarding the FAIL message. Actually Y changed for echo ‘FAIL: |’ . $result . ‘|’;

I went by the ACCESS_CONTROL_ALLOW_ORIGINS and now I don’t have any complain in the browser

The thing using the duncanc sample is that I get always

FAIL: | |

(with a space in between the |)

Reviwing the parameters passed to the index, they are: p=asubscribe&id=3 (the List name in the admin page is “insta” and the List ID is 3=

The inputs in the form are:

 <input type="hidden" name="list[3]" value="signup"  />
 <input type="hidden" name="subscribe" value="asubscribe"/>

I changed list[3] to insta and the result is the same. I am not sure which is the correct value. BTW is the third list in the admin panel.
I changed asubscribe to subscribe as well and same result

Checking the index page I noticed calls

and the parameters are (Firefox inspect)
{
“email”: “notuo@XXX.com”,
“attribute1”: “notuo”,
“htmlemail”: “1”,
“list[3]”: “signup”,
“subscribe”: “subscribe”,
“VerificationCodeX”: “”
}

Any idea? I am totally lost
Do you need more info?
Thanks in advance

@notuo Please read the earlier posts [SOLVED] Ajax subscribe api - #5 by duncanc

Hi @duncanc

I did. Do you mean this part?:

p=asubscribe&id=3
The value for id is the subscribe page id, not the list id.
list[11]
This is the list id.

I am sorry but this list[11] is not that clear for me. Do you mean list[11] should be always this name in the input form?

I just did and same result FAIL

p=asubscribe&id=3 is sent to index.php (as per your form) As I mentioned, mine is the third list defined, so I guess this is OK.

This image is the result of the form. I don’t get any info to debug what is happening.

Thank you for your time.

The 3 refers to the subscribe page id, replace that with the id of the subscribe page that you have created

list[11]

the 11 refers to the list id. Replace that with the id of the list that you included in the subscribe page.

You need to read the section of the manual Creating a subscribe page | phpList manual “Add an Ajax subscribe box”