Jump to content

Help with PHP Contact Form


lander

Recommended Posts

Hi,

 

We have created a contact form using RapidWeaver and we keep getting this same error. Any ideas of what might be causing this?

 

Notice: Undefined variable: tmp_name in c:\domains\designsensepanama.com\wwwroot\sourcefiles\mailer.php on line 25

 

Warning: Cannot modify header information - headers already sent by (output started at c:\domains\designsensepanama.com\wwwroot\sourcefiles\mailer.php:25) in c:\domains\designsensepanama.com\wwwroot\sourcefiles\mailer.php on line 58

 

We have our whole site pretty much ready to go, but cant figure out the above error.  Out contact form works, but instead of reloading the page with “thank-you your mail has been sent” we get a blank page with the above error.  The email does get sent though.

 

our code:

 

<?php
        if(!array_key_exists('formMessage', $_SESSION))
        $_SESSION['formMessage'] = "";
        if(!array_key_exists('form_element0', $_SESSION))
        $_SESSION['form_element0'] = "";
        if(!array_key_exists('form_element1', $_SESSION))
        $_SESSION['form_element1'] = "";
        if(!array_key_exists('form_element2', $_SESSION))
        $_SESSION['form_element2'] = "";
        if(!array_key_exists('form_element3', $_SESSION))
        $_SESSION['form_element3'] = "";
        ?>
                <div class="message-text">
                  <?php
          if (!$_SESSION['formMessage']) { 
          echo 'Fill in the form below to send us an email.';
          } else {
           echo $_SESSION['formMessage'];
           }
           ?>
                </div>
                <br />
                <form action="sourcefiles/mailer.php"
              method="post"
              enctype="multipart/form-data">
                  <label>Your Name:</label>
                  * <br />
  <input class="form-input-field"
                type="text"
                value="<?php echo $_SESSION['form_element0']; ?>"
                name="form_element0"
                size="40" />
  <br />
  <br />
  <label>Your Email:</label>
                  * <br />
  <input class="form-input-field"
                type="text"
                value="<?php echo $_SESSION['form_element1']; ?>"
                name="form_element1"
                size="40" />
  <br />
  <br />
  <label>Subject:</label>
                  * <br />
  <select name="form_element2">
    <option <?php if ($_SESSION['form_element2'] == "I am interested in a new web presence") { echo "selected"; } ?>
                    value="I am interested in a new web presence"> I am interested in a new web presence </option>
    <option <?php if ($_SESSION['form_element2'] == "I am interested in a re-design of my existing site.") { echo "selected"; } ?>
                    value=
                    "I am interested in a re-design of my existing site."> I am interested in a re-design of my existing site. </option>
    <option <?php if ($_SESSION['form_element2'] == "I am interested in website management") { echo "selected"; } ?>
                    value="I am interested in website management"> I am interested in website management </option>
    <option <?php if ($_SESSION['form_element2'] == "I am interested in website marketing") { echo "selected"; } ?>
                    value="I am interested in website marketing"> I am interested in website marketing </option>
  </select>
  <br />
  <br />
  <label>Message:</label>
                  * <br />
  <textarea class="form-input-field"
                name="form_element3"
                rows="8"
                cols="38">
<?php echo $_SESSION['form_element3']; ?>
</textarea>
  <br />
  <br />
  <input class="form-input-button"
                type="reset"
                name="resetButton"
                value="Reset" />
  <input class="form-input-button"
                type="submit"
                name="submitButton"
                value="Submit" />
                </form>
                <?php session_destroy(); ?> 

 

And our mailer.php code

 

<?php
session_start();
$_SESSION['form_element0'] = $_POST['form_element0'];
$_SESSION['form_element1'] = $_POST['form_element1'];
$_SESSION['form_element2'] = $_POST['form_element2'];
$_SESSION['form_element3'] = $_POST['form_element3'];
$form_element0 = $_SESSION['form_element0'];
$form_element1 = preg_replace("/\r/", "", $_SESSION['form_element1']);
$form_element1 = preg_replace("/\n/", "", $form_element1);
$form_element2 = preg_replace("/\r/", "", $_SESSION['form_element2']);
$form_element2 = preg_replace("/\n/", "", $form_element2);
$form_element3 = $_SESSION['form_element3'];
if(!$_SESSION['form_element0'] || !$_SESSION['form_element1'] || !$_SESSION['form_element2'] || !$_SESSION['form_element3']) {
$_SESSION['formMessage'] = "        Please fill out all the required fields<br />Fields marked with * are required.\n";
Header("Location: contactus.php");
exit();
} else {
$email="our e-mail address is here";

if (!eregi ("^([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,4}$", $email)) {
unset($email);
}

$from = stripslashes($form_element0)." <".stripslashes($form_element1).">";$subject= $form_element2;$message = "\nYour Name: $form_element0\n\nYour Email: $form_element1\n\nSubject: $form_element2\n\nMessage: $form_element3\n";
$headers="From: $from\n";if (file_exists($tmp_name)){
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));

}

$headers .= "MIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"{$mime_boundary}\"";

$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n"; 

$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n"; 
}

SESSION_UNREGISTER('form_element0');
SESSION_UNREGISTER('form_element1');
SESSION_UNREGISTER('form_element2');
SESSION_UNREGISTER('form_element3');
if (@mail($email, $subject, $message, $headers)) {$_SESSION["formMessage"] = "        ".'Thank you, your email has been sent.'."
";
header("Location: contactus.php");
} else {
$_SESSION["formMessage"] = "I'm sorry, there seems to have been an error trying to send your email. Please try again.";
header("Location: contactus.php");
}
}
?> 

 

Thanks!

Link to comment
Share on other sites

Are you running this off of your website or is it still on a local server? I always got this error (the 1st error) when I was building pages on my localhost but when I uploaded it to the website it worked fine. The second error looks like you have an undefined variable tmp_name lol.

Link to comment
Share on other sites

That's not really fixing the problem thesaleboat.

 

You need to figure what $tmp_name is supposed to be. You are using it here

if (file_exists($tmp_name)){

but I don't see it being set anywhere.

 

Fix that and the notice will go away and it should then redirect.

Link to comment
Share on other sites

Yes I've been reading.

 

And thanks thesaleboat! Your code fixed the second error not the first. When I hit submit I get the page with the errors and then get redirected to the contact form only this time I can see the thank you message.

Link to comment
Share on other sites

haha.

I don't understand your line of though how fixing the undefined variable problem wouldn't allow it to be re-directed...

The Notice

Warning: Cannot modify header information - headers already sent by

comes AFTER the

Notice: Undefined variable: tmp_name

in the same file.

Because you can't issue a header(s) after there has been output.

 

lander, declare the $tmp_name variable and you'll be set. Try it.

Link to comment
Share on other sites

Yeah man, you know how you defined all of your other variables at the top aka

$form_element0 = $_SESSION['form_element0'];

You are going to have to do something like that above line 25, I am guessing its supposed to be a temporary name of some file, but you do not have anything assigned to it, like something like

$tmp_name = $_SESSION['filename']; //get the filename from the form

Link to comment
Share on other sites

Ive never seen it before but it seems like its checking to see if they did upload a file,  and then fopen i am thinking is file open, fread - file read, fclose ... etc, and then it encodes the file 64 bit encryptions, but maybe that is just the way it sends the email. i just started to learn php 3 months ago

Link to comment
Share on other sites

Question...

 

Is that part of the code indicating that I am allowing users to upload a file?

 

$headers="From: $from\n";if (file_exists($tmp_name)){

 

Yes, that is where the Notice error is coming from. You're checking a variable $tmp_name but that variable doesn't exist.

Do you have something on your form asking for a file name or something? If so then do something like

if(!empty($_POST['file_name']) {
  $tmp_name = $_POST['file_name'];
else
  $tmp_name = '';

 

Then you won't get that Notice message and your page will re-direct like you want it to.

Link to comment
Share on other sites

When I saw that part of the code I thought it was doing something like that because I started seeing is_uploaded_file, fopen, fread, etc. But you see, the form and the code that validates it is being generated by RapidWeaver. I never chose the option or added any fields where I would allow the users to upload files so I guess there is no point of having that part of the code in there right? I am now wondering how it actually got in there?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.