[SOLVED] Ajax subscribe api

I want to add email to subscribe list using ajax.

mywebsite?p=asubscribe should do the task but i cannot find any documentation on how to use.

@trucmuche2005 has already done what im looking for.

i have tried a lot by coding but the response is always “FAILS”.

type: POST
url: /my-website/lists/?p=asubscribe
data: {'email': 'test@mail.com', 'list': {'3': 'signup'}}

(i have seen the phplist-plugin-restapi, it is not what i want and also it do not support operation without login)

1 Like

reference code: https://github.com/phpList/phplist3/blob/master/public_html/lists/index.php#L257

Here is an example of subscribing using ajax. The subscriber needs to enter a name as well as the email address.

subscription_form_example.html.zip (1.1 KB)

4 Likes

Thanks you for sharing @duncanc
I am still getting the problem.
for 3.1.2,
localhost: always return "FAIL"
demo.phplist.org: always return nothing (empty response) [for crossOrigin true and false]
i changed the list[11] to list[x] where x is a valid list-id. (3 for localhost and 5 for demo.phplist.org)
(also, i tried query argument id=x , x as above)

p=asubscribe&id=3

The value for id is the subscribe page id, not the list id.

list[11]

this is the list id.

phplist on the server to which you are subscribing will need to have the ACCESS_CONTROL_ALLOW_ORIGIN config file entry set to the server that is hosting the subscribe form.

2 Likes

@duncanc thanks for clearifing the query argument “id” usage.

also, im using a local server for development. (and the same server also serve the ajax enable form you provided.)
so, i guess ACCESS_CONTROL_ALLOW_ORIGIN is out of the question (though i even tried define("ACCESS_CONTROL_ALLOW_ORIGIN", "*"); in config for testing)

still the localhost server return “FAIL” output.
p=asubscribe&id=1 and request email=aaa%40aaa.com&attribute1=aaa&htmlemail=1&list%5B3%5D=signup&subscribe=subscribe&VerificationCodeX=
where my subscribe page id (default page) is 1
and list-id is 3

For the request to work the subscribe page has to be defined to default to HTML format emails, not display email confirmation, and offer only one list.

3 Likes

@duncanc THANK YOU VERY MUCH, finally the item appeared in the list.
you took the effort to clearify stuffs.

So, here is the deal for future readers.
Get @duncanc above script.
modify the <input> list[MY_LIST_ID] = "signup"
in the url query argument id change id=SUBSCRIBE_PAGE_ID where SUBSCRIBE_PAGE_ID is a newly created list page that offer single list with the above option (as @duncanc describe in the previous post)

and you should be running (or atleast near your goal that need more debugging :wink: ).

I don’t know what i’m doing wrong.

I did everything according to the instructions.

But i have still the error report “Sorry, we were unable to process your subscription.”.

So what i’m doing wrong? It’s because running phplist in a subdomain with SSL?

Maybe you can help. Thanks a lot!

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;