Hi All,
I’m looking for some help with the form API that i haven’t been able to find through Drupal forum, the form API drupal-support IRC, and googling. I’m still not getting any response and this is my first time with the FAPI so i’m doing a lot of guessing.
What i’m trying to do is:
I have a form that needs to send it’s results to a third party email collection agency, but I have to redirect the form on successful submission of the form to a thank you page/message that I am hosting on my drupal site.
right now I can either get my form to submit to the third party (and a page which only shows the success code and nothing else), or I can redirect the form to my thank you page (but the form into is not sent to my third party).
I’m using drupal 6, and have read the form API and I’ve seen the flowchart of how the drupal Api is supposed to complete the process, but that doesn’t mean I understand.
if there is an example or some documentation that I can look at I’d appreciate it. I’ve spend so long looking for something that will help me answer the question, I just can’t believe that this kind of function has never been done in drupal before - it’s got to be out there somewhere.
Comments
Hi there,I'm guessing you
Hi there,
I'm guessing you changed the "#action" property to submit to the 3rd party..? If so, that isn't going to work because the browser is taken to whatever page is in the action (ie the 3rd party) and will leave your site.
The only way to do this effectively is to issue a POST via drupal_http_request() during your form submission.
Please see this example I gave in another issue. Hope that helps.
Hi Roper, thank you yes that
Hi Roper,
thank you yes that is what I was doing, I'm sorry if i was vague for leaving it out - this is what i was using:
$form['#action'] = url('http://thirdpartysite.com', array('external' => true));
I've opened and scanned the pages you recommended and it looks a heck of a lot closer than what I've gotten on my own and will def spend time there. The one thing I don't see is the redirect back to a page of my own site, i'm sorry to ask for more but if you had a recommendation for that part too I'd really appreciate it.
Thanks a million for the help.
Steph
I love Drupal. Always.
Sure thing...
By default there is no redirect, so after form submission you will end up on the same page. To specify a redirect URL, you can do it 2 ways.
1) Set the #redirect property on the form itself:
$form['#redirect'] = 'path/to/redirect';2) In the form submit handler, modify the $form_state:
This method is useful if you want to change the redirect depending on any submitted values (or button click).
$form_state['redirect'] => 'path/to/redirect'; // Note there's no #-sign on "redirect" here.In both cases you can use an array instead of a string, as outlined in the docs for the #redirect attribute.
:)
Thanks again!
I'm working on my forms today with the info you've given, I'll let you know how it goes. Thanks again, I really appreciate the help :)
Steph
------------------
Update: Looks like it's exactly what I needed!
I was able to use the example you had suggested in your second link as a base and built form there, so far it seems to be working great. thank you very much for the help, hopefully i'll get my use case done and not find any more sticking points.
Thanks
Steph
I love Drupal. Always.
Happy to help. :)
Happy to help. :)
Hey Roper,I am sorry to do
Hey Roper,
I am sorry to do this to you, but there is a spot I'm stuck with this, if you don't mind another request for help.
I know that what I'm doing wrong is small (or impossible), but I'm not sure where I'm going wrong.
below is my code, and while I can get it to submit and go to my 3rd party site successfully, when I try to use the drupal_http_request it's not working. I know this because I'm supposed to get an email receipt.
I tried using reference vars to give value to my $pass_there vars, which didn't work.
and I've tried passing the values directly from the form, which did work on another form i did, which was much simpler. in the example below, I've got some conversions going on in other parts of my form.
any guidance would be much appreciate.
function formshare_nameform_submit($form_id, &$form_state) {
// Lookup share matrix retun value
$shareMatrixValue = setShareMatrixValue($form_state['values']['image_id']);
$langResultsCode = setLangForShare($langCode);
$gender = setGenderForShare($form_state['values']['gender']);
$first_name = $form_state['values']['first_name'];
$last_name = $form_state['values']['last_name'];
$email = $form_state['values']['email'];
$email_friend1 = $form_state['values']['email_friend1'];
$email_friend2 = $form_state['values']['email_friend2'];
$email_friend3 = $form_state['values']['email_friend3'];
$message = $form_state['values']['massage'];
$optin = $form_state['values']['optin'];
$category = $form_state['values']['category'];
$image_id = $shareMatrixValue;
$id_langue = $form_state['values']['id_langue'];
// Make an array of values to pass to 3rd party.
$pass_these = array();
$pass_these['shareMatrixValue'] = setShareMatrixValue($form_state['values']['image_id']);
$pass_these['langResultsCode'] = setLangForShare($langCode);
$pass_these['gender'] = setGenderForShare($form_state['values']['gender']);
$pass_these['first_name'] = $form_state['values']['first_name'];
$pass_these['last_name'] = $form_state['values']['last_name'];
$pass_these['email'] = $form_state['values']['email'];
$pass_these['email_friend1'] = $form_state['values']['email_friend1'];
$pass_these['email_friend2'] = $form_state['values']['email_friend2'];
$pass_these['email_friend3'] = $form_state['values']['email_friend3'];
$pass_these['message'] = $form_state['values']['massage'];
$pass_these['optin'] = $form_state['values']['optin'];
$pass_these['category'] = $form_state['values']['category'];
$pass_these['image_id'] = $shareMatrixValue;
$pass_these['id_langue'] = $form_state['values']['id_langue'];
// Post the data to 3rd party.
$url = 'http://myrecieving website.com';
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
$data = drupal_query_string_encode($pass_these);
drupal_http_request($url, $headers, 'POST', $data);
}
just as a side note - this is how i had it set up when I was trying to use reference vars:
// Lookup share matrix return value
$shareMatrixValue = setShareMatrixValue($form_state['values']['image_id']);
$langResultsCode = setLangForShare($langCode);
$gender = setGenderForShare($form_state['values']['gender']);
$first_name = $form_state['values']['first_name'];
$last_name = $form_state['values']['last_name'];
$email = $form_state['values']['email'];
$email_friend1 = $form_state['values']['email_friend1'];
$email_friend2 = $form_state['values']['email_friend2'];
$email_friend3 = $form_state['values']['email_friend3'];
$message = $form_state['values']['massage'];
$optin = $form_state['values']['optin'];
$category = $form_state['values']['category'];
$image_id = $shareMatrixValue;
$id_langue = $form_state['values']['id_langue'];
// Make an array of values to pass to 3rd party.
$pass_these = array();
$pass_these['shareMatrixValue'] = $shareMatrixValue;
$pass_these['langResultsCode'] = $langResultsCode;
$pass_these['gender'] = $gender;
$pass_these['first_name'] = $first_name;
$pass_these['last_name'] = $last_name;
$pass_these['email'] = $email;
$pass_these['email_friend1'] = $email_friend1;
$pass_these['email_friend2'] = $email_friend2;
$pass_these['email_friend3'] = $email_friend3;
$pass_these['message'] = $message;
$pass_these['optin'] = $optin;
$pass_these['category'] = $category;
$pass_these['image_id'] = $image_id;
$pass_these['id_langue'] = $id_langue;
I love Drupal. Always.
It looks a bit more
It looks a bit more extrapolated than in needs to be I think, and there's at least one typo (message => massage), but let's debug. Try this at the end where you're making the POST.
And post whatever you get here.
Hi Roper,believe it or not
Hi Roper,
Believe it or not that typo may have been my trouble, the tests i ran yesterday after fixing the test seemed to be working.
the only thing i'm curious about is that i never got a return from the script you gave me to test with. is that because of my #redirect?
thanks again for the help, i can't tell you how I appreciate it!
Steph
I love Drupal. Always.
Ah, yeah probably the
Ah, yeah probably the redirect causes you to miss it... You can try this instead, although if it's working it's probably not necessary.
That will add it to the log (/admin/reports/dblog) so you don't have to catch it real-time.