-1

I have been trying for ours, i just can't get this to work, your help is much appreciated.

    <?php
/*
This first bit sets the email address that you want the form to be submitted to.
You will need to change this value to a valid email address that you can access.
*/
$webmaster_email = "contact@mysite.com";

/*
This bit sets the URLs of the supporting pages.
If you change the names of any of the pages, you will need to change the values here.
*/
$feedback_page = "contact.html";
$error_page = "error.html";
$thankyou_page = "thankyou.html";

/*
This next bit loads the form field data into variables.
If you add a form field, you will need to add it here.
*/
$name = $_REQUEST['name'] ;
$email_address = $_REQUEST['email_address'] ;
$subject = $_REQUEST['subject'] ;
$comments = $_REQUEST['comments'] ;

/*
The following function checks for email injection.
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
*/
function isInjected($str) {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str)) {
        return true;
    }
    else {
        return false;
    }
}

// If the user tries to access this script directly, redirect them to the feedback form,
if (!isset($_REQUEST['email_address'])) {
header( "Location: $feedback_page" );
}

// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)) {
header( "Location: $error_page" );
}

// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
header( "Location: $error_page" );
}

// If we passed all previous tests, send the email then redirect to the thank you page.
else {
    $eml = "Name: ".$name."\r\n"."Subject: ".$subject."\r\n"."Comments: "."\r\n".$comments;

mail( "$webmaster_email", "Visitor Contact",
 $eml, "From: $email_address" );
header( "Location: $thankyou_page" );
}
?>

Alright you see the PHP above, all i want is for the php file send me the error or success string back to the html in <h1> header </h1> instead of redirecting me to those page.

Matt Ball
  • 332,322
  • 92
  • 617
  • 683
NetInfo
  • 559
  • 2
  • 6
  • 19
  • 2
    `

    header

    `, huh? Where?
    – Funk Forty Niner Aug 02 '13 at 03:56
  • Where can we see that in the PHP code? Every path in that code leads to a `header()` call to redirect the user. There's no HTML emitted at all. – David Aug 02 '13 at 03:56
  • What do you mean by "send me the error"? Are you trying to `echo` it instead? Are you making an AJAX request and would like to get back the error? Are you loading this from another file and wanting to get this back in a string? Please add some more context. Remember, we don't know what you're trying to do. – jprofitt Aug 02 '13 at 03:56
  • I'll bet it's inside `error.html` and/or `thankyou.html` (wink) Now where'd I put that crystal ball of mine?! Oh darn it, cat's playing with it. Sorry, cat takes precedence. – Funk Forty Niner Aug 02 '13 at 03:57
  • the

    whatever

    in my original html page.
    – NetInfo Aug 02 '13 at 03:58
  • basically what the file does, if there is an error, like empty email address it redirects to the errorpage.html page, instead of that i want it to send me a string to feedback_form.html to an element in that html file like

    .
    – NetInfo Aug 02 '13 at 04:00
  • the thankyou.html page is nothing but a simple html page that... well just says thank you for contacting us, and the error page says try to fill out all the fields, the problem i have is in the php file. – NetInfo Aug 02 '13 at 04:03
  • Ok I see now. Change `header( "Location: $feedback_page" );` to `echo "Thank you!";` and `header( "Location: $error_page" );` to `echo "Error sending mail";` etc. – Funk Forty Niner Aug 02 '13 at 04:07
  • the original html file lets call it "index.html" there is a form on it that posts the data (name, email...) to the php file, now when there is an error or success the php file redirects me to those pages "header( "Location: $error_page" );", i want it to be like this for instace "send "error or whatever" to index.html textbox1. – NetInfo Aug 02 '13 at 04:09
  • And if you want them set inside `

    `, do `echo "

    You have errors

    ";` etc.
    – Funk Forty Niner Aug 02 '13 at 04:10
  • Oh, you want Ajax for this, not the same thing. You want to display an error or success message set inside a `
    `, not the same animal at all here.
    – Funk Forty Niner Aug 02 '13 at 04:11
  • oh thanks, thats what i mean, sorry for all the hassle, now if you dont mind me asking, how does that work? – NetInfo Aug 02 '13 at 04:12
  • @jprofitt I think you may be right about the Ajax call. A whole different ball game for the OP, if it is. – Funk Forty Niner Aug 02 '13 at 04:13
  • @NetInfo [**Click here for an Ajax Tutorial**](http://www.phptutorialforbeginners.com/2013/01/jquery-ajax-tutorial-and-example-of.html) <<< that will explain it a lot better than I can. – Funk Forty Niner Aug 02 '13 at 04:14
  • thanks, i guess im gonna have to take a different route, in this case ajax. – NetInfo Aug 02 '13 at 04:16
  • @NetInfo You're welcome. You can just Google "Ajax forms php", and you'll get a whole slew of information, demos, etc. – Funk Forty Niner Aug 02 '13 at 04:18

3 Answers3

2

Replace

header();

With

require_once();

eg.

require_once($thankyou_page);

What this will do is get the contents of the file and read from it. You won't need to redirect if that's all you need to do.

Izodn
  • 152
  • 8
  • Thanks for your prompt reply, maybe im not explaining well, i just want to get rid of the thankyou and error pages, instead of "header( "Location: $error_page" );" in human language: send "error sending email" string to index.html into textbox lets say. – NetInfo Aug 02 '13 at 04:05
1

If i correctly understood your question you just need to pass the state of success or failure to your pages. And since your pages has an extension of .html it cannot run the php code inside it. so the good solution will be to rename the file extension to .php if you want to check the state inside your thankyou or error page.

EG

You can send the status from your header

<?php
 $thankyou_page = 'thankyou_page.php'; //notice the extension
 header("location:$thankyou_page?status=1"); //send status code in your header

And in your thankyou_page.php

<?php
 if( isset($_GET['status']) && $_GET['status'] == 1){
    //success
    //do whatever you want
 }

You can't run PHP in .html files because the server does not recognize that as a valid PHP extension unless you tell it to. To do this you need to create a .htaccess file in your root web directory and add this line to it:

AddType application/x-httpd-php .htm .html

This will tell Apache to process files with a .htm or .html file extension as PHP files.

Source: How do I add PHP code/file to HTML(.html) files?

WARNING: Thanks to @Fred

A mention about AddType application/x-httpd-php .htm .html is that it will change the functionality for "all" .htm/.html files. So not "a" file, but "all" files .htm/.html

Community
  • 1
  • 1
Konsole
  • 3,389
  • 3
  • 24
  • 38
  • 1
    A mention about `AddType application/x-httpd-php .htm .html` is that it will change the functionality for **"all"** .htm/.html files. So not "a" file, but "all" files `.htm/.html` (wink) – Funk Forty Niner Aug 02 '13 at 04:33
  • Thank you @Fred. That is why i add this line `the good solution will be to rename the file extension to .php`. I will edit and add a warning too :) – Konsole Aug 02 '13 at 04:36
  • You're welcome. Some may not want all their .htm/.html files to run as PHP and that's why I mentioned that. By far, the best way/option is to ultimately use .php extension. Cheers – Funk Forty Niner Aug 02 '13 at 04:37
