PDA

View Full Version : Creating forms with PHP script confirmation


stag
01-27-2007, 02:27 PM
Hi,

I have seen the "using dreamweaver and PHP to send form result in an email" tutorial, and my intention is to create a simple email capture form and send me the results via email as in the tutorial. However, a couple of questions-

1- i have heard you can make the form display some sort of confirmation when the email has been sent. How is this done?

2- when making forms, am I able to change the design and look eg CSS?

Cheers

(http://www.dreamweaverclub.com/forum/../dreamweaver-php-form.php)

davidj
01-27-2007, 03:06 PM
im not sure how that tutorial is setup but i look at it like this

your email form is a normal form which can be styled as you want using css.

i would just point the form to the script which performs the email task then once this has completed will redirect back to the page you came from but this time you will pass in a flag variable. The page you have returned to will look to see if this variable has been set and if so will display a message to confirm the email has been sent.

follow the tutorial and get it working then come back and we will get this message thingy working

stag
01-27-2007, 05:10 PM
I think im having problems making heads and tells of the script. I am using free web hosting to test (tripod) - The tutorial says copy the script and save the code in a file sendresults.php - err, I saved as a normal text file on mac and converted as php but im guessing this is totally wrong.

I understand the rest until pointing the script to process. Im not really sure about the Action bit probably cos ive b*lls up the first bit... :)

Urm, any guidence?

domedia
01-27-2007, 06:49 PM
The tutorial says copy the script and save the code in a file sendresults.php - err, I saved as a normal text file on mac and converted as php but im guessing this is totally wrong. Just open your texteditor, put your code into the file and save as sendresults.php :)

davidj
01-27-2007, 07:11 PM
does tripod support PHP?

stag
01-27-2007, 07:54 PM
Maybe not - Yup, just went thru support and the bugger doesnt offer it and not much else to be honest. Does anyone know of any free hosting which supports php?

Onto the php script again :) i copied the script into text editor and made the necessary changes and saved. I then placed into my route folder and in form properties for action, I placed the address of my website followed by file name for the php script. I then uploaded to host...

Does this sound about right?

davidj
01-27-2007, 08:04 PM
you could try writing it in klingon.

If your host doesnt suport PHP then the klingon version has as much chance of working as the correct version.

its like asking to drive a car without wheels and an engine

sort your hosting issues out first then we can proceed

stag
01-28-2007, 12:44 AM
Got the hosting sorted and tried the script. Something is working as the form redirects to the page I specifiy after inputing information within the form, but I dont seem to be recieving results to my email address. Ive checked my spam box just in case but nothing...

davidj
01-28-2007, 01:03 AM
post your code

stag
01-28-2007, 01:31 AM
Here is the form code (this is only a mock up form)


<form id="contact form" name="contact form" method="post" action="http://stag.xlphp.net/sendresults.php">
<label>
<div align="center"><span class="style7"><br />
Email:
<input name="textfield2" type="text" value="" size="20" />
</span><br />
<label>click me test</label>
<input name="Submit" type="submit" class="style2" value="submit" />
<br />
<span class="style2">Certified spam haters - Your email <br />
is safe with s&ouml;derhabet :)</span>
</label>
</div>
</form>


And php (the redirect form process is pointing to my tripod free host site)


<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Results from Contact form';

// Your email address. This is where the form information will be sent.
$emailadd = 'a*******@gmail.com';

// Where to redirect after form is processed.
$url = 'http://stag101.tripod.com/';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '1';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

davidj
01-28-2007, 01:42 AM
i see you are still redirecting to 'http://stag101.tripod.com/'

so you are still using tripod ??

here is a quick test to see if your host is using PHP

create a blank document and add the following...

<?php phpinfo(); ?>

save as phpinfo.php and upload it

navigate to ths page (live) through your browser and then tell me what appears.

stag
01-28-2007, 05:06 PM
no, i am no longer using tripod, I forgot to change the www page for re-routing after inputting details. Using some other free host which supports php. Just tried the test and the server im using displays the following-
Index of /


phpinfo.php (http://stag.xlphp.net/phpinfo.php)This looks like support for php?

davidj
01-28-2007, 05:08 PM
click on that phpinfo document in the INDEXof/

tell me what appears

stag
01-28-2007, 05:34 PM
An awful lot of info regarding PHP Version 5.2.0...

davidj
01-28-2007, 07:28 PM
thats great

ok now ask that host if they support php mail()

which host is it

stag
01-28-2007, 07:42 PM
Its these http://xlphp.net/
Have sent an email so will wait for an update. Is there anyway of finding out by looking at the info page?

chriskq
01-29-2007, 10:47 AM
stag can u plsnote wat u find here, i have been randomly browsing for some decent free hoster.

im looking to build a good personal site which will need php.

can it handle javascript too?

cheers stag

davidj
01-29-2007, 11:27 AM
can it handle javascript too?

chris.. Javascript is a clientside language not serverside

This means your machine processes the script so hosts would never need to support it as it doesnt work that way.

stag
01-30-2007, 12:57 AM
Hi

Im using http://www.your-websites.com and they have told me they support php mail. Have an active forum too so looks a good bet. The other one was previously using had incorrect contact so have had to change to these. Waiting for them to mail my password and then will set up using these.

Btw David, the form example im using from the tutorial, I am only using 2 form fields rather than the 3 in the example. Would this an issue as I havent made any changes to the php script?

Cheers

davidj
01-30-2007, 07:03 AM
which field have you omitted

stag
01-30-2007, 07:36 PM
actually, make that most of it. I have disregarded name, last name and comments. Thinking about it, I would like to have one name field along with , email address and submit button.

stag
02-10-2007, 12:55 PM
Right, got this sorted. Brought hosting and everything fine sending the results.

A couple more questions. I want the page to confirm once the email has been submitted. Do I get rid of the redirect field completely and place some HTML like "email confirmed" ???

Also I understand I need to place some if statements to stop the form sending blank results and incomplete email addresses. Again, how do I go about this?

Cheers

davidj
02-10-2007, 03:19 PM
Note at the end of your script the change i made

header("location:mail_sent_page.php");


where mail_sent_page.php is the mail sent confirmation page whch you will have to create


<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Results from Contact form';

// Your email address. This is where the form information will be sent.
$emailadd = 'a*******@gmail.com';

// Where to redirect after form is processed.
$url = 'http://stag101.tripod.com/';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '1';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');

header("location:mail_sent_page.php");
?>

stag
02-10-2007, 06:25 PM
Sorry, just to be absoutely clear on this, i create the confirmation and how do I link it to the correct page?

davidj
02-10-2007, 07:12 PM
Check the bottom the this script (your mail() script)

i have added a line so just add the line i have


<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Results from Contact form';

// Your email address. This is where the form information will be sent.
$emailadd = 'a*******@gmail.com';

// Where to redirect after form is processed.
$url = 'http://stag101.tripod.com/';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '1';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');

header("location:mail_sent_page.php"); /// <<< add this line
?>