newbie question regarding simple form submission

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fransgaard
Forum Newbie
Posts: 5
Joined: Sun Jun 22, 2003 12:35 pm

newbie question regarding simple form submission

Post by fransgaard »

I recently took up learning php and I have started with a simple form.

I have made a form that sends an email, and a form that checks whether required fields have been filled out. Both works but when combinded it doesn't. I was hoping somebody here could tell me what I am doing wrong here.

this one checks for required fields
http://www.fransgaard.net/php/form01.php

this one sends email
http://www.fransgaard.net/php/form02.php

This one is both combined, but doesn't work
http://www.fransgaard.net/php/form03.php

This is the code for the last form:

Code: Select all

<?php 
if (! isset ($your_message))
	$my_message = "";	
elseif ($your_message <> "")
	if  ($tlf <> "" || $email <> "")
		$my_message = "Thanks for your email";
		
		// bit that is sent off to my mail
		$to = "robert@fransgaard.com";
		$msg .= "$your_message\n\n";
		mail($to, "To me", $msg, "Fra $name\n Reply-To: $email \n");
		//
		
	else
		$my_message = "I need your phone or email";
elseif ($your_message == "")
	$my_message ="Don't you want to say something ?";	
?>

<html>
<head>
...
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

I don't see why you want to combine them, but try instead making a page with the form, and send the form to another page that is the script. On top of the mail script you can use this:

Code: Select all

if( $_POST&#1111;'name']=="" or $_POST&#1111;'phone']=="" or $_POST&#1111;'email']=="" or $_POST&#1111;'message']=="")
&#123;
        echo "Fill in the required fields";
        exit;
&#125;
Or you can put a header in there instead.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

javascript

Post by phpScott »

try adding javascript yo your exitment of learning php.

You could javascript to check to see if the required fields have been filled out and would save a trip to the server.

phpScott
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Big bonus of server-side validation over client-side validation is it doesn't rely on JS being available. Always good to add some server-side checking in case your client-side stuff is circumvented (never trust users).

Mac
fransgaard
Forum Newbie
Posts: 5
Joined: Sun Jun 22, 2003 12:35 pm

Post by fransgaard »

wow, thanks for all the quick answers :-)

Zeceer, I want to keep the same file after form has been submitted, but your code looks very much like flash actionscript code so it was suddenly a whole lot easier to figure out what to do, so I rewrote the codes to this http://www.fransgaard.net/php/form05.php which works:

Code: Select all

<?php
	if ( isset ($your_message))
	&#123;
		if( $_POST&#1111;'name']=="" or $_POST&#1111;'email']=="" or $_POST&#1111;'your_message']=="") 
		&#123; 
			$my_message = "OY! fill in the fields";
			
		&#125; else &#123;
			$to = "robert@fransgaard.com";
			$msg .= "$your_message\n\n";
			mail($to, "To me", $msg, "Fra $name\n Reply-To: $email \n");
		&#125;
	&#125;	
?>
phpScott, I'll take one programming language at the time since I am rather new at this :-)
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

IMHO: php and javascript are crosses between programming languages and something else. javascript is much much less of a programming language, php is much closer to a programming language.

a better way to check variables being set is to use the php function isset()

since you're using a form, i suggest passing a hidden variable. use that to determine what to do. the following i didn't test, but since i've already gone through this stuff with what i'm doing and i'm pretty confident that it'll work. don't just take it, read the link i gave you and then go through this line by line, if you don't understand everything, then post questions here, and then read the answers, otherwise you won't learn it and you will make the mistakes again. i just can't think of any other way to set you on a good track to complete it aside from giving you most of what you need layed out for you

Code: Select all

<?php

/*get variables*/

if(isset($stage)){
  if(isset($name)&&isset($email)&&isset($tlf)&&isset($your_message)){
    $date-time();
    $message="on $date, $name contactable via $phone and $email sent message...

$your_message

cc: $name"
      $other="cc: $email"
      mail('your_e-mail_address', 'user comments', $message, $other);
    }else{
      /* spit out your error messages */
    }
}else{
  ?><html>
<head>
<title>PHP form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>

	<strong></strong>
	<form action="<?php echo $_SERVER[PHP_SELF]; ?>">
	<input type="hidden" name="stage" value="process">
	Your name<br>
	<input name="name" type="text" size="30" maxlength="50" value=""><br><br>
	Your email<br>
	<input name="email" type="text" size="30" maxlength="50" value=""><br><br>
	Your phone<br>
	<input name="tlf" type="text" size="30" maxlength="20" value=""><br><br>
	 
	Message<br>
	<textarea name="your_message" rows="8" cols="23"></textarea><br><br>
	<input type="submit" value="Send">
	</form>
	

</body>
</html>
  <?php
}?>
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

javascript

Post by phpScott »

I never just rely on javascript and revalidate the info server side but it does mean less trips to the server if js is used to do some basic form validation before the request is sent.

php's isset() and isempty functions are really handy server side tools.

phpScott
fransgaard
Forum Newbie
Posts: 5
Joined: Sun Jun 22, 2003 12:35 pm

Post by fransgaard »

m3rajk, thanks I'll have a look through it, but as I said I am completely new to PHP and what I have done now I understand, I prefer to only use things I know how works.

ALso as I said it has simularities to action script which I know so it is a good place to start.

What I don't understand about yours is this bit:
<?php echo $_SERVER[PHP_SELF]; ?>

Also I don't understand why:
!isset($name)

is better than
$_POST['name']==""

They seem to do the same to me, but yes I'll have a read through your link, thanks :-)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

fransgaard wrote:What I don't understand about yours is this bit:

Code: Select all

<?php echo $_SERVER[PHP_SELF]; ?>
That's to set the action attribute of the form to the current page (i.e. make it a self submitting form).
fransgaard wrote:Also I don't understand why:

Code: Select all

!isset($name)
is better than

Code: Select all

$_POST['name']==""
They seem to do the same to me, but yes I'll have a read through your link, thanks :-)
If the $_POST['name'] variable is not set then the second method will cause an undefined index notice to appear (if you have error reporting at it's highest level) because you are trying to compare an empty string against a variable which does not exist. However, using isset() allows you to check that the variable exists before attempting to do anything with it. Using empty() instead of !isset() would also allow you to check that the variable was not null, equal to an empty string or 0 (zero).

Mac
fransgaard
Forum Newbie
Posts: 5
Joined: Sun Jun 22, 2003 12:35 pm

Post by fransgaard »

So isset() does not check whether there is a value to the variable but whether there is a variable at all, is that correctly understood?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

fransgaard wrote:So isset() does not check whether there is a value to the variable but whether there is a variable at all, is that correctly understood?
Yes.

Mac
fransgaard
Forum Newbie
Posts: 5
Joined: Sun Jun 22, 2003 12:35 pm

Post by fransgaard »

ok thanks. in that case I don't think it matters much since I know the variables are there since I built the form, but that was nice to know for future reference
Post Reply