PDA

View Full Version : PHP Form Redirect problem



selenium
October 24th, 2009, 06:00 PM
Hi all

Like 'olicourse' i'm trying to get my form to redirect to a webpage (http://www.kirupa.com/forum/showthread.php?t=337218).

I want to redirect to here:
http://www.communityspiritacupuncture.co.uk/formcomplete

but no matter what I try it's not working. I really don't know what i'm doing wrong.

The form works fine otherwise (thanks v. much for the great tutorial :) )

code:
--

<?php

if(isset($_POST['submit'])){


$to = "info@communityspiritacupuncture.co.uk";
$subject = "Website Query";
$name_field = htmlspecialchars($_POST['name']);
$email_field = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);

foreach($_POST['check'] as $value) {

$check_msg .= "Checked: $value\n";

}

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $check_msg";

$header="From: $email_field\r\n";

mail($to, $subject, $body);

header("Location:formcomplete.phtml");

} else {

echo "blarg!";

}
?>



any help would be much appreciated.
Andy

LooInSpain
October 26th, 2009, 04:54 AM
like the reply for the other thread, you need to set the mail function to have the from field:



mail($to, $subject, $body, $header);

utsav
October 26th, 2009, 06:13 AM
Hi all

Like 'olicourse' i'm trying to get my form to redirect to a webpage (http://www.kirupa.com/forum/showthread.php?t=337218).

I want to redirect to here:
http://www.communityspiritacupuncture.co.uk/formcomplete

but no matter what I try it's not working. I really don't know what i'm doing wrong.

The form works fine otherwise (thanks v. much for the great tutorial :) )

code:
--

<?php

if(isset($_POST['submit'])){


$to = "info@communityspiritacupuncture.co.uk";
$subject = "Website Query";
$name_field = htmlspecialchars($_POST['name']);
$email_field = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);

foreach($_POST['check'] as $value) {

$check_msg .= "Checked: $value\n";

}

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $check_msg";

$header="From: $email_field\r\n";

mail($to, $subject, $body);

header("Location:formcomplete.phtml");

} else {

echo "blarg!";

}
?>



any help would be much appreciated.
Andy




if(mail($to, $subject, $body))
{

echo "<script language=\"JavaScript\"> alert (\"You are already logged in. If it is not you, Please log out of this account.\");
window.location=\"formcomplete.phtml\";
</script>";
}
else
{
echo "Form couldnot be submitted,";
}
hope this helps.

simplistik
October 26th, 2009, 09:01 AM
if(mail($to, $subject, $body))
{

echo "<script language=\"JavaScript\"> alert (\"You are already logged in. If it is not you, Please log out of this account.\");
window.location=\"formcomplete.phtml\";
</script>";
}
else
{
echo "Form couldnot be submitted,";
}
hope this helps.
oh wow ... no ... that's a terrible solution.

selenium, what does the form do? is it submitting, or is it returning the "Can not modify header information" which is the most common error for people and redirects.

utsav
October 26th, 2009, 09:31 AM
well Simplistick i know using JS is not good but i thought it might help him for the time being. And yes with this i need to ask why is it considered bad practice? I really dont know the reason though i know it is bad.

simplistik
October 26th, 2009, 01:46 PM
well Simplistick i know using JS is not good but i thought it might help him for the time being. And yes with this i need to ask why is it considered bad practice? I really dont know the reason though i know it is bad.
Well it's a cheap hackish work around really. PHP already provides a way to natively redirect a page. Also, it's dependent on javascript being enabled on your visitors' browser. Which of course like 90% of all people have js enabled, but you still want to make it as functional across the board as you can, and the PHP method works 100% of the time. Oh and not to mention :lol:


<script language="JavaScript">

is outdated markup :P


<script type="text/javascript">

is the proper way

selenium
October 27th, 2009, 03:46 PM
Thanks for your replies looinspain, simplistik and ustav.

the form is sending information, but the browser just directs to the blank page www....co.uk/mailer.php (http://www....co.uk/mailer.php).

Even with the modification to the line looinspain suggested:

-----

<?php

if(isset($_POST['submit'])){


$to = "info@communityspiritacupuncture.co.uk";
$subject = "Website Query";
$name_field = htmlspecialchars($_POST['name']);
$email_field = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);

foreach($_POST['check'] as $value) {

$check_msg .= "Checked: $value\n";

}

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $check_msg";

$header="From: $email_field\r\n";

mail($to, $subject, $body, $header);

header("Location:formcomplete.phtml");

} else {

echo "blarg!";

---

:(

LooInSpain
October 30th, 2009, 06:00 AM
try this:


$redirecturl = "http://www.communityspiritacupuncture.co.uk/formcomplete.phtml";

header("Location: $redirecturl");

simplistik
October 30th, 2009, 08:38 AM
try this:


$redirecturl = "http://www.communityspiritacupuncture.co.uk/formcomplete.phtml";

header("Location: $redirecturl");

not any different from the way he had his header, also he already fixed the problem

selenium
November 1st, 2009, 01:07 PM
thanks all for your help, i ended up modifying oilcorse's code to suit my form. not sure why his worked and mine didn't, but anyway, sorted now..

thanks again :)

andy