473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

After form is submitted - blank process.php page instead of redirect

I know I'm close to having this working. Once the user clicks SUBMIT they end up at a blank process.php page rather then the redirect url specified in FormTools.

Here is my process.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /**
  4.  * File: process.php
  5.  *
  6.  * This file processes any form submissions for forms already added and configured within Form Tools. To
  7.  * use it, just point your form to this file, like so:
  8.  *
  9.  *   <form method="post" action="/path/to/process.php">
  10.  *
  11.  * Once the form has been added through the Form Tools UI, this script parses the form contents
  12.  * and adds it to the database then redirects the user to whatever page is required. In addition,
  13.  * this script is used to initially set up the form within the database, to map input fields to
  14.  * database columns and types.
  15.  */
  16.  
  17.  
  18. // always include the core library functions
  19. $folder = dirname(__FILE__);
  20. require_once("$folder/global/library.php");
  21.  
  22. // if the API is supplied, include it as well
  23. $folder = dirname(__FILE__);
  24. @include_once("$folder/global/api/api.php");
  25.  
  26.  
  27. // check we're receiving something
  28. if (empty($_POST))
  29. {
  30.   $page_vars = array("message_type" => "error", "message" => $LANG["processing_no_post_vars"]);
  31.   ft_display_page("error.tpl", $page_vars);
  32.   exit;
  33. }
  34.  
  35. // check there's a form ID included
  36. else if (empty($_POST["form_tools_form_id"]))
  37. {
  38.   $page_vars = array("message_type" => "error", "message" => $LANG["processing_no_form_id"]);
  39.   ft_display_page("error.tpl", $page_vars);
  40.   exit;
  41. }
  42.  
  43. // is this an initialization submission?
  44. else if (isset($_POST["form_tools_initialize_form"]))
  45.   ft_initialize_form($_POST);
  46.  
  47. // otherwise, it's a regular form submission. Process it!
  48. else
  49.   ft_process_form($_POST);
  50.  
  51. // -------------------------------------------------------------------------------------------------
  52.  
  53. /**
  54.  * This function processes the form submissions, after the form has been set up in the database.
  55.  */
  56. function ft_process_form($form_data)
  57. {
  58.   global $g_table_prefix, $g_multi_val_delimiter, $g_query_str_multi_val_separator, $g_root_dir, $LANG,
  59.     $g_api_version, $g_api_recaptcha_private_key;
  60.  
  61.   // ensure the incoming values are escaped
  62.   $form_data = ft_sanitize($form_data);
  63.  
  64.   $form_id = $form_data["form_tools_form_id"];
  65.   $form_info = ft_get_form($form_id);
  66.  
  67.   // do we have a form for this id?
  68.   if (!ft_check_form_exists($form_id))
  69.   {
  70.     $page_vars = array("message_type" => "error", "message" => $LANG["processing_invalid_form_id"]);
  71.     ft_display_page("error.tpl", $page_vars);
  72.     exit;
  73.   }
  74.  
  75.   extract(ft_process_hook_calls("start", compact("form_info", "form_id", "form_data"), array("form_data")), EXTR_OVERWRITE);
  76.  
  77.   // check to see if this form has been completely set up
  78.   if ($form_info["is_complete"] == "no")
  79.   {
  80.     $page_vars = array("message_type" => "error", "message" => $LANG["processing_form_incomplete"]);
  81.     ft_display_page("error.tpl", $page_vars);
  82.     exit;
  83.   }
  84.  
  85.   // check to see if this form has been disabled
  86.   if ($form_info["is_active"] == "no")
  87.   {
  88.     if (isset($form_data["form_tools_inactive_form_redirect_url"]))
  89.     {
  90.       header("location: {$form_data["form_tools_inactive_form_redirect_url"]}");
  91.       exit;
  92.     }
  93.  
  94.     $page_vars = array("message_type" => "error", "message" => $LANG["processing_form_disabled"]);
  95.     ft_display_page("error.tpl", $page_vars);
  96.     exit;
  97.   }
  98.  
  99.   // do we have a form for this id?
  100.   if (!ft_check_form_exists($form_id))
  101.   {
  102.     $page_vars = array("message_type" => "error", "message" => $LANG["processing_invalid_form_id"]);
  103.     ft_display_page("error.tpl", $page_vars);
  104.     exit;
  105.   }
  106.  
  107.  
  108.   // was there a reCAPTCHA response? If so, a recaptcha was just submitted. This generally implies the
  109.   // form page included the API, so check it was entered correctly. If not, return the user to the webpage
  110.   if (isset($g_api_version) && isset($form_data["recaptcha_response_field"]))
  111.   {
  112.     $passes_captcha = false;
  113.     $recaptcha_challenge_field = $form_data["recaptcha_challenge_field"];
  114.     $recaptcha_response_field  = $form_data["recaptcha_response_field"];
  115.  
  116.     $folder = dirname(__FILE__);
  117.     require_once("$folder/global/api/recaptchalib.php");
  118.  
  119.     $resp = recaptcha_check_answer($g_api_recaptcha_private_key, $_SERVER["REMOTE_ADDR"], $recaptcha_challenge_field, $recaptcha_response_field);
  120.  
  121.     if ($resp->is_valid)
  122.       $passes_captcha = true;
  123.     else
  124.     {
  125.       // since we need to pass all the info back to the form page we do it by storing the data in sessions. Enable 'em.
  126.       @ft_api_start_sessions();
  127.       $_SESSION["form_tools_form_data"] = $form_data;
  128.       $_SESSION["form_tools_form_data"]["api_recaptcha_error"] = $resp->error;
  129.  
  130.       // if there's a form_tools_form_url specified, redirect to that
  131.       if (isset($form_data["form_tools_form_url"]))
  132.       {
  133.         header("location: {$form_data["form_tools_form_url"]}");
  134.         exit;
  135.       }
  136.       // if not, see if the server has the redirect URL specified
  137.       else if (isset($_SERVER["HTTP_REFERER"]))
  138.       {
  139.         header("location: {$_SERVER["HTTP_REFERER"]}");
  140.         exit;
  141.       }
  142.       // no luck! Throw an error
  143.       else
  144.       {
  145.         $page_vars = array("message_type" => "error", "message" => $LANG["processing_no_form_url_for_recaptcha"]);
  146.         ft_display_page("error.tpl", $page_vars);
  147.         exit;
  148.       }
  149.     }
  150.   }
  151.  
  152.  
  153.   // get a list of the custom form fields (i.e. non-system) for this form
  154.   $form_fields = ft_get_form_fields($form_id, array("include_field_type_info" => true));
  155.  
  156.   $custom_form_fields = array();
  157.   $file_fields = array();
  158.   foreach ($form_fields as $field_info)
  159.   {
  160.     $field_id        = $field_info["field_id"];
  161.     $is_system_field = $field_info["is_system_field"];
  162.     $field_name      = $field_info["field_name"];
  163.  
  164.     // ignore system fields
  165.     if ($is_system_field == "yes")
  166.       continue;
  167.  
  168.     if ($field_info["is_file_field"] == "no")
  169.     {
  170.       $custom_form_fields[$field_name] = array(
  171.         "field_id"    => $field_id,
  172.         "col_name"    => $field_info["col_name"],
  173.         "field_title" => $field_info["field_title"],
  174.         "include_on_redirect" => $field_info["include_on_redirect"],
  175.         "field_type_id" => $field_info["field_type_id"],
  176.         "is_date_field" => $field_info["is_date_field"]
  177.       );
  178.     }
  179.     else
  180.     {
  181.       $file_fields[] = array(
  182.         "field_id"   => $field_id,
  183.         "field_info" => $field_info
  184.       );
  185.     }
  186.   }
  187.  
  188.   // now examine the contents of the POST/GET submission and get a list of those fields
  189.   // which we're going to update
  190.   $valid_form_fields = array();
  191.   while (list($form_field, $value) = each($form_data))
  192.   {
  193.     // if this field is included, store the value for adding to DB
  194.     if (array_key_exists($form_field, $custom_form_fields))
  195.     {
  196.       $curr_form_field = $custom_form_fields[$form_field];
  197.  
  198.       $cleaned_value = $value;
  199.       if (is_array($value))
  200.       {
  201.         if ($form_info["submission_strip_tags"] == "yes")
  202.         {
  203.           for ($i=0; $i<count($value); $i++)
  204.             $value[$i] = strip_tags($value[$i]);
  205.         }
  206.  
  207.         $cleaned_value = implode("$g_multi_val_delimiter", $value);
  208.       }
  209.       else
  210.       {
  211.         if ($form_info["submission_strip_tags"] == "yes")
  212.           $cleaned_value = strip_tags($value);
  213.       }
  214.  
  215.       $valid_form_fields[$curr_form_field["col_name"]] = "'$cleaned_value'";
  216.     }
  217.   }
  218.  
  219.   $now = ft_get_current_datetime();
  220.   $ip_address = $_SERVER["REMOTE_ADDR"];
  221.  
  222.   $col_names = array_keys($valid_form_fields);
  223.   $col_names_str = join(", ", $col_names);
  224.   if (!empty($col_names_str))
  225.     $col_names_str .= ", ";
  226.  
  227.   $col_values = array_values($valid_form_fields);
  228.   $col_values_str = join(", ", $col_values);
  229.   if (!empty($col_values_str))
  230.     $col_values_str .= ", ";
  231.  
  232.   // build our query
  233.   $query = "
  234.     INSERT INTO {$g_table_prefix}form_$form_id ($col_names_str submission_date, last_modified_date, ip_address, is_finalized)
  235.     VALUES ($col_values_str '$now', '$now', '$ip_address', 'yes')
  236.            ";
  237.  
  238.   // add the submission to the database (if form_tools_ignore_submission key isn't set by either the form or a module)
  239.   $submission_id = "";
  240.   if (!isset($form_data["form_tools_ignore_submission"]))
  241.   {
  242.     $result = mysql_query($query);
  243.  
  244.     if (!$result)
  245.     {
  246.       $page_vars = array("message_type" => "error", "error_code" => 304, "error_type" => "system",
  247.         "debugging"=> "Failed query in <b>" . __FUNCTION__ . ", " . __FILE__ . "</b>, line " . __LINE__ .
  248.             ": <i>" . nl2br($query) . "</i>", mysql_error());
  249.       ft_display_page("error.tpl", $page_vars);
  250.       exit;
  251.     }
  252.  
  253.     $submission_id = mysql_insert_id();
  254.     extract(ft_process_hook_calls("end", compact("form_id", "submission_id"), array()), EXTR_OVERWRITE);
  255.   }
  256.  
  257.  
  258.   $redirect_query_params = array();
  259.  
  260.   // build the redirect query parameter array
  261.   foreach ($form_fields as $field_info)
  262.   {
  263.     if ($field_info["include_on_redirect"] == "no" || $field_info["is_file_field"] == "yes")
  264.       continue;
  265.  
  266.     switch ($field_info["col_name"])
  267.     {
  268.       case "submission_id":
  269.         $redirect_query_params[] = "submission_id=$submission_id";
  270.         break;
  271.       case "submission_date":
  272.         $settings = ft_get_settings();
  273.         $submission_date_formatted = ft_get_date($settings["default_timezone_offset"], $now, $settings["default_date_format"]);
  274.         $redirect_query_params[] = "submission_date=" . rawurlencode($submission_date_formatted);
  275.         break;
  276.       case "last_modified_date":
  277.         $settings = ft_get_settings();
  278.         $submission_date_formatted = ft_get_date($settings["default_timezone_offset"], $now, $settings["default_date_format"]);
  279.         $redirect_query_params[] = "last_modified_date=" . rawurlencode($submission_date_formatted);
  280.         break;
  281.       case "ip_address":
  282.         $redirect_query_params[] = "ip_address=$ip_address";
  283.         break;
  284.  
  285.       default:
  286.         $field_name = $field_info["field_name"];
  287.  
  288.         // if $value is an array, convert it to a string, separated by $g_query_str_multi_val_separator
  289.         if (isset($form_data[$field_name]))
  290.         {
  291.           if (is_array($form_data[$field_name]))
  292.           {
  293.             $value_str = join($g_query_str_multi_val_separator, $form_data[$field_name]);
  294.             $redirect_query_params[] = "$field_name=" . rawurlencode($value_str);
  295.           }
  296.           else
  297.             $redirect_query_params[] = "$field_name=" . rawurlencode($form_data[$field_name]);
  298.         }
  299.         break;
  300.     }
  301.   }
  302.  
  303.   // only upload files & send emails if we're not ignoring the submission
  304.   if (!isset($form_data["form_tools_ignore_submission"]))
  305.   {
  306.     // now process any file fields. This is placed after the redirect query param code block above to allow whatever file upload
  307.     // module to append the filename to the query string, if needed
  308.     extract(ft_process_hook_calls("manage_files", compact("form_id", "submission_id", "file_fields", "redirect_query_params"), array("success", "message", "redirect_query_params")), EXTR_OVERWRITE);
  309.  
  310.     // send any emails
  311.     ft_send_emails("on_submission", $form_id, $submission_id);
  312.   }
  313.  
  314.   // if the redirect URL has been specified either in the database or as part of the form
  315.   // submission, redirect the user [form submission form_tools_redirect_url value overrides
  316.   // database value]
  317.   if (!empty($form_info["redirect_url"]) || !empty($form_data["form_tools_redirect_url"]))
  318.   {
  319.     // build redirect query string
  320.     $redirect_url = (isset($form_data["form_tools_redirect_url"]) && !empty($form_data["form_tools_redirect_url"]))
  321.       ? $form_data["form_tools_redirect_url"] : $form_info["redirect_url"];
  322.  
  323.     $query_str = "";
  324.     if (!empty($redirect_query_params))
  325.       $query_str = join("&", $redirect_query_params);
  326.  
  327.     if (!empty($query_str))
  328.     {
  329.       // only include the ? if it's not already there
  330.       if (strpos($redirect_url, "?"))
  331.         $redirect_url .= "&" . $query_str;
  332.       else
  333.         $redirect_url .= "?" . $query_str;
  334.     }
  335.  
  336.     header("Location: " . $redirect_url);
  337.     exit;
  338.   }
  339.  
  340.   // the user should never get here! This means that the no redirect URL has been specified
  341.   $page_vars = array("message_type" => "error", "message" => $LANG["processing_no_redirect_url"]);
  342.   ft_display_page("error.tpl", $page_vars);
  343.   exit;
  344. }
  345.  
