Zen Cart Logo
If this is your first visit, check our documentation. To post, register or login.
Forums / Customizing the Look of Your Shop / Templates, Stylesheets, Page Layout / Problem redirecting upon form submission

Problem redirecting upon form submission

Locked

Started by dgsignal on 25 Jun 2008, 6:54 PM · Views: 2,632

Results 1 to 16 of 16
This thread is locked. New replies are disabled.
25 Jun 2008, 6:54 PM
#1
dgsignal avatar

dgsignal

New Zenner

Join Date:
May 2008
Posts:
31
Plugin Contributions:
0

Post #1 by dgsignal

I'm having trouble redirecting after having a user fill out a form. In order to test functionality of my redirect, I've created a simple form where a user enters an email address and a name. The form calls itself to do a simplified validation, and then gets redirected to another page. Problem is...the page will not redirect. As you can see from the enclosed code, I've tried both methods of redirection using header(location: URL) and zen_redirect(URL). Right now, the redirection does not occur and results in my form main_page being blank.

My form resides in my template in define_my_test_page.php.

<?
$message = "";
$emailclass = "basictext";
$username = "";

if ($_POST['process'] == 1) {

$pattern = '/.*@.*\..*/';
$email = $_POST['email'];
$urlname = urlencode($$_POST['username']);

if (preg_match($pattern, $_POST['email']) > 0) {
// Here's where you would store
// the data in a database...
header("location: index.php?main_page=my_product_home");
//zen_redirect("index.php?main_page=my_product_home");

}
$message = "Please enter a valid email address.";
$username = $_POST['name'];
$emailclass = "errortext";
}
?>

<html>
<style>
.basictext {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px; color:#000066;
}
.errortext {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px; color:#C00000; font-weight: bold;
}
</style>
<body>
<form action="index.php?main_page=my_test_page" method="post">
<? if ($message != "") {
print '<span class="errortext">'.
$message."<span><br>\n";
}
?>
<span class="<?print $emailclass; ?>">
Email address:</span>
<input name="email" type="text"
class="<? print $emailclass; ?>"><br>

<span class="basictext">Your name:</span>
<input name="name" type="text" class="basictext"
value="<?print $username; ?>"><br>
<input type="hidden" name="process" value="1">
<input type="submit" name="Button1" value="Sign up!">
</form>
</body></html>

Thanks for any help,

Dustin

25 Jun 2008, 10:36 PM
#2
dgsignal avatar

dgsignal

New Zenner

Join Date:
May 2008
Posts:
31
Plugin Contributions:
0

Post #2 by dgsignal

Clarification: Even when I reduce the code in my define_my_test_page.php to the following, the redirect does not work. When I go to index.php?main_page=my_test_page, the main page is blank except for the header name. Obviously, redirects in this manner do not work.

Any suggestions for how to call a redirect from within define pages? I'm guessing I'm missing something simple.

<?php
zen_redirect("index.php?main_page=my_product_home");
?>
25 Jun 2008, 10:42 PM
#3
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Post #3 by kuroi

Have you tried ...?> ?php

zen_redirect("my_product_home");
?>

Re: your first post, short tags are really not recommended.

Kuroi Web Design and Development | Twitter

(Questions answered in the forum only - so that any forum member can benefit - not by personal message)

25 Jun 2008, 10:57 PM
#4
dgsignal avatar

dgsignal

New Zenner

Join Date:
May 2008
Posts:
31
Plugin Contributions:
0

Post #4 by dgsignal

Thanks for your reply.

Unfortunately, no luck with the change you recommended. Per your second recommendation, once I can get the redirect functionality working, I will make the change to get rid of my short tags.

I have attached a small snapshot of what my screen looks like when I load index.php?main_page=my_test_page. As you can see, the header title "my_test_page" shows up but is blank underneath.

26 Jun 2008, 12:13 AM
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Post #5 by drbyte

FAQ on troubleshooting blank pages (or blank "parts of pages"): https://www.zen-cart.com/tutorials/index.php?article=82

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

26 Jun 2008, 1:19 AM
#6
dgsignal avatar

dgsignal

New Zenner

Join Date:
May 2008
Posts:
31
Plugin Contributions:
0

Post #6 by dgsignal

Thanks DrByte. I'll download the error debugging utility and give it a try. In the meantime, could you (or anybody else) give me the proper way to redirect from within a define page?

Right now, all I have on my define redirect page is:

<?php zen_redirect("index.php?main_page=my_product_home"); ?>

I've eliminated anything extraneous to get the redirect functionality working.

Other than the usage of short tags, I am pretty sure this function call is written correctly. When I place the same function call in /modules/pages/my_test_page/header_php.php the redirect works fine. It seems to me that I simply don't know the correct way to redirect from within the define page, rather than the header. I've successfully written php scripts and redirected on other projects using header(location: $url) but something is different in this case.

Thanks again for all your help.

Dustin

26 Jun 2008, 5:03 AM
#7
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Post #7 by drbyte

I doubt you'll have any success doing a header-redirect from inside the define_xxxxxx.php file of a define page ... because the HTML headers have already been sent to the page. Thus, PHP will not let you send redirect or any other header commands, since they must happen before any HTML content is sent to the browser.

