...

View Full Version : Easy question for php programmers, I just want to display form data after submitting.



t-buck
11-29-2007, 06:08 AM
I just created a form that when submitted, it submits to register.php. This file connects to a sql database and then drops the data in and sends an email of the data. Everything works, and we're all good.

What I want to do, is from this file, it directs to a form submitted successfully page. On this page, I want to display some of the data that was submitted in the form. How do I go about doing this?

Here is what is in register.php

<?php
require 'functions.php';

dbconnect();

if ($_POST["Submit"]=="Submit")
{
//REGISTRATION INFO REQUIREMENTS
if (!$name) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>NAME.</B>"; exit(); }
if (!$nadults) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>LAST NAME.</B>"; exit(); }
if (!$nchildren) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>ORGANIZATION.</B>"; exit(); }
if (!$adultages) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>BUSINESS ADDRESS.</B>"; exit(); }
if (!$childages) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>BUSINESS CITY.</B>"; exit(); }
if (!$contact) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>BUSINESS STATE.</B>"; exit(); }
if (!$address) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>BUSINESS ZIP.</B>"; exit(); }
if (!$city) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>PHONE.</B>"; exit(); }

if (!$state) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>EMAIL.</B>"; exit(); }
if (!$zip) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>ZIP.</B>"; exit(); }
if (!$phone) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>TELEPHONE.</B>"; exit(); }
if (!$email) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>EMAIL.</B>"; exit(); }
if (!$transportation) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>TRANSPORTATION.</B>"; exit(); }

if (!$timeoutdoors) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>TIMEOUTDOORS.</B>"; exit(); }
if (!$visit) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>VISIT.</B>"; exit(); }
if (!$important) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>IMPORTANT.</B>"; exit(); }


// ENTER THE INFORMATION IN THE DATABASE
$date=date("m-d-Y");
$appid=time();
$sql="INSERT INTO registration (appid, date, name, nadults, nchildren, adultages, childages, contact, address, city, state, zip, phone, email, transportation, ethnicity, ethnicityother, timeoutdoors, visit, important) VALUES ('$appid', '$date', '$name', '$nadults', '$nchildren',
'$adultages', '$childages', '$contact', '$address', '$city', '$state', '$zip', '$phone', '$email','$transportation','$ethnicity','$ethnicityother','$timeoutdoors','$visit','$important')";
$result=mysql_query($sql);



// SEND AN EMAIL NOTIFICATION
$from="$email";
$fromname="$name";
$to="";
$subject="Lincoln Safari Registration";
$html="

<strong>Application ID:</strong> $appid<p>
<strong>Date:</strong> $date<p>
<strong>Applicants Name:</strong> $fname $lname<p>
<strong>Organization:</strong> $organization<p>
<strong>Nickname:</strong> $nickname<p>
<strong>Address:</strong> $baddress<p>
<strong>City, State Zip:</strong> $bcity, $bstate $bzip<p>
<strong>Telephone:</strong> $phone<p>

<strong>Email:</strong> $email<p>
<hr>
<strong>First Time Attendee:</strong> $attended<p>
<hr>
<strong>Total Amount Charged:</strong> $answer<p>
<strong>Credit Card Type:</strong> $cctype<p>
<strong>Credit Card Number:</strong> $ccnumber<p>
<strong>Credit Card Expiration:</strong> $ccexpm/$ccexpy<p>
<strong>Billing Address:</strong> $ccaddress<p>
<strong>Billing City, State Zip:</strong> $cccity, $ccstate $cczip<p>
<strong>Name on Card:</strong> $ccname<p>";
$text=ereg_replace("<br>","\r\n",$html);
$attm=array();
SendMail($from,$fromname,$to,$subject, $html,$text,$attm,"\r\n\r\n");

// SEND APPLICANT TO SUCCESSFUL PAGE
header ("Location:successful.php");
exit();
}
?>




And here is what is in function.php