And here is some of the HTML:

Expand|Select|Wrap|Line Numbers
  1. <form action="http://californiaschooloflaw.com/forms/form_tools_2/process.php" method="post" enctype="multipart/form-data">
  2. <input type="hidden" name="form_tools_form_id" value="9" />
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <meta content="en-us" http-equiv="Content-Language" />
  7. <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  8. <title>CALIFORNIA SCHOOL OF LAW APPLICATION</title>
Feb 6 '14 #1
0 1365

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Johann Blake | last post by:
The following is a bug I have discovered using tab pages and threads and I am looking for a workaround. Create a new Windows Forms application and add a tab control with two tab pages. Add a...
4
by: Raul M. Colon | last post by:
I have a web application and need to redirect to a another page. Is there any way to see the redirected page in a new page? (that's keeping the sending page visible) Thanks!!! Raul
5
by: Eric Shin | last post by:
Hi all. I'm like really on the beginning stage for ASP.NET just got a few questions to ask... Please help me. I've created a form page called "join.aspx" and it has lots of codes but the...
3
by: Marc Castrechini | last post by:
Forgive me on the lack of specifics but I am not a "server" guy. We just rolled our code out to a 2003 Server from Win2k Server and for some reason our pages are painfully slow. It appears the...
1
by: student | last post by:
Hello all, could someone explain why I'm getting a blank web page when I run start my code in a web browser? Here is the code: Imports System.Data.SqlClient Imports System.Data Public Class...
4
by: Andrea De Santi | last post by:
How can I redirect to another page with form data? In asp Classic I write: <form ... action="filename">...</form> and in then target page I write <%=request.form("fieldname")%> ..... but in...
5
by: Homa | last post by:
Hi all, Can anyone give me some links about how to do an async web service call from aspx and display a temperary page before the web service returns? Thanks, Homa Wong
0
by: netlady | last post by:
Hi, can someone help me or share his or her idea why I am always getting a blank web page whenever I click the export button or the navigational next toolbar of the crystalreportviewer in .net ? I...
4
by: jobs | last post by:
Hello. If my users are logged in, and try to access restricted pages I want to direct them to a custom 403 page. If they are not logged in, I would like to continue to direct them to the login...
2
by: Reza Ruslan | last post by:
I'm trying to create a simple login page with php using session. when i want to access the index.php, the page successfully redirect to login page. then I put username and password based on the table...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.