If you must do a redirect from that position, you'll probably have to do it with javascript.
Or better yet, do it from within header_php.php, since that's the correct place to do form processing and redirects ... certainly much more appropriate there than in a template-output file as you're doing now.

The real question is ... "why" are you trying to do a redirect from within a define page? What you've described seems like you're going about it the wrong way.

If it's email processing of collected information, the following is a packaged approach: http://www.zen-cart.com/forum/showthread.php?p=386482#post386482

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

26 Jun 2008, 1:52 PM
#8
dgsignal avatar

dgsignal

New Zenner

Join Date:
May 2008
Posts:
31
Plugin Contributions:
0

Post #8 by dgsignal

DrByte, thanks for the information. It's been obvious from the get go that I must be taking the wrong approach for my forms. What I'm hoping now is that you'll be able to give me some insight as to where and how my form integration should take place.

My goal is to have a couple of forms that take in some information from the user, validate, then store it in a table in the database. Apparently, I have incorrectly assumed that I could put the html form tags and validation code into the define pages. Instead, should I place this code into the header_php.php file?

Thanks again for your help. Your last email definitely got me headed in the right direction.

Dustin

26 Jun 2008, 2:21 PM
#9
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Post #9 by kuroi

It's not really that what you're doing is wrong. More that you're trying to do it from the wrong place.

Each define page has a includes/modules/pages/page_name/header_php.php file and sometimes a main_vars.php file too. These normally process the data that will be used on the page and if a reason for a re-direct emerges, will execute it from there. This is probably where you should be executing yours.

Kuroi Web Design and Development | Twitter

(Questions answered in the forum only - so that any forum member can benefit - not by personal message)

26 Jun 2008, 2:47 PM
#10
dgsignal avatar

dgsignal

New Zenner

Join Date:
May 2008
Posts:
31
Plugin Contributions:
0

Post #10 by dgsignal

Thanks Kuroi.

Okay, I'm getting closer.

So if I'm trying to create a form with action to itself to validate, then redirect to another page upon success...

Given your last post, would a better approach be to put the form tags into the tpl_mypage_default.php, and validation code with redirect-to-success-page in the header_php.php file? Or should the form tags go in the define page?

Will the $_POST variables from the form be accessible from both pages so that the header_php.php file can validate the input from the form page? I hope this makes sense, I'm still learning...

Thanks,

Dustin

26 Jun 2008, 2:59 PM
#11
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Post #11 by kuroi

I'd recommend putting them in the template (tpl) file and having the form callback to the same page. If form validation is successful, redirect to the next page.

Equally valid is to have the form go to the next page, and have that page's header_php.php validate the form and if it fails redirect back.

Kuroi Web Design and Development | Twitter

(Questions answered in the forum only - so that any forum member can benefit - not by personal message)

26 Jun 2008, 3:59 PM
#12
dgsignal avatar

dgsignal

New Zenner

Join Date:
May 2008
Posts:
31
Plugin Contributions:
0

Post #12 by dgsignal

Allright! Thanks for the reply. First, I'm going to try a form with a callback to itself, validate, then redirect.

To make sure I understand, when you say to put "them" in the tpl file, are you referring to putting both the form tags AND the validation code in the tpl file? I thought that DrBytes mentioned that any redirect has to occur from within the header_php file. If this is the case, maybe I need to try your second suggestion of form action to second page, validate, then redirect back if there are errors.

Thanks again for all your help!

Dustin

26 Jun 2008, 4:19 PM
#13
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Post #13 by drbyte

Following the example from the thread I posted a link to:

  • header_php.php does the validation and processing (ie: all "logic")
  • tpl file contains form tags and all visual/input interface components (ie: all "display" and info-collection)
  • language file (named similar to the page name) stores language-specific text that the template uses for output purposes (not editable from the admin)
  • define_xxxx.php file could optionally be used to display some informational content, but is not required at all (ie: display-only info that you want to be able to frequently alter from the admin). DefinePages are only intended as a convenience for frequent-edit needs.

The following might be overkill, but could be a handy reference: http://www.zen-cart.com/index.php?main_page=product_contrib_info&cPath=40_54&products_id=378

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

26 Jun 2008, 4:34 PM
#14
dgsignal avatar

dgsignal

New Zenner

Join Date:
May 2008
Posts:
31
Plugin Contributions:
0

Post #14 by dgsignal

Per DrBytes last post, I'll try dividing up my current non-functional form (which presently also contains the validation code) and will put the validation (w/redirect) in the header_php, and the form with callback in the tpl file.

Anything else I should take into consideration?

I'm excited to give this a try! Thanks!

Dustin

26 Jun 2008, 4:59 PM
#15
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Post #15 by drbyte

The band-signup attachment in my first link shows the file structure of a page intended to collect signup details, validate the data, and then send it via email to the store owner. Storing into database tables instead of emailing follows the same concept.
Nothing wrong with looking through it for examples and using it as a model.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

26 Jun 2008, 5:16 PM
#16
dgsignal avatar

dgsignal

New Zenner

Join Date:
May 2008
Posts:
31
Plugin Contributions:
0

Post #16 by dgsignal

Whew! I think I've got the functionality working now. I'm in the process of using DrBytes's band_signup attachment to use as a starting point to get things going.

Thanks so much to DrBytes and kuroi for for getting me headed in the right direction.

Regards,

Dustin