WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to change URL after submitting the form successfully? (10 posts)

  1. Rafael_Magnvs
    Member
    Posted 1 year ago #

    I have a form on my website that I want to set up as a goal on my Google Analytics account. In order to do so, I need to specify the URL the user gets after submitting the form successfully..

    Well, it so happens that the form on my website does not change URL after submitting the form, the user gets the success message under the same URL.

    What could I do to get a different URL after user submitted the form?

    Find the code below:

    <?php
    /*
    Template Name: Survey
    */
    ?>
    
    <?php
    if(isset($_POST['submitted'])) {
    	if(trim($_POST['contactName']) === '') {
    		$nameError = 'Please enter your name.';
    		$hasError = true;
    	} else {
    		$name = trim($_POST['contactName']);
    	}
    
    	if(trim($_POST['email']) === '')  {
    		$emailError = 'Please enter your email address.';
    		$hasError = true;
    	} else if (!preg_match("/^:alnum:[a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
    		$emailError = 'You entered an invalid email address.';
    		$hasError = true;
    	} else {
    		$email = trim($_POST['email']);
    	}
    
    	if(trim($_POST['endereco']) === '') {
    		$enderecoError = 'Please enter a message.';
    		$hasError = true;
    	} else {
    		if(function_exists('stripslashes')) {
    			$endereco = stripslashes(trim($_POST['endereco']));
    		} else {
    			$endereco = trim($_POST['endereco']);
    		}
    	}
    
    	if(trim($_POST['servicos']) === '') {
    		$servicosError = 'Please enter a message.';
    		$hasError = true;
    	} else {
    		if(function_exists('stripslashes')) {
    			$servicos = stripslashes(trim($_POST['servicos']));
    		} else {
    			$servicos = trim($_POST['servicos']);
    		}
    	}
    
    	if(trim($_POST['comments']) === '') {
    		$commentError = 'Please enter a message.';
    		$hasError = true;
    	} else {
    		if(function_exists('stripslashes')) {
    			$comments = stripslashes(trim($_POST['comments']));
    		} else {
    			$comments = trim($_POST['comments']);
    		}
    	}
    
    	if(!isset($hasError)) {
    		$emailTo = get_option('tz_email');
    		if (!isset($emailTo) || ($emailTo == '') ){
    			$emailTo = get_option('admin_email');
    		}
    		$subject = '[PESQUISA SERVICEIRO] DE '.$name;
    		$body = "Name: $name \n\nEmail: $email \n\nEndereco: $endereco \n\nServicos: $servicos \n\nComments: $comments";
    		$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
    
    		wp_mail($emailTo, $subject, $body, $headers);
    		$emailSent = true;
    	}
    
    }
    ?>
    
    <?php get_header(); ?>
    		<div id="wrapper">
    			<div id="main">
    			<div id="content_page">
    
    			<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    			<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    				<h2><?php the_title(); ?></h2>
    					<div class="entry">
    						<?php if(isset($emailSent) && $emailSent == true) {?>
    							<div class="successmsg">
    								<p>Obrigado, sua solicita&ccedil;&atilde;o foi enviada com sucesso!</p>
    
    						<?php } else { ?>
    							<?php the_content(); ?>
    							<?php if(isset($hasError) || isset($captchaError)) { ?>
    								<p class="error">Ooops!, aconteceu algo de errado...<p>
    							<?php } ?>
    
    						<form action="<?php the_permalink(); ?>" id="contact" method="post">
    							<ul class="contact">
    							<li>
    								<label for="contactName"><strong>Seu Nome:</strong></label>
    								<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
    								<?php if($nameError != '') { ?>
    									<span class="error">Por favor coloque seu nome</span>
    								<?php } ?>
    							</li>
    
    							<li>
    								<label for="email"><strong>Seu Email:</strong></label>
    								<input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required requiredField email" />
    								<?php if($emailError != '') { ?>
    									<span class="error">Por favor coloque seu email</span>
    								<?php } ?>
    							</li>
    
    							<li>
    								<label for="commentsText"><strong>Seu Endere&ccedil;o ou local onde procura o servi&ccedil;o:</strong></label>
    								<textarea name="endereco" id="enderecoText" rows="10" cols="30" class="required requiredField"><?php if(isset($_POST['endereco'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['endereco']); } else { echo $_POST['endereco']; } } ?></textarea>
    								<?php if($enderecoError != '') { ?>
    									<br/><span class="error">Por favor coloque especifique alguma regi&atilde;o para localizarmos as melhores op&ccedil;&otilde;es</span><br/>
    								<?php } ?>
    							</li>
    
    							<li>
    								<label for="commentsText"><strong>Liste os servi&ccedil;os que est&aacute; procurando:</strong></label>
    								<textarea name="servicos" id="servicosText" rows="10" cols="30" class="required requiredField"><?php if(isset($_POST['servicos'])) { if(function_exists('servicos')) { echo stripslashes($_POST['servicos']); } else { echo $_POST['servicos']; } } ?></textarea>
    								<?php if($servicosError != '') { ?>
    									<br/><span class="error">Por favor indique quais servi&ccedil;os voc&ecirc; est&aacute; interessado</span><br/>
    								<?php } ?>
    							</li>
    
    							<li>
    								<label for="commentsText"><strong>Caso tenha prefer&ecirc;ncias, liste alguns estabelecimentos:</strong></label>
    								<textarea name="comments" id="commentsText" rows="10" cols="30" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
    								<?php if($commentError != '') { ?>
    									<br/><span class="error">Caso n&atilde;o tenha prefer&ecirc;ncias, determine a dist&acirc;ncia que est&aacute; disposto a aceitar uma cota&ccedil;&atilde;o. Ex. 20km</span><br/>
    								<?php } ?>
    							</li>
    
    							<li>
    								<input type="submit"></input>
    							</li>
    						</ul>
    						<input type="hidden" name="submitted" id="submitted" value="true" />
    					</form>
    				<?php } ?>
    				</div><!-- .entry-content -->
    			</div><!-- .post -->
    
    				<?php endwhile; endif; ?>
    		</div><!-- #content -->
    	</div><!-- #container -->
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Thanks in advance for any insights

  2. mattyrob
    Member
    Posted 1 year ago #

    Can'y you just change the form action parameter from <?php the_permalink(); ?> to the URL location you really want?

  3. Rafael_Magnvs
    Member
    Posted 1 year ago #

    Hey Mattyrob,

    I have tried what you said, but if I do that I stop receiving the email messages (Which are just as important, if not more..)

    If I change the URL, do I have to make any other changes to make sure the email is delivered?

  4. bcworkz
    Member
    Posted 1 year ago #

    Whatever URL your action sends the form to needs to have the 1st php block of code. Even so, the error checking will not work very well. I think the action should remain pointing to the same page for that reason.

    Perhaps try a wp_redirect() after the $emailSent = true; line to the URL that you register as a goal? It should work as long as it appears before the get_header() line. Additionally, only error free submissions will be logged by Analytics this way.

  5. aammir
    Member
    Posted 1 year ago #

    can anyone tell me about that???

  6. esmi
    Forum Moderator
    Posted 1 year ago #

  7. Rafael_Magnvs
    Member
    Posted 1 year ago #

    Hello Bc Workz,

    Your solution was as simple as it was on the money.. I did just what you said and my code ended up like this:

    wp_mail($emailTo, $subject, $body, $headers);
    		$emailSent = true;
                    wp_redirect( 'my-target-page-here', 301 );
    		exit;

    Thanks you for your valuable insight.

    All the best mate!

  8. Rafael_Magnvs
    Member
    Posted 1 year ago #

    This is solved!

  9. Pankaj_VNT
    Member
    Posted 1 year ago #

    change URL after form Submission code, i tried many different code but it's not working...Pls help me, sorry to post full code.

    [ code removed ]

  10. cubecolour
    ɹoʇɐɹǝpoɯ
    Posted 1 year ago #

    pankaj, this is a resolved topic. Please start your own topic and use a pastebin if you want to include a large block of code http://codex.wordpress.org/Forum_Welcome#Posting_Code

Topic Closed

This topic has been closed to new replies.

About this Topic