Forum Moderators: coopster

Message Too Old, No Replies

PHP Contact FOrm problem

PHP Contact form

         

LeeAllen

2:19 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Hello all,

Hope your all well.

Hoping for some help. I have recently created a PHP contact form to be used on my website. Everything works fine with the email being sent correctly and a nice thank you message notifiying the user the message has been sent.

However, I have come to use the same contact form on a different project and low and behold it doesn’t work.

I'm not a php bod but I thought it would be straight forward as everything is the same on the servers I’m using (hosts etc) so why it doesn’t work I don’t know.

The problem lies at when the user adds contact info (name, email, message) and presses send. Rather than displaying the thank you message, a fields are missing message is displayed instead (which should only happen if one of the said fields have not been completed).

I would be very grateful if someone could help me or point me in the correct direction. I’m confused and frustrated!

Kind regards,

Lee

[edited by: LeeAllen at 3:03 pm (utc) on Feb. 16, 2004]

Knowles

2:27 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Welcome to WebmasterWorld LeeAllen!

With out looking to deep into what you are doing and with out seening any of the code I would guess that your globals are turned off on the server which its not working on. Do you ever use $_GET or $_POST on the script? Even though this is the same host, doesnt mean its the same server or that they are configed the same. The first server may be an old server they "forgot" about.

PS. you may want remove the links as they are not allowed here.

LeeAllen

3:05 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Hi Knowles,

Cheers for the post. Ive removed the links, soz didnt realise.

I'm not at hand to check the code i used, but will check when i get back. I do think it includes what you say.

Would i have to ask my host to switch globals on then? How would i phrase that?

Many thanks,

Lee

Knowles

3:32 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Just like you just did can you turn on globals please? You would probably be better off to do without globals as they are turned off by default now because of a possible security flaw with them. To work with out them you will want to use what I showed. Something like this

method=post:
$name = $_POST['name'];

method=get:
$name = $_POST['name'];

This will then give your $name the value you are looking for. If I remember correctly they will work even if globals are turned on, but dont hold me to that its been awhile since they where turned on for me.

willybfriendly

4:19 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, $_POST will work always.

Another approach might be to use extract($_POST) at the top of the script. Depends on how the script is set up. If it is using a lot of functions that reference the $_POST variables it might be problematic, but if it is simple script that only uses native PHP functions it will probably work.

WBF

LeeAllen

4:47 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Hi, Thanks for you help.

Can you print code in these forums. It starting to get a little technical and im afraid im losing beginning to lose track.

Lee

jatar_k

5:36 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sure you can, just take a look at the PHP Charter [webmasterworld.com] and WebmasterWorld Member Guidelines on Posting Code [webmasterworld.com]

mainly try to stick to the relevant code and don't just paste the whole script.

LeeAllen

7:53 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Ok here goes:

PHP Code

<?php

if (($Name == "") ¦¦ ($Email == "") ¦¦ ($Comments == ""))
{
echo "<form name=form method=post action=contact_thanks.php>";
echo "<h1>Field Missing</h1>";
echo "<p class=bodymd>All three fields of this form are required,</p>";
echo "<p class=bodymd>Fill in the ones you missed, they are listed below.</p>";
}
if ($Name == "")
{
echo "<p class=bodymd>Your Name<br><input type=text name=Name></p>";
}
else
{
echo "<input type=hidden name=Name value=$Name>";
}
if ($Email == "")
{
echo "<p class=bodymd>Your Email<br><input type=text name=Email></p>";
}
else
{
echo "<input type=hidden name=Email value=$Email>";
}
if ($Comments == "")
{
echo "<p class=bodymd>Comments or Questions<br><textarea name=Comments rows=5 cols=40></textarea></p>";
}
else
{
echo "<input type=hidden name=Comments value=$Comments>";
}

if (($Name == "") ¦¦ ($Email == "") ¦¦ ($Comments == ""))
{
echo "<input type=submit name=Submit value=Send>";
echo "<input type=reset name=Reset value=Clear Form>";
echo "</form>";
}
else
{
$message = "Name: $Name\nEmail: $Email\nComments: $Comments\n";
$extra = "From: $Name\r\nReply-To: $Email\r\n";
mail ("me@example.com", "Website Email", $message, $extra);
echo "<h1>Thanks for your enquiry, $Name.</h1>";
echo "<p>A response will be sent to $Email as soon as possible.</p>";
}
?>

and the corresponding html

<form name="form" method="post" action="contact_thanks.php">
<p class="bodymd">Your Name<br>
<input type="text" name="Name">
</p>
<p class="bodymd">Your Email<br>
<input type="text" name="Email">
</p>
<p class="bodymd">Comments or Questions<br>
<textarea name="Comments" rows="5" cols="40"></textarea>
</p>
<p class="bodymd">
<input type="submit" name="Submit" value="Send">
<input type="reset" name="Reset" value="Clear Form">
</p>
</form>

[edited by: jatar_k at 8:19 pm (utc) on Feb. 16, 2004]
[edit reason] generalized email [/edit]

Knowles

8:02 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



<?php

if (($Name == "") ¦¦ ($Email == "") ¦¦ ($Comments == ""))


I would say you dont have globals turned on on this server. You can make a page with phpinfo(); to see it is listed on tehre. If its not and the host wont turn them on just add $Name = $_POST['Name']; for each one.... post may need to be get depending on the method used in the form.

willybfriendly

8:22 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add:

extract($_POST);

on the first line (i.e. after <? ) and see what happens...

WBF

LeeAllen

8:56 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Well guys, we can put this to bed. I added the additional code at the top and everything is now working! Being honest I dont know what that meant but it worked!

Thank you all very much!