function SendMail($From,$FromName,$To,$Subject,$Text,$Html,$AttmFiles)
{
//sends email with attachments
$OB="----=_OuterBoundary_000";
$IB="----=_InnerBoundery_001";
$Html=$Html?$Html:preg_replace("/\n/","{br}",$Text) or die("neither text nor h
tml part present.");
$Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
$From or die("sender address missing");
$To or die("recipient address missing");

$headers ="MIME-Version: 1.0\r\n";
$headers.="From: ".$FromName." <".$From.">\n";
$headers.="To: ".$ToName." <".$To.">\n";
$headers.="Cc: " ."\n";
$headers.="Reply-To: ".$FromName." <".$From.">\n";
$headers.="X-Priority: 1\n";
$headers.="X-MSMail-Priority: High\n";
$headers.="X-Mailer: My PHP Mailer\n";
$headers.="Content-Type: multipart/text;\n\tboundary=\"".$OB."\"\n";

//Messages start with text/html alternatives in OB
$Msg ="This is a multi-part message in MIME format.\n";
$Msg.="\n--".$OB."\n";
$Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";

//plaintext section
$Msg.="\n--".$IB."\n";
$Msg.="Content-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n";
$Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$Msg.=$Text."\n\n";

// html section
$Msg.="\n--".$IB."\n";
$Msg.="Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n";
$Msg.="Content-Transfer-Encoding: base64\n\n";
// html goes here
$Msg.=chunk_split(base64_encode($Html))."\n\n";

// end of IB
$Msg.="\n--".$IB."--\n";

// attachments
if($AttmFiles)
{
foreach($AttmFiles as $AttmFile)
{
$patharray = explode ("/", $AttmFile);
$FileName=$patharray[count($patharray)-1];
$Msg.= "\n--".$OB."\n";
$Msg.="Content-Type: application/octetstream;\n\tname=\"".$FileName."\"\n"
;
$Msg.="Content-Transfer-Encoding: base64\n";
$Msg.="Content-Disposition: attachment;\n\tfilename=\"".$FileName."\"\n\n"
;

//file goes here
$fd=fopen ($AttmFile, "r");
$FileContent=fread($fd,filesize($AttmFile));
fclose ($fd);
$FileContent=chunk_split(base64_encode($FileContent));
$Msg.=$FileContent;
$Msg.="\n\n";
}
}

//message ends
$Msg.="\n--".$OB."--\n";
mail($To,$Subject,$Msg,$headers);
//syslog(LOG_INFO,"Mail: Message sent to $ToName <$To>");

}
?>

homerUK
11-29-2007, 08:25 AM
I would probably set up the informtion you want to show in a session? eg:

at the top of register.php, add

session_start();

then in the code

// SEND APPLICANT TO SUCCESSFUL PAGE
header ("Location:successful.php");
exit();

change to



// SEND APPLICANT TO SUCCESSFUL PAGE
$_SESSION['name'] = $name;
$_SESSION['name'] = $contact; //etc
header ("Location:successful.php");
exit();


then on successful.php you add session_start() at the top and just display what you want.

Name: <?php echo $_SESSION['name']; ?><br />

There may be other ways, but that's probably what I would do...

aedrin
11-29-2007, 03:20 PM
Either store it in a session, or forward to a URL like this (might be a bit ugly):


result.php?value1=sometext&value2=somemoretext

t-buck
11-29-2007, 03:26 PM
Ok I added the code and then in successful.php this is the file:

<?php
session_start();

Name: <?php echo $_SESSION['name']; ?>

But I'm getting this error:

Parse error: parse error, unexpected ':' in /usr/local/apache/htdocs/www.lincolnsafari.org/Registration/successful.php on line 6

Inigoesdr
11-29-2007, 03:27 PM
You didn't close your first PHP tag before you put a plaintext string(Name:).

t-buck
11-29-2007, 03:30 PM
so the whole file should look like this:

<?php session_start(); ?>

Name: <?php echo $_SESSION['name']; ?>

Because that gives me this error:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /usr/local/apache/htdocs/www.lincolnsafari.org/Registration/successful.php:3) in /usr/local/apache/htdocs/www.lincolnsafari.org/Registration/successful.php on line 4

