Redirection after form submit

Discussion in 'PHP' started by Andres_BBB, Aug 30, 2012.

  1. #1
    Hi.

    I have installed a contact form module in my website and would like to implement a redirection to a certain page after submit.

    I have fond diverse possible solutions, of no avail so far.

    To be exact:

    print('<META HTTP-EQUIV="Refresh" CONTENT=10; URL=redirection.php >');

    and

    header("location:redirection.php");

    My knolwedge of PHP is basic and I cannot make it work.

    I would appreciate some help or advise.

    MODULE CODE:

    
    <?php
    // no direct access
    defined( '_JEXEC' ) or die( 'Restricted access' );
    $document =& JFactory::getDocument();
    $document->addStyleSheet(JURI::root() . 'modules/contactform/css/style.css');
     
    //Email Parameters
    $recipient = $params->get('email_recipient', '');
    $fromName = @$params->get('from_name');
    $fromEmail = @$params->get('from_email', 'info@my-email.com');
     
    // Text Parameters
    $myNameLabel = $params->get('name_label', 'Name:');
    $myEmailLabel = $params->get('email_label', 'Email:');
    $myTelephoneLabel = $params->get('telephone_label', 'Telephone:');
    $mySubjectLabel = $params->get('subject_label', 'Subject:');
    $myMessageLabel = $params->get('message_label', 'Message:');
    $buttonText = $params->get('button_text', 'Send Message');
    $pageText = $params->get('page_text', 'Thank you for your contact.');
    $errorText = $params->get('error_text', 'Your message could not be sent. Please try again.');
    $noEmail = $params->get('no_email', 'Please write your email');
    $invalidEmail = $params->get('invalid_email', 'Please write a valid email');
    $pre_text = $params->get('pre_text', '');
     
    // Size and Color Parameters
    $label_pos = $params->get('label_pos', '0');
     
    // URL Parameters
    $exact_url = $params->get('exact_url', true);
    $disable_https = $params->get('disable_https', true);
    $fixed_url = $params->get('fixed_url', true);
    $myFixedURL = $params->get('fixed_url_address', '');
     
    // Module Class Suffix Parameter
    $mod_class_suffix = $params->get('moduleclass_sfx', '');
     
     
    if ($fixed_url) {
      $url = $myFixedURL;
    }
    else {
      if (!$exact_url) {
        $url = JURI::current();
      }
      else {
        if (!$disable_https) {
          $url = (!empty($_SERVER['HTTPS'])) ?  "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] :  "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        }
        else {
          $url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        }
      }
    }
     
    $url = htmlentities($url, ENT_COMPAT, "UTF-8");
     
    $myError = '';
    $CORRECT_NAME = '';
    $CORRECT_EMAIL = '';
    $CORRECT_SUBJECT = '';
    $CORRECT_MESSAGE = '';
     
    if (isset($_POST["rp_email"])) {
      $CORRECT_SUBJECT = htmlentities($_POST["rp_subject"], ENT_COMPAT, "UTF-8");
      $CORRECT_MESSAGE = htmlentities($_POST["rp_message"], ENT_COMPAT, "UTF-8");
      $CORRECT_NAME = htmlentities($_POST["rp_name"], ENT_COMPAT, "UTF-8");
     
      // check email
      if ($_POST["rp_email"] === "") {
        $myError = '<div class="pop-up">' . '<p>' . $noEmail . '</p>' . '</div>';
      }
      if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", strtolower($_POST["rp_email"]))) {
        $myError = '<div class="pop-up">' . '<p>' . $invalidEmail . '</p>' . '</div>';
      }
      else {
        $CORRECT_EMAIL = htmlentities($_POST["rp_email"], ENT_COMPAT, "UTF-8");
      }
     
      if ($myError == '') {
        $mySubject = $_POST["rp_subject"];
        $myMessage = 'Remitente: '. $_POST["rp_name"] ."\n\n" . 'E-Mail: '.  $_POST["rp_email"] ."\n\n". 'Telf.: '. $_POST["rp_telephone"] ."\n\n".  $_POST["rp_message"];
     
        $mailSender = &JFactory::getMailer();
        $mailSender->addRecipient($recipient);
     
        $mailSender->setSender(array($fromEmail,$fromName));
        $mailSender->addReplyTo(array( $_POST["rp_email"], '' ));
     
        $mailSender->setSubject($mySubject);
        $mailSender->setBody($myMessage);
     
     
        if ($mailSender->Send() !== true) {
          $myReplacement = '<div class="pop-up">' . '<p>' . $errorText . '</p>' . '</div>';
          print $myReplacement;
          return true;
        }
        else {
          $myReplacement = '<div class="pop-up">' . '<p>' . $pageText . '</p>' . '</div>';
          print $myReplacement;
              }
     
      }
    } // end if posted
     
    // check recipient
    if ($recipient === "") {
      $myReplacement = '<div class="pop-up">' . '<p>Recipient not described</p>' . '</div>';
      print $myReplacement;
      return true;
    }
     
    print '<style type="text/css"><!--' . $addcss . '--></style>';
    print '<div class="rapid_contact ' . $mod_class_suffix . '"><form action="' . $url . '" method="post">' . "\n" .
          '<div class="rapid_contact intro_text ' . $mod_class_suffix . '">'.$pre_text.'</div>' . "\n";
     
    if ($myError != '') {
      print $myError;
    }
     
    print '<div id="forminfe">';
     
    // print name input
    print '<div class="forminfe">' . '<label for="name">' .  $myNameLabel . '</label>' . '<input class="rapid_contact  inputbox ' . $mod_class_suffix . '" type="text" name="rp_name" size="' .  '" value="'.$CORRECT_NAME.'"/></div>' . "\n";
    // print email input
    print '<div class="forminfe">' . '<label for="email">' .  $myEmailLabel . '</label>' . '<input class="rapid_contact  inputbox ' . $mod_class_suffix . '" type="text" name="rp_email"  value="'.$CORRECT_EMAIL.'"/></div>' . "\n";
    // print name input
    print '<div class="forminfe">' . '<label for="telephone">' .  $myTelephoneLabel . '</label>' . '<input class="rapid_contact  inputbox ' . $mod_class_suffix . '" type="text" name="rp_telephone"' .  '"/></div>' . "\n";
    // print subject input
    print '<div class="forminfe">' . '<label for="name">' .  $mySubjectLabel  . '</label>' . '<input class="rapid_contact  inputbox ' . $mod_class_suffix . '" type="text" name="rp_subject"' . '"  value="'.$CORRECT_SUBJECT.'"/></div>' . "\n";
    // print message input
    print '<div class="forminfe">' . '<label for="mensaje">' .  $myMessageLabel  . '</label>' .  '<textarea  class="rapid_contact textarea ' . $mod_class_suffix . '"  name="rp_message"' . '"  rows="4">'.$CORRECT_MESSAGE.'</textarea></div>' . "\n";
     
    // print button
    print '<div class="forminfe"><input class="rapid_contact button  ' . $mod_class_suffix . '" type="submit" value="' . $buttonText .  '"/></div></div></form></div>' . "\n";
    return true;
    
    Code (markup):

    Many thanks for you comments.
     
    Andres_BBB, Aug 30, 2012 IP
  2. phpking

    phpking Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What error u r getitng?
     
    phpking, Aug 30, 2012 IP
  3. fastestsms

    fastestsms Greenhorn

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #3
    You should try

    and let us know what error if it doesn't work for you. :)
     
    fastestsms, Aug 30, 2012 IP
  4. Andres_BBB

    Andres_BBB Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Sorry about my ignorance, where should I put that piece of code?

    ????


    if (
    $mailSender->Send() !== true) {
    $myReplacement = '<div class="pop-up">' . '<p>' . $errorText . '</p>' . '</div>';
    print $myReplacement; return true;
    }
    else {
    $myReplacement = '<div class="pop-up">' . '<p>' . $pageText . '</p>' . '</div>'; print $myReplacement;
    }
    }
     
    Andres_BBB, Aug 30, 2012 IP
  5. Andres_BBB

    Andres_BBB Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    No; it didn't work.

    Anyway, being my PHP so basic, perhaps some error in the code:

    ???????????????????

    if ($mailSender->Send() !== true) {
    $myReplacement = '<div class="pop-up">' . '<p>' . $errorText . '</p>' . '</div>';
    print $myReplacement;
    header('Location: http://www.example.com/');
    }
    else {
    $myReplacement = '<div class="pop-up">' . '<p>' . $pageText . '</p>' . '</div>';
    print $myReplacement;
    }

    Please some help with this.
     
    Andres_BBB, Sep 1, 2012 IP
  6. fastestsms

    fastestsms Greenhorn

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #6
    What function Send() return? I think if it's a class function, you should use try exception.
     
    fastestsms, Sep 2, 2012 IP
  7. Andres_BBB

    Andres_BBB Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    Hi.

    Two problems.

    I modified the code like this:

    if ($mailSender->Send() !== true) {
    $myReplacement = '<div class="pop-up">' . '<p>' . $errorText . '</p>' . '</div>';
    print $myReplacement;
    $url = '#anchor';
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    }
    else {
    $myReplacement = '<div class="pop-up">' . '<p>' . $pageText . '</p>' . '</div>';
    print $myReplacement;
    $url = '#anchor';
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    }


    }
    }

    Well, the idea is to redirect to an anchor in the form.

    RESULT:

    1. When the form is properly filled and with no errors, the redirection works and the page is http://www.thepage.com/index.php#anchor

    Or any other in the website (customers.php#anchor, etc.)

    2. When the form is not properly filled, the redirection does not work.

    3. When you try it in Chrome, something really weird: one second after the redirection, the redirection is unmade and changing http://www.thepage.com/index.php#anchor for http://www.thepage.com/index.php

    Some clue please?
     
    Andres_BBB, Sep 3, 2012 IP
  8. Gemba

    Gemba Member

    Messages:
    36
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    25
    #8
    Can't you just set the action parameter of the form to #ancor?
    Something like this <form action="#anchor">?
    Otherwise, I'm assuming the redirection doesn't work when it's properly filled before
     
    Gemba, Sep 3, 2012 IP
    Andres_BBB likes this.
  9. Andres_BBB

    Andres_BBB Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #9
    Hi Gemba.

    Solved with your final comment.

    Thanks very much.
     
    Andres_BBB, Sep 3, 2012 IP