Forums

custom code (api) on checkbox validation

Tiko 06 Feb, 2014
Hello,
I have an API that inscribes the email adress of the visitor on an external mailinglist, and I would like to include this in my form, but the adress should only be sended if the visitor checkes a certain checkbox. Can someone help me with this problem? I need to know where to include the API and how to make the checkbox work... Thank you in advance,
Tiko
Tiko 06 Feb, 2014
Ok, I really have to get myself some php knowledge... I won't get any further without... Aparently the things I want are to complicated to get easy answers🙂 . Thank you anyway.
GreyHead 06 Feb, 2014
Hi Tiko,

There is some PHP required but it's fairly simple. If you have a colleague or friend with some coding experience then they should be able to help you.

Unfortunately it's not possible to build-in every possible form application.

Bob
Tiko 06 Feb, 2014
yes, of course, I understand. What I understood is that I should include an event switcher, configure it for the checkbox, and grag a custom code with the api into one of the events of the switcher... Is this right?
Thank you for your patience.
tiko
GreyHead 07 Feb, 2014
Hi Tiko,

Yes, that's correct.

Or you could do it all with PHP. What is the API that you want to connect to? Is there some basic documentation that I could look at?

Bob
Tiko 07 Feb, 2014
ok, I'll send you the api in PM because of some personal info included in the api. Thank you for your interest.
GreyHead 08 Feb, 2014
Hi Tiko,

The Custom code version would look like this:
<?php
if ( !isset($form->data['check_box']) || $form->data['check_box'] != 'Yes' ) {
  return;
}
// Include Mailjet's API Class
include_once('JPATH_SITE.'/some_path/php-mailjet.class-mailjet-0.1.php');

// Create a new Object
$mj = new Mailjet();
$email = $form->data['email'];

// Parameters
$params = array(
    'method' => 'POST',
    'contact' => $email,
    'id' => '999999'
);
// Call
$response = $mj->listsAddContact($params);
// Result
$form->data['contact_id'] = $response->contact_id;
?>
You can then use {contact_id} in an email or Thank You page.
Bob
Tiko 08 Feb, 2014
Wow thank you for that! I tried to include it, but it just doesn't seem to work. What I did: I copied your PHP in a custom code action. I replaced the "id" with my id, I replaced "some_path/php-mailjet.class-mailjet-0.1.php" with the path to the file on my server, and I named the checkbox "check_box" and wrote "yes=" in the optionbox... So tell me, how many things do I do wrong?🙂 Everything???
GreyHead 09 Feb, 2014
Hi Tiko,

The obvious thing to debug to see if the Checkbox 'filter' is failing - 'yes' isn't the same as 'Yes' here so it may be. If you comment out the // return; line temporarily that will remove the filter and you can see if the rest work.

Bob
Tiko 09 Feb, 2014
ok, I tried without the filter and that doesn't work. The only thing I see that could be wrong is the path to the php-mailjet.class-mailjet-0.1.php file. How should the line look if it's placed here: http://reve2.com/MyJoomla/mailjet/php-mailjet.class-mailjet-0.1.php .
Thank you for your patience.
Tiko
GreyHead 10 Feb, 2014
Hi Tiko,

That's a URL, not a folder path, please try
// Include Mailjet's API Class
include_once('JPATH_SITE.'/mailjet/php-mailjet.class-mailjet-0.1.php');
or
// Include Mailjet's API Class
include_once('JPATH_SITE.'/MyJoomla/mailjet/php-mailjet.class-mailjet-0.1.php');

Bob
Tiko 12 Feb, 2014
Hello, problem still not solved.
I have been working on it with someone who knows PHP, and after unsuccesfully trying to use the given code he tried it this way:
<?php
if ( !isset($form->data['check_box'][0]) || $form->data['check_box'][0] != 'Yes' ) {
return;
} else {
$email = $form->data['email'];
$post = 'email='.$email;
$url ="http://reve2.com/MyJoomla/mailjet/api.php";

$ch  = curl_init();
curl_setopt($ch, CURLOPT_URL; $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Accept: application/json',
    'Content-Length: ' . strlen($post))
);
curl_exec($ch);

curl_close($ch);
}
?>


But that doesn't work neither. Do you have any idea what could be wrong? The api works, the form works (it sends a confirmation email) , but the code in the form doesn't work... HELP!!!
Thank you in advance,

Tiko
Tiko 17 Feb, 2014
Hello,
sorry for insisting, but does someone have any idea what could be wrong with what I'm trying?
Thank you in advance,
Tiko
Tiko 04 Apr, 2014
I still don't have a solution for this problem. Any help is welcome.
This topic is locked and no more replies can be posted.