Forum Moderators: coopster

Message Too Old, No Replies

PHP & a hidden HTML form field

Pass an error message from PHP to hidden HTML field

         

t1mmy

3:12 pm on Aug 19, 2005 (gmt 0)

10+ Year Member



I literally just started writing PHP code yesterday, so I'm an über noob. However, I have been learning slowly about PHP for the past couple of weeks, but not applying any of the knowledge until now. I've searched this site and haven't been able to find any help from a previous thread and am hoping that someone can help me.

In the HTML file I have a form field with two required fields and an unrequired one. I have a PHP script which mails the website owner the information that a user has submitted.

I would like a hidden field to display any errors or display a thank you message when the forms have been completed correctly. Right now, I have the Thank You part taking the user to a separate page.

I'm not 100% sure on how to pass a variable to an HTML page, and I feel terrible that I'm posting such an easy question. Since I'm on the topic, I've been reading the tutorials on HotScripts.com regarding Mailing List information. Anyone want to tell me his or her favorite resources? I also use php.net for a reference already.

HTML Information
=============
<form name="contact" method="post" action="includes/contact_send.php">
<input type="hidden" name="hiddenField" value="<?php $_POST['hiddenField'];?>"> <!--Error Message Area-->
<INPUT type="text" name="name" maxlength="50">
<INPUT type="text" name="email" maxlength="100">
<INPUT type="text" name="org" maxlength="50">

PHP
=============
<?
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$org = $_REQUEST['org'];

$hiddenField = 'You must fill out the Name & Email section';

if (!isset($_REQUEST['name'])) {
header("Location: contact.asp");
}
elseif (empty($email) ¦¦ empty($name)) {
$hiddenField = $_POST;
}

else {
mail("dude@thewebsite.com", "Contact Form Submission", "Name: $name\nEmail: $email\nOrganization: $org");
header( "thankyou.asp" );
}
?>

jatar_k

9:03 pm on Aug 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I have a few threads here from our PHP Library [webmasterworld.com]

Basics of Submitting and Emailing Forms with PHP [webmasterworld.com]

Learning PHP - Books, Tutorials and Online Resources [webmasterworld.com]

if that baiscs thread doesn't really help then I, or any else, will be happy to walk through it

t1mmy

9:42 pm on Aug 19, 2005 (gmt 0)

10+ Year Member



Thank you for your help, I greatly appreciate it. I'm really happy that I stumbled across this site a couple of months ago.

mcibor

10:21 pm on Aug 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So first of all - no hidden field for you! - As name suggest it will be hidden, so user won't see it
Second - always process php first, then html (header function won't work otherwise)
Third - don't use _REQUEST variable (use _POST or _GET)
Fourth you don't want to write to those super globals (nor POST nor GET)
Fifth and last for now - try to use English only :) (I know German, but many don't)
This is a solution to your problem:
<?php
if($_POST["action"] == "Submit") {
$name = $_POST['name'];
$email = $_POST['email'];
$org = $_POST['org'];
if (empty(trim(($name))) {
$text = '<b>You must fill in the name section</b>';}
elseif (empty(trim($email))) {
$text = '<b>You must fill in the email section</b>';}
else {

mcibor

10:22 pm on Aug 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mail("dude@thewebsite.com", "Contact Form Submission", "Name: $name\nEmail: $email\nOrganization: $org");
$text = '<b>THANK YOU!</b>';}}//else the form wasn't submitted. If you don't want to see the form again place here the else {?> html.....<? }?>
?>
<html....
<body><?php if($hiddenField) echo $text;?>
<form name="contact" method="post" action="includes/contact_send.php">
<INPUT type="text" name="name" maxlength="50">
<INPUT type="text" name="email" maxlength="100">
<INPUT type="text" name="org" maxlength="50">
<INPUT type="submit" name="action" value="Submit">
</html>

you should do some verification if email is valid, etc but it is a more complicated matter.
Best regards
Michal Cibor