Custom contact form

Permalink
Hi All, I'm using Jordan Lev's Custom contact form package -
https://github.com/jordanlev/c5_custom_contact_form...

to make a form that when submitted sends the visitor to different pages depending on the summed value of the select fields on the form.

I pm'd Jordan and he quickly and helpfully responded with the following advice:

----

This is a fairly easy modification, and you are correct that it can be done in the block controller's successRedirect function. All you need to do is get the values you want to compare from the $_POST array, and set the redirect location based on that. For example:


private function successRedirect() {
    $question1 = $_POST['question_1']; //replace 'question_1' with the desired field name (the one that is in the "name" attribute of the form field)
    $question2 = $_POST['question_2']; //replace 'question_2' with the desired field name (the one that is in the "name" attribute of the form field)
    $result = intval($question1) + intval($question2); //be careful with user-supplied inputs, since they cannot be "trusted" -- you should ensure you are dealing with integers here (non-integers will be converted to 0). Use floatval() instead of intval() if the numbers will have decimals.
    if ($result < 2) {
      $redirect_to_url = BASE_URL . View::url('/path/to/page');
    } else {
      $redirect_to_url = BASE_URL . View::url('/path/to/another/page');
    }
    header('Location: ' . $redirect_to_url);
    exit;
  }


One thing to note is that if you redirect to another page, you will not get the benefit of a "thank you" message being displayed automatically, so you'll want to be sure that the page you're redirecting to has some kind of confirmation message on it (so users don't get confused). Another thing to note is that in my example code, I'm assuming you're redirecting to other pages in your Concrete5 site (hence the use of BASE_URL and View::url()), but if you're redirecting to another site, then the $redirect_to_url variable can just be the full url to the page (e.g. $redirect_to_url = 'http://example.com/page1.php';)

----

This all seems sensible to me. However I cannot get the succeessRedirect function to do anything. On submitting the form, the data is saved to the database and the email is sent, but there is no redirect, the form is hidden and the thanks message displayed.

Below is the code from the custom_contact_form/controller.php, I changed to redirect function to be simpler for testing purposes...

public function action_submit() {
      $this->processForm(false);
   }
   public function processForm($is_ajax = false) {
      $submission = new CustomContactFormSubmission($_POST);
      $error = $submission->validate();
      if ($error->has()) {
         if ($is_ajax) {
            return $error->getList();
         } else {
            $this->set('errors', $error->getList());
         }
      } else {
         $submission->save();
         $this->sendNotificationEmail($submission);

 
hutman replied on at Permalink Best Answer Reply
hutman
Does this work if you remove the is_ajax stuff?
richmason replied on at Permalink Reply
Got a response from Jordan Lev and he recommended pretty much the same thing: Deleting the javascript in the view.php fixed it.


--------
I think the easiest solution here will just be to disable the ajax altogether, which you can do by deleting the javascript code at the top of /packages/custom_contact_form/blocks/custom_contact_form/view.php.

If you get rid of that code (everything between the <script></script> tags) then the form will just submit normally, so your redirect logic will always be called.
--------

Thanks for the help.