aedrin
11-29-2007, 04:09 PM
http://www.google.com/search?q=Cannot+send+session+cookie+-+headers+already+sent

"Results 1 - 10 of about 1,260,000"

t-buck
11-29-2007, 04:21 PM
Oh see I didn't know that. That's how dumb I am.

But the google posting helped, and it's now working correctly!

One last question, is there a way to assign a number to each form submitted, and have it be passed to the output as well? I need the number to start at 2,000 and just increment by one, for each submission.

For example, the first form submitted would have output of 2,000, and the second would have 2,001. How could I do this?

Inigoesdr
11-29-2007, 04:26 PM
<input type="hidden" name="number" value="<?php echo !empty($_POST['number']) ? ++$_POST['number'] : 2000; ?>" />

t-buck
11-29-2007, 04:33 PM
So would I put this in the form itself? And then in the register.php add
$_SESSION['number'] = $number;

And then do the output in the successful.php

fl00d
11-29-2007, 04:48 PM
what you could also do instead of sessions is just query the database and return it from there on successful.php

t-buck
11-29-2007, 04:51 PM
you mean for the number 2000? That would be awesome, but I have no idea how to do that. I have the table where the form is being dumped into, set to increment from 2000. So the number is assigned in the database as id. But how do I get that number for the form that was just submitted. No clue.

I would think that this would work too:
<input type="hidden" name="number" value="<?php echo !empty($_POST['number']) ? ++$_POST['number'] : 2000; ?>" />

But I can't get it to display on the output..... I just get the word "number" not an actual number.

aedrin
11-29-2007, 05:03 PM
I have the table where the form is being dumped into, set to increment from 2000. So the number is assigned in the database as id. But how do I get that number for the form that was just submitted. No clue.


$generatedId = mysql_insert_id();

