Jump to content

PHP Newbie – Code to process form and redirect


hobbscreative

Recommended Posts

Hi there,

 

I am a complete newbie to php, in fact it is the first day I have even touched the stuff. I am a print designer that is working on website in way over my head. Hopefully someone can point me in the direction of a tutorial for help or just plain help me out as I have been searching all day for an answer and pulling out my hair.

 

Basically I have a form on this site that is being processed through a php page (process.php) I have got this sorted however, I have two issues.

 

1) Once the form is processed it pops up with a thank-you message. I am trying to get the code to redirect straight to an html thank-you (or error) page where I can control the style etc. I have tried some code I have found off the net but nothing seems to be working.

 

2) I am receiving the email with all of the data except for the checkboxes. Do I have to include some specific code for this in my process.php?

 

I have posted my code below:

 

HTML Form:

 

        <form action="process.php" method="post">            
		  	<p><span id="spry_title"><label for="title">Title <strong>*</strong></label><input type="text" name="title" id="label" size="3"/><span class="textfieldRequiredMsg">This field is required.</span></span></p>
   			  	<p><span id="spry_first_name"><label for="first_name">First Name <strong>*</strong></label><input type="text" name="first_name" id="label" size="25"/><span class="textfieldRequiredMsg">This field is required.</span></span></p>
                <p><span id="spry_surname"><label for="surname">Surname <strong>*</strong></label><input type="text" name="surname" id="label" size="25"/><span class="textfieldRequiredMsg">This field is required.</span></span></p>
			<p><label for="address">Address</label> <input type="text" id="address" size="25" /></p>
			<p><label for="city">City</label> <input type="text" id="city" size="25" /></p>
                <p><span id="spry_phone"><label for="phone">Daytime Phone <strong>*</strong></label><input type="text" name="phone" id="label" size="15"/><span class="textfieldRequiredMsg">This field is required.</span></span></p>
                <p><span id="spry_email"><label for="email">Email <strong>*</strong></label><input type="text" name="email" id="label" size="15"/><span class="textfieldRequiredMsg">This field is required.</span></span></p>
                <p>I am interested in: <strong>*</strong>
               	<p class="checkbox_list">
                <label for="golf_membership" class="checkbox_list"><input type="checkbox" id="golf_membership" value="golf_membership" class="checkbox"/>Golf Membership</label>
                <label for="conferences" class="checkbox_list"><input type="checkbox" id="conferences" value="conferences" class="checkbox"/>Conferences</label>
                <label for="functions" class="checkbox_list"><input type="checkbox" id="functions" value="functions" class="checkbox"/>Functions</label><br />
                <label for="database" class="checkbox_list"><input type="checkbox" id="database" value="database" class="checkbox"/>Joining the Database</label>
                <label for="other" class="checkbox_list"><input type="checkbox" id="other" value="other" class="checkbox"/>Other</label></p><br />
		  	<p class="submit_btn"><input type="submit" value="Submit" /><input type="reset" value="Reset" class="reset_btn" /></p>
       </form>
        </div>
        <script type="text/javascript">
<!--
var spry_title = new Spry.Widget.ValidationTextField("spry_title");
var spry_first_name = new Spry.Widget.ValidationTextField("spry_first_name");
var spry_surname = new Spry.Widget.ValidationTextField("spry_surname");
var spry_phone = new Spry.Widget.ValidationTextField("spry_phone");
var spry_email = new Spry.Widget.ValidationTextField("spry_email");
//-->
        </script>

 

process.php:

 

<?php
   if ($_SERVER['REQUEST_METHOD']=="POST"){
      // In testing, if you get an Bad referer error
      // comment out or remove the next three lines
      if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
         !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
         die("Bad referer");
      $msg="Values submitted by the user:\n";
      foreach($_POST as $key => $val){
         if (is_array($val)){
            $msg.="Item: $key\n";
            foreach($val as $v){
               $v = stripslashes($v);
               $msg.="   $v\n";
            }
         } else {
            $val = stripslashes($val);
            $msg.="$key: $val\n";
         }
      }
      $recipient="[email protected]";
      $subject="Form submission";
      error_reporting(0);
      if (mail($recipient, $subject, $msg)){
         echo "<h1>Thank you</h1><p>Message successfully sent:</p>\n";
         echo nl2br($input);
		echo header('Location: http://www.example.com/');
      } else
         echo "An error occurred and the message could not be sent.";
   } else
      echo "Bad request method";
?>

 

 

I apologize for the apparent stupidity of my questions. However this sort of stuff is not my forte and I have nowhere else to turn.

 

Thanks in advance,

 

James

 

<input type="checkbox" name="checkbox[]" value="Your_value">

 

It will return an array

 

$_POST['checkbox']

 

with numeric keys, and Your_value as its content

 

You can turn the array into a string by using this code

 

$newStr = implode(", ", $_POST['checkbox']);

 

it will return whatever Your_value was only from the checked boxes,

 

similar to:

 

Your_value, Your_value, Your_value, Your_value,

 

Sorry DarkerAngel, as I said this is my first day working with php. I may have placed it in the wrong place because it doesn't seem to be working?

 

Can you tell me if I have made an error please?

 

<?php
   if ($_SERVER['REQUEST_METHOD']=="POST"){
      // In testing, if you get an Bad referer error
      // comment out or remove the next three lines
      if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
         !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
         die("Bad referer");
      $msg="Values submitted by the user:\n";
      foreach($_POST as $key => $val){
         if (is_array($val)){
            $msg.="Item: $key\n";
            foreach($val as $v){
               $v = stripslashes($v);
               $msg.="   $v\n";
            }
         } else {
            $val = stripslashes($val);
            $msg.="$key: $val\n";
         }
      }
      $recipient="[email protected]";
      $subject="Form submission";
      error_reporting(0);
      if (mail($recipient, $subject, $msg)){
    header("Location: http://www.ferrymeadgolf.co.nz/thankyou.html")
         echo "<h1>Thank you</h1><p>Message successfully sent:</p>\n";
         echo nl2br($input);
      } else
         echo "An error occurred and the message could not be sent.";
   } else
      echo "Bad request method";
?>

Try:

 

<?php
   if ($_SERVER['REQUEST_METHOD']=="POST"){
      // In testing, if you get an Bad referer error
      // comment out or remove the next three lines
      if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
         !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
         die("Bad referer");
      $msg="Values submitted by the user:\n";
      foreach($_POST as $key => $val){
         if (is_array($val)){
            $msg.="Item: $key\n";
            foreach($val as $v){
               $v = stripslashes($v);
               $msg.="   $v\n";
            }
         } else {
            $val = stripslashes($val);
            $msg.="$key: $val\n";
         }
      }
      $recipient="[email protected]";
      $subject="Form submission";
      error_reporting(0);
      if (mail($recipient, $subject, $msg)){
    header("Location: http://www.ferrymeadgolf.co.nz/thankyou.html");
//you were missing your semi colon
      } else
         echo "An error occurred and the message could not be sent.";
   } else
      echo "Bad request method";
?>

 

Just fyi, the http_referer isn't the most reliable thing on the planet. It can easily be faked; but more moreover, there are certain browsers and firewalls which do not send it. You could therefore be blocking legitimate users.

<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
// In testing, if you get an Bad referer error
// comment out or remove the next three lines
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 || !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])) { die("Bad referer"); }
$msg="Values submitted by the user:\n";
foreach($_POST as $key => $val){
	if (is_array($val)){
		$msg.="Item: $key\n";
		foreach($val as $v){
			$v = stripslashes($v);
			$msg.="   $v\n";
		}
	} else {
		$val = stripslashes($val);
		$msg.="$key: $val\n";
	}
}
$recipient="[email protected]";
$subject="Form submission";
error_reporting(0);
if (mail($recipient, $subject, $msg)){
	header( "Location: http://www.ferrymeadgolf.co.nz/thankyou.html");
} else {
	echo "An error occurred and the message could not be sent.";
} else {
echo "Bad request method";
}
?>

Thanks to you both DarkerAngel and GingerRobot, I appreciate you help.

 

GingerRobot, you mentioned that "http_referer" is not that reliable. The tutorial I am using http://apptools.com/phptools/forms/forms7.php mentions the use of "sessions". Would this be a good way to combat this problem?

Maybe generate a cypher key on page load, and save it in a file or DB (i'd suggest a DB if available) and make a hidden input element with a file name(without extention the element would be more secure it can be added back later) or DB Table index then verify the file or table content on the post script

 

it would still be easy with the input element to make a script, maybe store the file name or Table index in a session variable. But I'm not totally talented in Sessions

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.