PDA

View Full Version : mail form question


rattlsnak
08-28-2008, 10:33 PM
Hello, a few questions on this mail form. At the bottom there is this code:

echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
referencing this: $url = 'http://www.anydomainnamehere.com/requestsent.php';

Can this be changed to a Header function, or a document.location code instead since where i want it to go will still be inside the member pages in the same directory and in the same session? Like header=('location.requestsent.php'); ?
I realize it will get to the same place, but am wondering if I had to use the url function.

code for the mail form:


//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'Form Results';
// Your email address. This is where the form information will be sent.
$emailadd = 'youremailaddress.com;
// Where to redirect after form is processed.
$url = 'http://www.anydomainnamehere.com/requestsent.php';
// 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.'">';
?>

Now, I have a member login page ahead of this with a session set, so the people and their email addresses who will be using this form are known by the login validation through the database.

I have a form that has 10 people listed with a checkbox next to each one. I want the logged in user to be able to check a checkbox and hit submit and be able to send that person an email. I have changed the value of each checkbox to that persons email address. (is that the right direction?) So I'm am guessing that I need to change the $emailadd variable to = the person checked and the From. to the logged in persons email.
I am on the right track here?

rattlsnak
08-29-2008, 05:40 AM
Now, I have a member login page ahead of this with a session set, so the people and their email addresses who will be using this form are known by the login validation through the database. The session however is set from their username and not their email address.

OK, I have that part fixed now. I have a session variable that reads their email address.

I have changed the form so it has 10 people listed with a checkbox next to each one. I want the logged in user to be able to check a checkbox and hit submit and be able to send that person an email. I have changed the value of each checkbox to that persons email address. (is that the right direction?) So I'm am guessing that I need to change the $emailadd variable to = the person checked and the From. to the logged in persons email.
I am on the right track here?

OK fixed,.. Made an array of all the checkboxes to correct the TO field and used a session variable for the FROM field.

Blog continued.. :) :)

davidj
08-29-2008, 09:01 AM
hahaha

mad as a box of frogs

rattlsnak
08-29-2008, 03:04 PM
[quote]
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
referencing this: $url = 'http://www.anydomainnamehere.com/requestsent.php';

Can this be changed to a Header function, or a document.location code instead since where i want it to go will still be inside the member pages in the same directory and in the same session? Like header=('location.requestsent.php'); ?
I realize it will get to the same place, but am wondering if I had to use the url function.
[/qoute]

I still have that question.

And one more:

How do i keep all of the POST values from echoing on the email?

i.e. it echos 'submit' and the checkbox value, along with the stuff i want like the name and comments.

davidj
08-29-2008, 03:12 PM
this wont redirect until the page has been completly loaded


echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';


this will redirect before the headers are sent so no page will load

header("location:$url");
exit;


are you sure its $_POST being sent and not $_REQUEST

rattlsnak
08-29-2008, 03:29 PM
this wont redirect until the page has been completly loaded


echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';


this will redirect before the headers are sent so no page will load

header("location:$url");
exit;




Ok, are you talking about the the page it is being sent to, or the page currently on?

The page I have this on is simply a php page, with no html, just for checking the form data and then redirecting to a 'form sent' page. So which would be better for that situation?

are you sure its $_POST being sent and not $_REQUEST

Well I dont see a $_REQUEST anywhere on the form code and it is set to 'post'.
The entire script came from this site. I just built the html form like it said to here:

Step 1: Create a form


We have created a simple form with four form fields as:

Name (text field)
Last name (text field)
Email address (text field)
Comments (textarea)http://www.dreamweaverclub.com/article-files/php-form/contactform.gif

When I get the email back, it shows up like this:

checkbox: 'value of checkbox' << i have added a checkbox input to the form.
name: Bob
last_name: Smith
email_address: youremailaddress@hotmail.com
comments: testing1
Submit: Submit

I dont want the 'checkbox' or 'submit' lines to show up. or for that matter, how do i change the 'name' last_name' values?

davidj
08-29-2008, 03:55 PM
remember that $_POST is an Array

there is a line...


foreach ($_POST as $key => $value)


What this loop is doing is getting everything in the $_POST array and assigning it to $value looping as it goes

you can check whats in the $_POST array by using a print_r() on it...

/// save to snippets ///

echo "<pre>";
print_r($_POST);
echo "</pre>";


all you have to do to omit those you dont want displayed is to use an IF inside the loop before the rest of the code

like this...


foreach ($_POST as $key => $value){

if($key !="Submit" || $key !="CheckBoxName"){

/// rest of code //////


///////////////////////////////////////

this is HTML yes...


echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';


so if your echoing it in an empty page then its invalid markup. It may work and refresh to the url but its still invalid

the correct method would be to use a...


header("location");

rattlsnak
08-29-2008, 04:57 PM
ok when i add that code i get this:

Parse error: syntax error, unexpected '!'

davidj
08-29-2008, 05:01 PM
need to see the code

rattlsnak
08-29-2008, 05:02 PM
this is HTML yes...


HTML Code:
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
so if your echoing it in an empty page then its invalid markup. It may work and refresh to the url but its still invalid

the correct method would be to use a...


PHP Code:
header("location");



OK, what I have is the html page which has the form on it, that when submitted gets sent/validated to a php page with no html and no display, that when the script is complete, then redirects to a html page.

So, which one is correct?

rattlsnak
08-29-2008, 05:05 PM
//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'Form Results';
// Your email address. This is where the form information will be sent.
$emailadd = 'youremailaddress.com;
// Where to redirect after form is processed.
$url = 'http://www.anydomainnamehere.com/requestsent.php';
// 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($key !="submit" || $key !="checkboxname")

{
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
08-29-2008, 05:53 PM
this is HTML yes...


HTML Code:
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
so if your echoing it in an empty page then its invalid markup. It may work and refresh to the url but its still invalid

the correct method would be to use a...


PHP Code:
header("location");



OK, what I have is the html page which has the form on it, that when submitted gets sent/validated to a php page with no html and no display, that when the script is complete, then redirects to a html page.

So, which one is correct?

use a header where there is no html output

davidj
08-29-2008, 06:00 PM
//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'Form Results';
// Your email address. This is where the form information will be sent.
$emailadd = 'youremailaddress.com;
// Where to redirect after form is processed.
$url = 'http://www.anydomainnamehere.com/requestsent.php';
// 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($key !="submit" || $key !="checkboxname")

{
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.'">';
}
?>

you missed a currly brace

rattlsnak
08-29-2008, 07:26 PM
hmmmm, the code has been working fine without the ending curly. its when i put in this:
{ if($key !="submit" || $key !="checkboxname")
is when I get the error, pointing to this line, whether the ending curly is there or not.. When i take it back out, and even without the ending curly, it works fine.

rattlsnak
08-31-2008, 02:51 PM
Any ideas on this?

this is what it displays in the email:

checkbox: email @adress.com
fname: joe
lname: smith
submit: submit

I would like to get rid of the checkbox and submit displays on the email.

What i have done for now, is changed the name on the checkbox button and deleted the name of the submit button name and it works, but i would like to know how to do it correctly.

davidj
08-31-2008, 05:39 PM
you could use an unset on the $_POST array


unset($_POST['submit'])

unset($_POST['checkboxname']) /// << replace this with the name of the checkbox you want to omit

rattlsnak
08-31-2008, 09:11 PM
So that would have to go after the 'foreach' line somewhere?
Or is there a way to make the three items i want posted into an array (while not including the two I dont) and then use that variable in the foreach loop?