form

This is a discussion on "form" within the PHP Forum section. This forum, and the thread "form are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Apr 2nd, 2007, 20:12
Reputable Member
Join Date: Sep 2006
Location: Rothwell
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chubbs Send a message via Skype™ to chubbs
form

Hi Guys!

I have this phpfile which i use for a contact form. How can i redirect the visitor after submiting the form?

Here is the php:

PHP: Select all

<?php
if(isset($_POST['submit'])) {

    
$to "me.me@vinotrade.co.uk"
    
$subject "Contact Form";
    
$name_field $_POST['name'];
    
$email_field $_POST['email'];
    
$message $_POST['message'];
    
$option $_POST['radio'];
    
$dropdown $_POST['drop_down'];

    foreach(
$_POST['check'] as $value) {
        
$check_msg .= "Checked: $value\n";
    }
    
    
$body "From: $name_field\n E-Mail: $email_field\n $check_msg Option: $option\n Drop-Down: $dropdown\n Message:\n $message\n";

    echo 
"Data has been submitted to $to!";
    
mail($to$subject$body);
    
} else {
    echo 
"blarg!";
}
?>
i dont know anything about php by the way...... Thank you for helping me

Last edited by karinne; Apr 3rd, 2007 at 14:00. Reason: Please use [php]...[/php] tags when displaying PHP code!
Reply With Quote

  #2 (permalink)  
Old Apr 3rd, 2007, 13:54
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form

Maybe somebody has an instant answer based on your post. I don't know of one.

This is a very simple problem but unfortunately, afaik, needs to be done in light of the entire framework of the form and form handler and the pages and file names involved. (The code you have given is the core of the form handler, btw.) You may not be able to actually redirect the user, which can be done only prior to any other browser output.

My quick and dirty suggestion is to insert this line immediatly after "mail($to, $subject, $body);"
Code: Select all
 
echo '<br /><a href="./whereIwantyoutogo.php">Click here to continue</a>';
Obviously you will change the href to your target page.
Reply With Quote
  #3 (permalink)  
Old Apr 3rd, 2007, 19:07
Reputable Member
Join Date: Sep 2006
Location: Rothwell
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chubbs Send a message via Skype™ to chubbs
Re: form

Thank you Mason i will try it def.
Reply With Quote
  #4 (permalink)  
Old Apr 4th, 2007, 02:24
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form

PHP: Select all

header("Location: whereever.html"); 

Reply With Quote
  #5 (permalink)  
Old Apr 4th, 2007, 05:46
Reputable Member
Join Date: Sep 2006
Location: Rothwell
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chubbs Send a message via Skype™ to chubbs
Re: form

thanx Ryan! and where should i place it?
Reply With Quote
  #6 (permalink)  
Old Apr 4th, 2007, 06:24
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form

You have to place it before any content appears. For instance:

PHP: Select all

<?php
echo "Welcome to my site!";
header("Location: whatever.php");
?>
The above won't work. The header function must come before any HTML or content.
Reply With Quote
  #7 (permalink)  
Old Apr 5th, 2007, 16:56
Junior Member
Join Date: Jan 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form

As above, if you're sending headers you need to do so before anything is echoed to the screen as that would automatically send the headers. Therefore either don't have any echoes in sections of your code that may then lead to a redirect, or use output buffering to make sure its not actually sent. Add exit; directly under your header call.
Reply With Quote
  #8 (permalink)  
Old Apr 5th, 2007, 17:19
Reputable Member
Join Date: May 2006
Location: Northampton, UK
Posts: 399
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form

to make things simple i use a session trick

1) submit the form for the php processing file.
2) process the form
3) set sessions depending upon the outcome of the process
...... for example ::
Code: Select all
 if($pass != $pass2){
$_SESSION['passnomatch'] = "yes":
}
then once all that gumpf is done use the header to go to whichever page you want.

On that destination page, test whether any of the sessions are set, and echo out the corresponding erros / confirmations etc.

Just remember to kill the sessions when your done
Reply With Quote
  #9 (permalink)  
Old Apr 5th, 2007, 20:20
Reputable Member
Join Date: Sep 2006
Location: Rothwell
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chubbs Send a message via Skype™ to chubbs
Re: form

ohhhh....thanx guys! i will try it! i will come back if it doesnt work.
Reply With Quote
  #10 (permalink)  
Old Apr 21st, 2007, 14:02
Reputable Member
Join Date: Sep 2006
Location: Rothwell
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chubbs Send a message via Skype™ to chubbs
Re: form

i have Mason's version which is works ok, but i dont realy get the rest of the tips guys as i dont know nothing about php.
Reply With Quote
  #11 (permalink)  
Old Apr 21st, 2007, 15:59
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form

There are actually a lot of things you can do in PHP that are a little slicker, but there is something to be said in terms of user psychology for a "Your message has been delivered" message with a "click to continue" link. Especially for people who are not totally adept at using the internet, it gives a feeling of security and control.

Anyway, if you are interested in something fancier, you'll need to post a little more info for us: Where is the form located, how do they get to it, and where do you want them to go after the submission?

Actually, instead of redirecting the user back to his previous location, you might consider not making him leave the page in the first place. You can put the form as an include on the pages from which it is accessed and put the form handler right on the same page, as well. Instead of leaving the page, the form can be added to the page contents when a link is clicked, the success message can be added to the page when the form is submitted, and the user would never need to navigate out in the first place.

Something like this (off the top of my head as an idea, there could easily be a bug or two). I'll do this so that the form appears right on the page, but all you have to do is relocate the code block and move the final bracket around to make the other content disappear:
PHP: Select all

[The header and all the content that appears before the form goes here]
if (isset(
$_POST['submit'])) {
   if (
$_POST['submit']="submitted") {
      echo 
'<div class="form>';
      include (
"./form.php");
      echo 
'</div>'; [Note you could put the formatting in the include]
   } elseif (
$_POST['submit']="success") {
      include (
"./formhandler.php");
   }
} else {
[
Put whatever content you want to disappear hereTo make it reappear after submission just takes a little work.  To make all the original content stay putjust leave out the }else{]
}
[
Content to appear after the form goes here.] 
I've never done this, so it might take a bit of fine-tuning, but the code theory is fairly basic. Although it might look a tad convoluted in the file due to multiple nested 'if' clauses. You just have to put a different hidden inputs on the form and on the form handler setting the value of "submit". That is, you would transmit submit="submitted" when the form was sent. You would have a mini-form on the form handler page that does nothing but set submit="success" and the button, which could look like a link, would say "click here to return".

If this is confusing, here's what I am thinking:
1) User gets a content page containing a link "contact us".
2) If the user clicks the link, the page changes so that the contact form appears. You have the option to display any, all, or none of the page content along with the form.
3) After the user has submits the form, he is still on the same page, only now there is a message "your data has been sent" or "something went wrong". Again, you have simple options. You can have the original page with the success message in it somewhere; you can have just the message and a link/button to click to redisplay the original page; or you can show a picture of Anna Nichole Smith's baby.

I think this will access the cache, at least for the graphics; maybe someone with some knowledge in the area will tell us. If so, it should be as fast as the connection and server. With a good connection and the host server behaving, the page should barely blink. It's a poor man's AJAX -- in fact it is superior to AJAX for us non-guru types in one respect, i.e. the security is easier.

Last edited by masonbarge; Apr 21st, 2007 at 16:13.
Reply With Quote
  #12 (permalink)  
Old Apr 21st, 2007, 16:24
Reputable Member
Join Date: Sep 2006
Location: Rothwell
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chubbs Send a message via Skype™ to chubbs
Re: form

Here is the link to the site which is still in developing. http://www.vinotrade.co.uk
Reply With Quote
  #13 (permalink)  
Old Apr 21st, 2007, 18:10
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form

Okay, I played with this and stuck it into a page I'm making. Here is what I came up with. It's just a skeleton template to change the display, with no transmission of the form data and no security. It can easily be improved (the code could use some touchup and the formatting needs work) but it works ok.

http://www.muggajo.com/SunriseCitrus/index.php#contact

Use the big button in the center of the page -- the link in the footer will just get you an email form.
PHP: Select all

<?php
if (isset($_POST['submit2'])) {
    [
transmission and security code goes here]
    
$_POST['go'] = "success";    
   echo 
'<div id="form"><span class="contactform">Your message has been
 sent to us.  Thank you for your interest.</div>'
;
} elseif ((isset(
$_POST['submit'])) && (!isset($_POST['submit2']))) {
      include (
"./form.php");
} else {
?>
<form id="contact" method="post" action="<?php echo $PHP_SELF?>">
<input type="submit" name="submit" class="cbutton" value="Contact Us" >
</form>
<?php }?>
form.php is
PHP: Select all