mysql_insert_id (http://php.net/mysql_insert_id)

t-buck
11-29-2007, 05:10 PM
ok so I added this to register.php
$generatedId = mysql_insert_id();

and then at the bottom of that file:
$_SESSION['number'] = $generatedId;

And then in successful.php
Group Number: <?php echo $_SESSION['number']; ?> <br />

But the number outputted is "0" every time. Do I have this set up wrong? Is the id not assigned till after register.php has run? So do I need to somehow call the number from successful.php

Fumigator
11-29-2007, 05:47 PM
You want to make sure each step is working. Try adding die("generatedId is $generatedId"); just after you assign a value to that variable to see if that is working. If it is, move on to the next step and add a similar statement right after you assign your session variable.

You could show us your code again... you should use the [ PHP ] tags this time though. It does seem like you're on the right track.

t-buck
11-29-2007, 05:52 PM
Ok here is register.php

<?php
require 'functions.php';

session_start();

dbconnect();

if ($_POST["Submit"]=="Submit")
{
//REGISTRATION INFO REQUIREMENTS
if (!$name) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>NAME.</B>"; exit(); }
if (!$nadults) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>LAST NAME.</B>"; exit(); }
if (!$nchildren) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>ORGANIZATION.</B>"; exit(); }
if (!$adultages) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>BUSINESS ADDRESS.</B>"; exit(); }
if (!$childages) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>BUSINESS CITY.</B>"; exit(); }
if (!$contact) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>BUSINESS STATE.</B>"; exit(); }
if (!$address) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>BUSINESS ZIP.</B>"; exit(); }
if (!$city) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>PHONE.</B>"; exit(); }

if (!$state) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in your <b>EMAIL.</B>"; exit(); }
if (!$zip) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>ZIP.</B>"; exit(); }
if (!$phone) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>TELEPHONE.</B>"; exit(); }
if (!$email) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>EMAIL.</B>"; exit(); }
if (!$transportation) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>TRANSPORTATION.</B>"; exit(); }

if (!$timeoutdoors) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>TIMEOUTDOORS.</B>"; exit(); }
if (!$visit) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>VISIT.</B>"; exit(); }
if (!$important) { echo "Please <a href=javascript:history.go(-1)>go back</a> and fill in the <b>IMPORTANT.</B>"; exit(); }


// ENTER THE INFORMATION IN THE DATABASE
$date=date("m-d-Y");
$appid=time();
$generatedId = mysql_insert_id();
$sql="INSERT INTO registration (number, appid, date, name, nadults, nchildren, adultages, childages, contact, address, city, state, zip, phone, email, transportation, ethnicity, ethnicityother, timeoutdoors, visit, important) VALUES ('$number','$appid', '$date', '$name', '$nadults', '$nchildren',
'$adultages', '$childages', '$contact', '$address', '$city', '$state', '$zip', '$phone', '$email','$transportation','$ethnicity','$ethnicityother','$timeoutdoors','$visit','$important')";
$result=mysql_query($sql);




$from="$email";
$fromname="$name";
$to="";
$subject="";
$html="

<strong>Application ID:</strong> $appid<p>
<strong>Date:</strong> $date<p>
<strong>Applicants Name:</strong> $fname $lname<p>
<strong>Organization:</strong> $organization<p>
<strong>Nickname:</strong> $nickname<p>
<strong>Address:</strong> $baddress<p>
<strong>City, State Zip:</strong> $bcity, $bstate $bzip<p>
<strong>Telephone:</strong> $phone<p>

<strong>Email:</strong> $email<p>
<hr>
<strong>First Time Attendee:</strong> $attended<p>
<hr>
<strong>Total Amount Charged:</strong> $answer<p>
<strong>Credit Card Type:</strong> $cctype<p>
<strong>Credit Card Number:</strong> $ccnumber<p>
<strong>Credit Card Expiration:</strong> $ccexpm/$ccexpy<p>
<strong>Billing Address:</strong> $ccaddress<p>
<strong>Billing City, State Zip:</strong> $cccity, $ccstate $cczip<p>
<strong>Name on Card:</strong> $ccname<p>";
$text=ereg_replace("<br>","\r\n",$html);
$attm=array();
SendMail($from,$fromname,$to,$subject, $html,$text,$attm,"\r\n\r\n");

// SEND APPLICANT TO SUCCESSFUL PAGE
$_SESSION['number'] = $generatedId;
$_SESSION['name'] = $name;
$_SESSION['nadults'] = $nadults;
$_SESSION['nchildren'] = $nchildren;

header ("Location:successful.php");
exit();
}
?>


------------------------------
And here is functions.php
<?php

function validate($session)
{
$sql="SELECT * FROM logins WHERE session='$session'";
$result=mysql_query($sql);
if(mysql_num_rows($result)==0) return false;
else return true;
}


function dbconnect()
{
$db=mysql_connect(".....");
mysql_select_db(".....",$db);
}