0

As Izodn said.. what you probably want to do is include the html of the error or success page (error.html, thankyou.html).

However, as I understand it.. you have a h1 element in those pages where you'd like a specific error or success message to be displayed.

So, in the contact form handler code you posted above.. you'll need a variable that will store the message you'd like to show in your error/thankyou page.

Something like:

....
$message = "Thank You"; // default message will be "Thank You" if no errors

// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)) {
   $message = "Error: You must enter an e-mail address and comment!";
   require_once($error_page);
   exit;
}

// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
   $message = "Error: Injection attempt discovered!";
   require_once($error_page);
   exit;
}

... send email

require_once($thankyou_page);

The exits are there so that after the error page is displayed, no more code will run (otherwise it would continue on to send the email and display the thankyou page.

So, then change your thankyou.html and error.html pages to thankyou.php and error.php so we can use PHP to print out the $message;

In those pages you'd put something like:

<h1><?php echo $message; ?></h1>

Edit: Just saw your response to Izodon.

So, you can't just 'send a string' to an html page (edit: that's not entirely true, you could use javascript to grab the result and maniuplate the DOM to display the message, but that's probably not ideal for this scenario). The html page containing the contact form is static (doesn't change), and when you submit the form you are being taken to the php page containing your form processing code (what you posted). Now, once you're at that php page, there's two possibilities: either have the php output some html, or redirect the user to another page.

If you want to make it 'look' like you haven't left the page (hit submit and nothing changes except the error message showing up), you would do everything all in one php file. You can have the form submit to itself with $_SERVER['PHP_SELF'], and have code to detect wither the form has been submitted or not with $_POST. If the form has been submitted you run the processing code which might set a $message variable that has an error or success message.. Then you have your html code after that with a place where you echo your $message variable with php.

AnthonyRyan
  • 153
  • 1
  • 7