<div id="form">
<h3> Contact Form</h3>
<form id="contact" method="post" action="<?php echo $PHP_SELF?>">
<label for="firstname" class="contactform">First name</label>
    <input type="text" name="firstname"  size="8" maxlength="20"  />
    <br /><br />    
<input type="submit" name="submit2" class="contactform" value="Send Your Message" >
</form></div>
Reply With Quote
  #14 (permalink)  
Old Apr 21st, 2007, 18:16
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form

Quote:
Originally Posted by chubbs View Post
Here is the link to the site which is still in developing. http://www.vinotrade.co.uk
Wow, I love the page design! The navigation needs some adjustment for better vertical placement.

I actually think the template I posted will work great on this. I actually like the way it's set up just fine, but it would be nice to have a little message confirming that a message has been sent.

BTW you'll need a spam block on the message feature. And form validation -- this is like a sign "PLEASE HACK ME".

Last edited by masonbarge; Apr 21st, 2007 at 18:18.
Reply With Quote
  #15 (permalink)  
Old Apr 23rd, 2007, 12:28
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form

Bah, I should learn not to post anything in a forum for at least an hour after I wake up.
Reply With Quote
  #16 (permalink)  
Old Apr 24th, 2007, 07:27
Marc's Avatar
Moderator

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Age: 15
Posts: 1,639
Thanks: 0
Thanked 7 Times in 7 Posts
Send a message via MSN to Marc Send a message via Skype™ to Marc
Re: form

i use a little window that does 2 things. 1. Tells user that form has been submitted. 2. When they click ok it redirects them to the page where the form was.

Here it is here: (in head tags)
Code: Select all
<script type="text/javascript">
   alert("Thank You, Your Email Has Been Sent.")
   window.location.href = 'contact_us.php';
  </script>
Im sorry if i posted this too late!
Reply With Quote
  #17 (permalink)  
Old Apr 24th, 2007, 08:17
Reputable Member
Join Date: Sep 2006
Location: Rothwell
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chubbs Send a message via Skype™ to chubbs
Re: form

That sounds good.And how can i use it with the code i have now? Should i just place this code to the head of the contact page? I dont realy know javascript either to be honest..
Reply With Quote
  #18 (permalink)  
Old Apr 24th, 2007, 08:31
Marc's Avatar
Moderator

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Age: 15
Posts: 1,639
Thanks: 0
Thanked 7 Times in 7 Posts
Send a message via MSN to Marc Send a message via Skype™ to Marc
Re: form

If you mean the code i posted yes, put it into the head tags. You can change the message to whatever you want, as you can see just now the message is "Thank You, Your Email Has Been Sent". And the direct page just now is ''contact_us.php'', change this to the page that you want the user to be re-directed too.

I hope that helps.
Reply With Quote
  #19 (permalink)  
Old Apr 24th, 2007, 08:44
Marc's Avatar
Moderator

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Age: 15
Posts: 1,639
Thanks: 0
Thanked 7 Times in 7 Posts
Send a message via MSN to Marc Send a message via Skype™ to Marc
Re: form

no, the code dosnt go in the head of the contact page, it should go in the page that actually processes your data to an email address or somthing like that. Ive had a look, it should go in your mailer.php of your website
Reply With Quote
  #20 (permalink)  
Old Apr 24th, 2007, 08:51
Reputable Member
Join Date: Sep 2006
Location: Rothwell
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chubbs Send a message via Skype™ to chubbs
Re: form

I see. I will try it and come back later with the result. Thank you!
Reply With Quote
Reply

Tags
form

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hiding / Showing form fields based on previous form input John Alexander Hopper PHP Forum 1 Mar 10th, 2008 11:30
form variable within an iframe component of a form kissfreaque PHP Forum 3 Feb 29th, 2008 13:06
form variable within an iframe component of a form kissfreaque JavaScript Forum 5 Feb 29th, 2008 11:57
[SOLVED] PHP contact form redirect to same form Posie PHP Forum 14 Jan 29th, 2008 20:28
ASP form to check weather a form value is already in the database Andrew1986 Classic ASP 3 Oct 25th, 2007 08:23


All times are GMT. The time now is 21:31.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved