Page 1 of 1

Pass form data to another page

Posted: Thu Sep 30, 2010 8:21 pm
by sss123
Hi there,

I currently have a simple form that potential clients use to enter their details. I need to be able to pass this data to a hidden field on the next page when they click submit. I have found some code but have been unble to make it work.

One of the fields I am trying to get from the first page is called "realname".The code I found for retreaving data on the second page is as follows:
<form><input type="hidden" name="realname" value="<?php echo $_POST["realname"]; ?>"></form>

Do I need any code on the first page to 'send' the data to next the page?

Thanks in advance for any help with this, it is much appriciated.

Kind regards

Mike

Re: Pass form data to another page

Posted: Fri Oct 01, 2010 1:32 am
by cpetercarter
But what code have you got on your first page? The form (on the first page) needs an "action" attribute which points to the second page, like this:

Code: Select all

<form action="my_second_page.php" method="post">
<-- content of form -->
</form>
This will of course send all the form data to my_second_page.php for processing, not just the 'realname' field.

Re: Pass form data to another page

Posted: Fri Oct 01, 2010 5:57 am
by DigitalMind
sss123 wrote:

Code: Select all

<form><input type="hidden" name="realname" value="<?php echo $_POST["realname"]; ?>"></form>
I would write

Code: Select all

value="<?php echo (isset($_POST["realname"]) ? $_POST["realname"] : ''); ?>"

Re: Pass form data to another page

Posted: Fri Oct 01, 2010 8:34 am
by sss123
Hi, thank you very much for your posts.

I think I realise where I am going wrong, however I am unsure how to fix it.

Basically, when the user clicks submit on the first page, the 'action=' part of the form loads a php file which uploads the values to mysql database and sends me an email with the entered values.

What I then wanted to do was also send a sort of autoresponse email to the user, using the email address they enter. However when I added another send function to the php, it wouldn't send either email.

So I thought I would redirect the php file (after it had uploaded to the databse and sent me an email) to a thank you page with hidden fields (containing the initial values) so that I could use another send function, for the auto response email.

I hope this makes sense! Can you see anyway of achieving this?

Thank you again for your time, I really appriciate it!

Kind regards

Mike

Re: Pass form data to another page

Posted: Sat Oct 02, 2010 1:43 am
by cpetercarter
You can achieve all this with one php script.

Code: Select all

<?php
if (!empty($_POST))
{
    //validate user input
    //add stuff to database (remember to use mysql_real_escape_string())
    //send email to you
    if (!empty($_POST['useremail']))
    {
         //send email to user
    }
    //display a welcome page
}
else
{
    //display form (action attribute points to this script, or is omitted)
}
?>
You say that when previously you had tried a structure like this, the programme would not send either of the emails. The solution is to find the error which is blocking the emails, not to invent a new and more complicated structure in the hope that it will get round the problem. Error messages may help you to find what is wrong, Do you have error reporting switched on? (see http://php.net/manual/en/function.error-reporting.php)

Re: Pass form data to another page

Posted: Sat Oct 02, 2010 6:31 am
by sss123
Hi thank you for your most useful post.

That worked a treat!

I don't have error reporting, this sound slike something that I should have though. I'll look into it. With regards to the two emails, the first one would send on its own, but when another send function was added after it, neither would send. I think that possible you can only send one email with the send function? I tried copying the code that worked on its own to see if it would send that twice and it wouldn't.

Do you know if it is possible to use the send function twice in one php script?

Thank you so much for your time and help.

Kind regards

Mike

Re: Pass form data to another page

Posted: Sun Oct 03, 2010 1:01 pm
by cpetercarter
You can only send one email at a time with the mail(), but of course you can call mail() as many times as you like in the course of a script. If you post the code you which fails to work, someone in the Forum may be able to see where the problem lies.

Re: Pass form data to another page

Posted: Sun Oct 03, 2010 1:32 pm
by sss123
Hi there,

Thank you for your continuing support!

The code for the two mail functions is:

<?php
$to = $_POST["email"];
$subject = "Thank You";
$body ="Dear ";
$body .= $_POST["realname"];
$body .= ",\n\nThank you for your enquiry.\n\nWe have received your enquiry and will contact you shortly.\n\nKind regards.";
$email = "autoreply@******";

function is_valid_email($email) {
return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);
}

function contains_bad_str($str_to_test) {
$bad_strings = array(
"content-type:"
,"mime-version:"
,"multipart/mixed"
,"Content-Transfer-Encoding:"
,"bcc:"
,"cc:"
,"to:"
);

foreach($bad_strings as $bad_string) {
if(eregi($bad_string, strtolower($str_to_test))) {
echo "$bad_string found. Suspected injection attempt - mail not being sent.";
exit;
}
}
}

function contains_newlines($str_to_test) {
if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) {
echo "newline found in $str_to_test. Suspected injection attempt - mail not being sent.";
exit;
}
}

if (!is_valid_email($email)) {
echo 'Invalid email submitted - mail not being sent.';
exit;
}

contains_bad_str($email);
contains_bad_str($subject);
contains_bad_str(body);

contains_newlines($email);
contains_newlines($subject);

$headers = "From: $email";
mail($to, $subject, $body, $headers);
echo "";
?>

<?php
$to = "sales@******";
$subject = "New Enquiry";
$body = $_POST["realname"];
$body .= $_POST["number"];
$email = "sales@******";

function is_valid_email($email) {
return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);
}

function contains_bad_str($str_to_test) {
$bad_strings = array(
"content-type:"
,"mime-version:"
,"multipart/mixed"
,"Content-Transfer-Encoding:"
,"bcc:"
,"cc:"
,"to:"
);

foreach($bad_strings as $bad_string) {
if(eregi($bad_string, strtolower($str_to_test))) {
echo "$bad_string found. Suspected injection attempt - mail not being sent.";
exit;
}
}
}

function contains_newlines($str_to_test) {
if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) {
echo "newline found in $str_to_test. Suspected injection attempt - mail not being sent.";
exit;
}
}

if (!is_valid_email($email)) {
echo 'Invalid email submitted - mail not being sent.';
exit;
}

contains_bad_str($email);
contains_bad_str($subject);
contains_bad_str(body);

contains_newlines($email);
contains_newlines($subject);

$headers = "From: $email";
mail($to, $subject, $body, $headers);
echo "";
?>

Thanks again.

Kind regards

Mike

Re: Pass form data to another page

Posted: Sun Oct 03, 2010 4:49 pm
by cpetercarter
Your script contains the functions is_valid_email(), contains_bad_str(), and contains_new _lines() twice. You can only, and need only, declare a function once. If you enabled error reporting you would find a fatal error notice to say that the function is_valid_email() has already been declared and cannot be declared again.

If you remove the second instance of each of these functions, I suspect that your script will work fine.

Re: Pass form data to another page

Posted: Sun Oct 03, 2010 7:22 pm
by sss123
Hi Peter,

Thank you very much. I deleted the repeated functions and the script works fine now. :D

Thanks again!

Kind regards,

Mike