PHP contact form, auto-populate and attachment help please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Loougle
Forum Newbie
Posts: 22
Joined: Fri Feb 25, 2011 11:23 am

PHP contact form, auto-populate and attachment help please

Post by Loougle »

I have been looking on this forum and some others and can't find an answer to this, I need help with auto-populating a form.

I want people to input data in 2 fields then on the backend have it auto-populate a form. They never need to see this form, I just need the form sent when they submit the contact form. ( or saved on a folder on the server, doesn't matter to me, whichever is easier)

Also I need help find a good code so people can include attachment documents to the contact form.

Code: Select all

<p style: align="center"><form action="mailtest.php" method="POST">
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" /> 
<div align="center">
<p class="style1">Name</p> 
<input type="text" name="name">
<p class="style1">Address</p> 
<input type="text" name="address">
<p class="style1">Email</p> 
<input type="text" name="email">
<p class="style1">Phone</p> 
<input type="text" name="phone">
<p class="style1">Debtor</p> 
<input type="text" name="debtor">
<p class="style1">Debtor Address</p> 
<input type="text" name="debtora">
<br />
<br />
<a href="authoforms.php" target="_blank" style="color:#ffcb00"  vlink="#ffcb00">Click here to view Assignment Agreement and Contract Agreement</a>

<p class="style1"><input type='checkbox' name='chk' value='I Have read and Agree to the terms.'> I have read and agree to the Assignment and Contract Agreement <br></p>
<p class="style1">Print Name</p> 
<input type="text" name="pname">
<p class="style1">Title</p> 
<input type="text" name="title">


<p class="style1">I hear by agree that the information I have provided is true, accurate and the information I am submitting is <br />
not fraudulent. Please click the agree button that you adhere to Commercial Recovery Authority Inc.'s terms:</p>
<select name="agree" size="1">
<option value="Agree">Agree</option>
<option value="Disagree">Disagree</option>
</select>
<br />
<br />
<p class="style1">Employee ID:</p> 
<input type="text" name="employee">

<br />

<input type="submit" value="Send"><input type="reset" value="Clear">
</div>
</form>
</p>
Ok so I need the field Print name and Title to autofill a form/pdf/doc (whatever is easier) on a backend folder or sent through email.

And what is a simple attachment code I can use.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: PHP contact form, auto-populate and attachment help plea

Post by danwguy »

in your mailtest.php you would use something like this to send off an email with those fields...

Code: Select all

$name = $_POST['name'];
$title = $_POST['title'];
$to = 'putyouremailaddresshere';
$subject = 'whatever you want your subject to be';
$message = "<html><head><title>your title</title></head><body><p><b>Name:</b> " . $name . "<br /><b>Title:</b> " . $title . "</body></html>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
and there you have it.
Loougle
Forum Newbie
Posts: 22
Joined: Fri Feb 25, 2011 11:23 am

Re: PHP contact form, auto-populate and attachment help plea

Post by Loougle »

This is the mailtest.php code I am using

Code: Select all

?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$debtor = $_POST['debtor'];
$debtora = $_POST['debtora'];
$value = $_POST['chk'];
$pname = $_POST['pname'];
$title = $_POST['title'];
$agree = $_POST['agree'];
$employee = $_POST['employee'];
$formcontent=" From: $name \n Address: $address \n Email: $email \n Phone: $phone \n Debtor: $debtor \n Debtor's Address: $debtora \n 'Client' has read Assignment and Contract Agreement: $value \n Print Name: $pname \n Title: $title \n I hear by agree that the information I have provided is true, accurate and the information I am submitting is not fraudulent. Please click the agree button that you adhere to Commercial Recovery Authority Inc.'s terms: $agree \n \n  Employee ID: $employee \n IP: $ip";
$recipient = "mail@crapower.com";
$subject = "Online Authorization Form 33.3%";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='index.php' style='text-decoration:none;color:#ffcb00;'> Return Home</a>";
$ip = $_POST['visitoraddress']
?>

Now I need the
$pname = $_POST['pname'];
$title = $_POST['title'];

(or print name field and title field on the form) to be displayed on a separate document it's going to be a contract form and we would like the words to be auto-populated.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: PHP contact form, auto-populate and attachment help plea

Post by danwguy »

You could store them in session variables and then have access to them on any page you want. look into this http://www.tizag.com/phpT/phpsessions.php and see if it helps at all.
Loougle
Forum Newbie
Posts: 22
Joined: Fri Feb 25, 2011 11:23 am

Re: PHP contact form, auto-populate and attachment help plea

Post by Loougle »

danwguy wrote:You could store them in session variables and then have access to them on any page you want. look into this http://www.tizag.com/phpT/phpsessions.php and see if it helps at all.

Code: Select all

<?php
session_register("pname");
session_register("title");
$pname = "Name";
$title = "CEO";
$sessionfile = fopen("sessionfile.txt", "w");
fputs($sessionfile, session_encode( ) );
fclose($sessionfile);
?>
Last edited by Loougle on Fri Feb 25, 2011 1:22 pm, edited 1 time in total.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: PHP contact form, auto-populate and attachment help plea

Post by danwguy »

Code: Select all

$pname = $_POST['pname'];
$title = $_POST['title'];
session_start();
$_SESSION['name'] = $pname;
$_SESSION['title'] = $title;
then on any page you want that data you would just use it...

Code: Select all

<form method='post' action='contract.php' />
<input type='text' value='<?php echo $_SESSION['name']; ?>' />
<input type='text value='<?php echo $_SESSION['title']; ?>' />
<input type='submit' value='submit' />
</form>
something like that should work out for ya.

EDIT: Please use the "PHP Code" button when posting code, it makes it easier to read. Thank you.
Loougle
Forum Newbie
Posts: 22
Joined: Fri Feb 25, 2011 11:23 am

Re: PHP contact form, auto-populate and attachment help plea

Post by Loougle »

danwguy wrote:

Code: Select all

$pname = $_POST['pname'];
$title = $_POST['title'];
session_start();
$_SESSION['name'] = $pname;
$_SESSION['title'] = $title;
then on any page you want that data you would just use it...

Code: Select all

<form method='post' action='contract.php' />
<input type='text' value='<?php echo $_SESSION['name']; ?>' />
<input type='text value='<?php echo $_SESSION['title']; ?>' />
<input type='submit' value='submit' />
</form>
something like that should work out for ya.

EDIT: Please use the "PHP Code" button when posting code, it makes it easier to read. Thank you.
woops sorry I used the php code button on the previous posts just forgot on that one.

So I have the original contact form, they put data into the fields, they hit the submit button it goes to my mailtest.php form so information gets submitted to me.

Now i'm going to add

Code: Select all

<p style: align="center"><form action="mailtest.php, contract.php" method="POST">
(so the form data gets sent to both mailtest.php and contract.php

Then place the two codes you gave me, 1st code set on the contact form, the 2nd code on contract.php

How do I store information from the contract.php?
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: PHP contact form, auto-populate and attachment help plea

Post by danwguy »

if you are sending the form data to both places then you can just use the

Code: Select all

$name = $_POST['pname'];
$title = $_POST['title'];
in contract.php and then use those variables to autopopulate the form with

Code: Select all

<form method='post' action='whatever'>
<input type='text' value='<?php echo $name; ?>' />
<input type='text' value='<?php echo $title; ?>' />
<input type='submit' value='submit' />
</form>
so your whole contract.php would look something like this...

Code: Select all

<?php
$name = $_POST['pname'];
$title = $_POST['title'];
?>
<html><head>//all your head info </head><body>
//all your body content here
]<form method='post' action='whatever'>
<input type='text' value='<?php echo $name; ?>' />
<input type='text' value='<?php echo $title; ?>' />
<input type='submit' value='submit' />
</form>
//the rest of your body content and footer
</body></html>
I was unaware that you could send a form to multiple pages at the same time, but if you can do that then there is no need to store the variables in a session since you are sending them directly to that page.
Loougle
Forum Newbie
Posts: 22
Joined: Fri Feb 25, 2011 11:23 am

Re: PHP contact form, auto-populate and attachment help plea

Post by Loougle »

I'm not sure if it's possible, but I'm going to try, it's worth a shot. If it doesn't work, i'll come back in here and we can go from there.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: PHP contact form, auto-populate and attachment help plea

Post by danwguy »

it is possible but you need to do some javascripting to make it happen... check out this page here... http://www.codeproject.com/KB/scripting/multiact.aspx and use _blank instead of iFrame in the target area, then your form will be sent to both pages with all the data along with it. I would use the iFrame target for the mailtest.php and _blank for the contract.php page.

EDIT: I have an idea on how to make it all work, come back and ask if you need anything after reading that page and testing it out.
Loougle
Forum Newbie
Posts: 22
Joined: Fri Feb 25, 2011 11:23 am

Re: PHP contact form, auto-populate and attachment help plea

Post by Loougle »

danwguy wrote:it is possible but you need to do some javascripting to make it happen... check out this page here... http://www.codeproject.com/KB/scripting/multiact.aspx and use _blank instead of iFrame in the target area, then your form will be sent to both pages with all the data along with it. I would use the iFrame target for the mailtest.php and _blank for the contract.php page.

EDIT: I have an idea on how to make it all work, come back and ask if you need anything after reading that page and testing it out.
Thank you.

Code: Select all

<script language="javascript">
<!--
function OnButton1()
{
    document.Form1.action = "contract.php"    // First target

    document.Form1.target = "iframe1";    // Open in a iframe

    document.Form1.submit();        // Submit the page
    return true;
}
-->
</script>
then I have my contract.php page

Code: Select all

<?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$pname = $_POST['pname'];
$title = $_POST['title'];
$formcontent=" content
Print Name: $pname                                                 
 
Title: $title";
$subject = "Contract";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$ip = $_POST['visitoraddress']
?>
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: PHP contact form, auto-populate and attachment help plea

Post by danwguy »

Looks good, on your contract.php where are you getting the $recipient variable from, I don't see it defined anywhere. Other than that I think it will work out nicely for ya.
Loougle
Forum Newbie
Posts: 22
Joined: Fri Feb 25, 2011 11:23 am

Re: PHP contact form, auto-populate and attachment help plea

Post by Loougle »

danwguy wrote:Looks good, on your contract.php where are you getting the $recipient variable from, I don't see it defined anywhere. Other than that I think it will work out nicely for ya.

Code: Select all

$recipient = "mail@crapower.com";
$subject = "Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

now.... here is a problem... none of the forms are working... I don't know if the server is slow, or what's going on, but I just tried some other contact forms that were working and now nothing is, so I think my server is backed up or I don't know.

But I did not get any error pages or anything while using the javascript so I'm hoping that does the trick.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: PHP contact form, auto-populate and attachment help plea

Post by danwguy »

don't forget to check your spam folder, I had a lot of forms that I sent off that got filtered into the spam folder for one reason or another. The code should work, it's written properly. Did you make the <div> space on your page for the second page to open into? did you hide it properly?

EDIT: Are you calling your function correctly in your html?
Loougle
Forum Newbie
Posts: 22
Joined: Fri Feb 25, 2011 11:23 am

Re: PHP contact form, auto-populate and attachment help plea

Post by Loougle »

danwguy wrote:don't forget to check your spam folder, I had a lot of forms that I sent off that got filtered into the spam folder for one reason or another. The code should work, it's written properly. Did you make the <div> space on your page for the second page to open into? did you hide it properly?

EDIT: Are you calling your function correctly in your html?

Ok after all this work over the weekend, my other forms are submitting properly except this one... but I'm able to access each people and I get no submitting errors so I gotta be missing something small.

Code: Select all

<script language="javascript">
<!--
function OnButton1()
{
    document.Autho.action = "contract.php"    // First target

    document.Autho.target = "iframe1";    // Open in a iframe

    document.Autho.submit();        // Submit the page
    return true;
}
-->
</script>

Code: Select all

<p style: align="center"><form name=Autho form action="mailtest.php" method="POST">
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" /> 
<div align="center">
<p class="style1">Name</p> 
<input type="text" name="name">
<p class="style1">Address</p> 
<input type="text" name="address">
<p class="style1">Email</p> 
<input type="text" name="email">
<p class="style1">Phone</p> 
<input type="text" name="phone">
<p class="style1">Field</p> 
<input type="text" name="field">
<p class="style1">Field Address</p> 
<input type="text" name="fielda">
<br />
<br />
<a href="authoforms.php" target="_blank" style="color:#ffcb00"  vlink="#ffcb00">Click here to view Assignment Agreement and Contract Agreement</a>

<p class="style1"><input type='checkbox' name='chk' value='I Have read and Agree to the terms.'> I have read and agree to the Assignment and Contract Agreement <br></p>
<p class="style1">Print Name</p> 
<input type="text" name="pname">
<p class="style1">Title</p> 
<input type="text" name="title">

<p class="style1">I hear by agree that the information I have provided is true, accurate and the information I am submitting is <br />
not fraudulent. Please click the agree button that you adhere to Commercial Recovery Authority Inc.'s terms:</p>
<select name="agree" size="1">
<option value="Agree">Agree</option>
<option value="Disagree">Disagree</option>
</select>
<br />
<br />
<p class="style1">Employee ID:</p> 
<input type="text" name="employee">

<br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</div>
</form>
</p>
<div style="visibility:hidden">
<IFRAME NAME="iframe1" WIDTH="40" HEIGHT="40"></IFRAME>
</div>
Now for the mailtest.php form

Code: Select all

<?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$debtor = $_POST['field'];
$debtora = $_POST['fielda'];
$value = $_POST['chk'];
$pname = $_POST['pname'];
$title = $_POST['title'];
$agree = $_POST['agree'];
$employee = $_POST['employee'];
$formcontent=" From: $name \n Address: $address \n Email: $email \n Phone: $phone \n Field: $field \n Field Address: $fielda \n 'Client' has read Assignment and Contract Agreement: $value \n Print Name: $pname \n Title: $title \n I hear by agree that the information I have provided is true, accurate and the information I am submitting is not fraudulent. Please click the agree button that you adhere to Commercial Recovery Authority Inc.'s terms: $agree \n \n  Employee ID: $employee \n IP: $ip";
$recipient = "mail@crapower.com";
$subject = "Online Authorization Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='index.php' style='text-decoration:none;color:#ffcb00;'> Return Home</a>";
$ip = $_POST['visitoraddress']
?>

Code: Select all

<?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$pname = $_POST['pname'];
$title = $_POST['title'];
$formcontent=" contract info
Print Name: $pname                                                 
 
Title: $title;
$recipient = "mail@crapower.com";
$subject = "Online Authorization Form 33.3%";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$ip = $_POST['visitoraddress']
?>
For some reason, neither form is getting sent... so I'm guessing it has to do with the html form?
Post Reply