function SendMail($From,$FromName,$To,$Subject,$Text,$Html,$AttmFiles)
{
//sends email with attachments
$OB="----=_OuterBoundary_000";
$IB="----=_InnerBoundery_001";
$Html=$Html?$Html:preg_replace("/\n/","{br}",$Text) or die("neither text nor h
tml part present.");
$Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
$From or die("sender address missing");
$To or die("recipient address missing");

$headers ="MIME-Version: 1.0\r\n";
$headers.="From: ".$FromName." <".$From.">\n";
$headers.="To: ".$ToName." <".$To.">\n";
$headers.="Cc: " ."\n";
$headers.="Reply-To: ".$FromName." <".$From.">\n";
$headers.="X-Priority: 1\n";
$headers.="X-MSMail-Priority: High\n";
$headers.="X-Mailer: My PHP Mailer\n";
$headers.="Content-Type: multipart/text;\n\tboundary=\"".$OB."\"\n";

//Messages start with text/html alternatives in OB
$Msg ="This is a multi-part message in MIME format.\n";
$Msg.="\n--".$OB."\n";
$Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";

//plaintext section
$Msg.="\n--".$IB."\n";
$Msg.="Content-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n";
$Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$Msg.=$Text."\n\n";

// html section
$Msg.="\n--".$IB."\n";
$Msg.="Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n";
$Msg.="Content-Transfer-Encoding: base64\n\n";
// html goes here
$Msg.=chunk_split(base64_encode($Html))."\n\n";

// end of IB
$Msg.="\n--".$IB."--\n";

// attachments
if($AttmFiles)
{
foreach($AttmFiles as $AttmFile)
{
$patharray = explode ("/", $AttmFile);
$FileName=$patharray[count($patharray)-1];
$Msg.= "\n--".$OB."\n";
$Msg.="Content-Type: application/octetstream;\n\tname=\"".$FileName."\"\n"
;
$Msg.="Content-Transfer-Encoding: base64\n";
$Msg.="Content-Disposition: attachment;\n\tfilename=\"".$FileName."\"\n\n"
;

//file goes here
$fd=fopen ($AttmFile, "r");
$FileContent=fread($fd,filesize($AttmFile));
fclose ($fd);
$FileContent=chunk_split(base64_encode($FileContent));
$Msg.=$FileContent;
$Msg.="\n\n";
}
}

//message ends
$Msg.="\n--".$OB."--\n";
mail($To,$Subject,$Msg,$headers);
//syslog(LOG_INFO,"Mail: Message sent to $ToName <$To>");

}
?>

------------------------------
And finally, successful.php
<?php
session_start(); ?>
Group Number: <?php echo $_SESSION['number']; ?> <br />
Number of adults: <?php echo $_SESSION['nadults']; ?> <br />
Number of children: <?php echo $_SESSION['nchildren']; ?><br />
Group Name: <?php echo $_SESSION['name']; ?>

Fumigator
11-29-2007, 05:59 PM
Whoops, you forgot to put your code inside [ PHP ] and [ /PHP ] tags (no spaces).

At any rate, the mysql_insert_id() should be the first thing you do after the INSERT query, not before.

Also make sure you check all your queries for errors. It's good programming practice and will save you endless headache.

t-buck
11-29-2007, 06:08 PM
so on all three files I need
[ PHP ] at the top

and [ /PHP] at the bottom?

and the mysql_insert_id() should be moved to the very bottom?

Fumigator
11-29-2007, 06:45 PM
so on all three files I need
[ PHP ] at the top

and [ /PHP] at the bottom?


Just for the benefit of us, posting code, in this forum here. Don't put that in your actual code.



and the mysql_insert_id() should be moved to the very bottom?

Put it right after your INSERT query.

fl00d
11-30-2007, 04:54 AM
you mean for the number 2000? That would be awesome, but I have no idea how to do that. I have the table where the form is being dumped into, set to increment from 2000. So the number is assigned in the database as id. But how do I get that number for the form that was just submitted. No clue.

I would think that this would work too:
<input type="hidden" name="number" value="<?php echo !empty($_POST['number']) ? ++$_POST['number'] : 2000; ?>" />

But I can't get it to display on the output..... I just get the word "number" not an actual number.

You could just assign the id to a session, then query the database and select from where the id equals the session. that way you only need to set and call 1 session.

page1.php - sets the session


$id = $_POST['id'];
$_SESSION['id'] = $id;


page2.php - sucessful


$id = $_SESSION['id'];
//setup query
$sql = "SELECT * FROM table_name WHERE id='$id'";
$query = mysql_query($sql) or die("Error: ".mysql_error());

//get the info from db so it can be displayed
while($row = mysql_fetch_array($query){
$info1 = $row['info1'];
$info2 = $row['info2'];
//display the info
echo $info1."<br />".$info2;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum