Jump to content
Larry Ullman's Book Forums

Passing Field Data To Success Form


Recommended Posts

I have 2 pages "Contact.php" and "Success.php"

 

The contact form has the fields validated in javascript i.e

<script type="text/javascript" src="./jquery-1.7.2.min.js"></script>
<script type="text/javascript">
<!--
function Validategeneral_contact_form(theForm)
{
var regexp;
if (theForm.Editbox1.value == "")
{
alert("You Forgot To Enter Your First Name!");
theForm.Editbox1.focus();
return false;
}
</script>

 

 

When the user presses the submit, if all is ok the user is re-directed to the success page that just says your details have been sent. What I would like to know how can I get the success page to display "Thank you Billy, your details have been sent.

 

I have tried a few senarios but none seem to work.

Link to comment
Share on other sites

I'm tying myself up in knots with this. I've tried it all ways but can't get it to work. Could I be cheeky and ask just how to code it.

 

the form field is

 

<input type="text" id="Editbox1" style="position:absolute;left:135px;top:45px;width:198px;height:23px;line-height:23px;z-index:3;" name="first_name" value="" title="First Name:" placeholder="First Name:">

 

This is how I did it

 

<script type="text/javascript">
<!--header('Location: success.php?name=' . $Editbox1);
//-->
</script>

 

And this was what I put on the success page

Thank you $first_name Your email was sent to us.

Link to comment
Share on other sites

As you're using a mix of javascript and php its difficult to help without knowing how your logic is structured. What's the action attribute on your form set to? success.php? If so in success.php, you could do something like

 

$fn=$_POST['first_name];
echo '<p>Thank you ' . $fn. '. Your email was sent to us.</p>';

 

this is the bare bones of what you need to do and is not secure. I imagine many programmers will rail on this code for how insecure it is. Show how your logic is structured and we'll be able to tighten up the code.

 

Really you will want to do some server side validation as well.

 

And a side note... placeholder is an HTML5 attribute and will not be recognised in older browsers. Not a huge issue as long as you've provided a label for the fields so visitors using older browsers are catered for.

Link to comment
Share on other sites

You've used the PHP solution which I provided inside of javascript. I assumed wrongly you were redirecting after server-side processing from the contact.php script.

 

Build the functionality in PHP first, then build javascript on top of that.

 

<form action="contact.php" method="post">
<input type="text" name="first_name">

$first_name = $_POST['first_name'];

header('Location: success.php?first_name=' . $first_name);

 

$first_name = $_GET['first_name'];

echo 'Thank you ' . htmlentities( $first_name ) . ', your email was sent to us.';

 

 

Also, as marguax has pointed out, you've provided few details of the whole process which has made it difficult to help.

Link to comment
Share on other sites

Rob

 

You were right when the user fills in the form and presses submit it then re-directs them to a success page telling the user that the message has been sent.

 

Would it help if I add the link so you can see the source code: http://www.trsb.co.uk/contact_folder/contact.php

 

Do you think it would it be better to remove the javascript validation and replace it in php? It's just that the program I am using does all the validation by just ticking a few boxes.

Link to comment
Share on other sites

This is what's on the form page

 

<script type="text/javascript" src="./jquery-1.7.2.min.js"></script>
<script type="text/javascript">
<!--
function Validategeneral_contact_form(theForm)
{
  var regexp;
  if (theForm.Editbox1.value == "")
  {
  alert("You Forgot To Enter Your First Name!");
  theForm.Editbox1.focus();
  return false;
  }
 //-->
</script>
<form name="general_contact_form" method="post" action="<?success.php; ?>" enctype="multipart/form-data" id="Form1" onsubmit="return Validategeneral_contact_form(this)">
<select name="title" size="1" id="Combobox1 title="Title:">
<option selected>Please Select</option>
<option value="Mr.">Mr.</option>
<option value="Mrs.">Mrs.</option>
<option value="Miss.">Miss.</option>
</select>
<input type="text" id="Editbox1" name="first_name" value="" title="First Name:" placeholder="First Name:">
body>
</html>

 

and this is whats on the success page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>You have successfully sent mail to TRSB and Tower Employment Services</title>
<meta name="keywords" content="recruitment ageny, recruitment agencies in lancashire, recruitment agencies,recruitment agency,  jobs in preston, jobs in blackpool, hgv / lgv driving jobs, waste and recycling jobs, commercial recruitment, commercial employment agencies in blackpool, nationwide recruitment, jobs, carears, student jobs, part time jobs, full time jobs, employment in lancashire, Jobs in lancashire, tower employment services, stubbs management services, west coast recruitment, chef jobs.">
<meta name="author" content="Paul Stubbs 12/09/2012">
<meta name="generator" content="Tower Employment Services - http://www.trsb.co.uk">
</head>
<body>
<strong>Your email was successfully sent</strong>
Your email was sent to us. You should expect to hear from us within<strong><em> two business days
</body>
</html>

Link to comment
Share on other sites

These are the code files and you're not just copying and pasting source code from the pages viewed via the browser, right?

 

There's nothing in the code you've provided that shows any of the basic php that you would need to process form data.

 

Paul have you read Larry's book? Do you know how to write a form, send data to a php script, how to access and process that data via php?

 

If you do, please provide this code.

Link to comment
Share on other sites

Sorry Rob

 

This is the PHP thats on the page. I thought it was just something to do with sending the email:

 

<?php
  function ValidateEmail($email)
  {
  $pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
  return preg_match($pattern, $email);
  }
  if($_SERVER['REQUEST_METHOD'] == 'POST')
  {
  $mailto = 'paul@wcrltd.co.uk';
  $mailfrom = isset($_POST['email']) ? $_POST['email'] : $mailto;
  $subject = 'Contact Form';
  $message = 'Values submitted from Contact Form';
  $success_url = './../mail_folder/success.php';
  $error_url = './../mail_folder/fail.php';
  $error = '';
  $eol = "\n";
  $max_filesize = isset($_POST['filesize']) ? $_POST['filesize'] * 1024 : 1024000;
  $boundary = md5(uniqid(time()));
  $header  = 'From: '.$mailfrom.$eol;
  $header .= 'Reply-To: '.$mailfrom.$eol;
  $header .= 'MIME-Version: 1.0'.$eol;
  $header .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'.$eol;
  $header .= 'X-Mailer: PHP v'.phpversion().$eol;
  if (!ValidateEmail($mailfrom))
  {
	 $error .= "The specified email address is invalid!\n<br>";
  }
  if (!empty($error))
  {
	 $errorcode = file_get_contents($error_url);
	 $replace = "##error##";
	 $errorcode = str_replace($replace, $error, $errorcode);
	 echo $errorcode;
	 exit;
  }
  $internalfields = array ("submit", "reset", "send", "captcha_code");
  $message .= $eol;
  $message .= "IP Address : ";
  $message .= $_SERVER['REMOTE_ADDR'];
  $message .= $eol;
  foreach ($_POST as $key => $value)
  {
	 if (!in_array(strtolower($key), $internalfields))
	 {
	    if (!is_array($value))
	    {
		   $message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol;
	    }
	    else
	    {
		   $message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol;
	    }
	 }
  }
  $body  = 'This is a multi-part message in MIME format.'.$eol.$eol;
  $body .= '--'.$boundary.$eol;
  $body .= 'Content-Type: text/plain; charset=ISO-8859-1'.$eol;
  $body .= 'Content-Transfer-Encoding: 8bit'.$eol;
  $body .= $eol.stripslashes($message).$eol;
  if (!empty($_FILES))
  {
	  foreach ($_FILES as $key => $value)
	  {
		 if ($_FILES[$key]['error'] == 0 && $_FILES[$key]['size'] <= $max_filesize)
		 {
		    $body .= '--'.$boundary.$eol;
		    $body .= 'Content-Type: '.$_FILES[$key]['type'].'; name='.$_FILES[$key]['name'].$eol;
		    $body .= 'Content-Transfer-Encoding: base64'.$eol;
		    $body .= 'Content-Disposition: attachment; filename='.$_FILES[$key]['name'].$eol;
		    $body .= $eol.chunk_split(base64_encode(file_get_contents($_FILES[$key]['tmp_name']))).$eol;
		 }
	 }
  }
  $body .= '--'.$boundary.'--'.$eol;
  mail($mailto, $subject, $body, $header);
  header('Location: '.$success_url);
  exit;
  }
?>

Link to comment
Share on other sites

In your form tag change the action to the name of the file it's posting to.

 

Near the bottom of your php script is a header call, change this to reflect what I previously suggested to pass a $_GET variable via a redirect.

 

On the success page retrieve the $_GET variable and make sure it's sanitised before sending it to the browser.

Link to comment
Share on other sites

  • 7 months later...

Hi Guys

 

Im new to the forum and came across this post which has helped me get over a problem I was having.

 

I used the code that rob suggested which was great, as follows:

$name = 'Billy';
header('Location: success.php?name=' . $name);

 

However, I require to pass multiple fields to the success page, any help on this would be great.

 

Just a breif history of what Im trying to do is as follows:

I have a php booking form which needs to be emailed, written to text file as a backup if emails are lost and then pass several fields to the success page which in this instance is a romancart shopping cart.

 

I have been trying the following to no avail:

 

header('Location: http://www.romancart.com/cart.asp?storeid='. $storeid, 'price='. $price,'tax='. $tax);
 

It throws up an error Warning: header() expects parameter 3 to be long, string given in /home/productf/public_html/mailtest.php on line 44

 

It is also worth noting that I am a novice in this sort of code.

Link to comment
Share on other sites

Welcome to the forums, silent-gwebchap.

I hope you find the help you're looking for.

 

Your code is very close to correct, but if there's one thing I've learned about programming, close doesn't count.

Anyway, if you simply modify your parameter list by adding apostrophes (&) between your parameters and then replace the commas with periods to join the strings together, you should be fine.

 

For example:

 

header('Location: http://www.romancart...rt.asp?storeid=' . $storeid . '&price=' . $price . '&tax=' . $tax);
  • Upvote 1
Link to comment
Share on other sites

  • 8 months later...

Hi all,

 

I came across this post and have a question about using header('Location:).

 

One of my forms posts data to another page. After I have validated the inputs, I then include the header function which has a query string appended to the URL. Is this an acceptable means of sending form values to another page? I think I read somewhere that some servers have a problem with this - or maybe not, I cannot remember.

 

 

Is there another way to send the form values to a different page?

 

 

Thank you for your time!

Link to comment
Share on other sites

Instead of saying it's okay vs. not okay, I'd be inclined to ask why you want to do that.

Why post to one script, and then send those values onto another script? Why not just post to the final script in the first place?

 

Also, sending parameters via the Location header means that you have to use the GET method, which could expose sensitive data you posted from the form.

 

Are you sure attempting to do what you want to do is the right thing?

Link to comment
Share on other sites

Hi HartleySan,

 

thanks for responding.

 

On one of my pages I'm using a datepicker and the selected dates need to sent to another page. And on that other page I have another form whose selected values, together with the datepicker values, need to be sent to another page. It's a type of checkout setup.

 

The only data I'm sending along are dates and quantities, so no sensitive information really

Link to comment
Share on other sites

If the page the form is being posted to has a form of its own, then you should post all the values from the first form and the second form together to the third page.

 

How you post the values that were in the first form from the second form is up to you, but you can either store them as hidden input values in the second form (so that the user cannot (easily) edit them, but they're still sent to the third form), or you can redisplay those values in the first form in the second form, which would allow the users to then edit the values in the first form from the second form, if they so desired.

 

Of course, that begs the question: Why not just use one form to begin with?

 

Just my two cents.

Link to comment
Share on other sites

 

How you post the values that were in the first form from the second form is up to you, but you can either store them as hidden input values in the second form (so that the user cannot (easily) edit them, but they're still sent to the third form), or you can redisplay those values in the first form in the second form, which would allow the users to then edit the values in the first form from the second form, if they so desired.

 

 

Thanks, some things to consider. I'll probably go with the hidden input values for the second form.

 

 

It's strange that I can post the form values to another page if I don't use validation, but with validation I'm forced to use the GET method by appending the values to the location URL. Or am I missing something?

Link to comment
Share on other sites

You're missing something.

 

 

 

Perhaps I haven't explained myself properly.

 

I know that I can validate data sent via POST.  When I first created my form, the only inputs were for the datepicker. If someone selected a date he/she could submit the form and it would go to the next page. The form had action="page2.php" method="post", and I didn't validate the information. But in order to ensure that the form wasn't submitted without a date, I had to add some validation. Towards the end of the validation code I had:

 

if ($startdate && $enddate)

 

Now clearly something has to happen after this code - the form must be submitted with the values and in my case be redirected to another page. What I meant above was that without validation I could submit the form and the values would be sent with the POST method, but with validation I need to use the header: location function, and the GET method.

Link to comment
Share on other sites

Yes, you are right about that. With your current implementation, you will have to perform a redirect back to the first page if the user enters an invalid date and then posts to page 2. That's exactly why I wouldn't implement things the way you are implementing them though.

 

I would make the whole form one page, and as the user submit parts of the form, reload the same page, validate the data, and then reveal more of the form as necessary.

Another big plus to this method is that you can use JS to perform basic validation and reveal more of the form without reloading the page.

 

That all make sense?

Link to comment
Share on other sites

 Share

×
×
  • Create New...