PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 2nd, 2005, 10:21 AM
Rahh Rahh is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Louisville
Posts: 5 Rahh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 4 sec
Reputation Power: 0
Send a message via AIM to Rahh
PHP Form Validation

I'm new to the whole php scene and need some help getting a jump-start if anyone is willing. I did some searches on this topic and read through some posts but they just got me a little lost. I need to implement some php that will process a form with validation server side. If anyone can show me how I would wrap the php around this small form I would be most appreciative. I also was wondering if there was a way to only have some fields validated for instance, the E-Mail field is not required but the others are?





<html>
<head>
<title>Php Form Validation</title>
</head>


<body>
<table width="25%" height="222" border="1">
<tr>
<td><form name="form1" method="post" action="">
<p>Name:
<input name="name" type="text" id="name">
</p>
<p>E-Mail:
<input name="email" type="text" id="email">
</p>
<p>Comments:
<textarea name="comments" id="comments"></textarea>
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form></td>
</tr>
</table>
</body>
</html>




Any examples of how I would implement php with "selective" validation to this form? Any help is appreciated!



Thanks,



-Rahh

Reply With Quote
  #2  
Old March 2nd, 2005, 10:28 AM
Rahh Rahh is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Louisville
Posts: 5 Rahh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 4 sec
Reputation Power: 0
Send a message via AIM to Rahh
Well got it working somewhat. The page that shows the result though I'm wondering if I can change to my own page. How would I make something like this go to my own page with the results?

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST'){
$me = $_SERVER['PHP_SELF'];
?>
<form name="form1" method="post" action="<?=$me?>">
<table border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td align="right" valign="middle">Name:</td>
<td><input name="Name" type="text" size="40">
</td>
</tr>
<tr>
<td align="right" valign="middle">Size:</td>
<td><select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>
</td>
</tr>
<tr>
<td align="right" valign="middle">Options:</td>
<td><select name="options[]" size="4" multiple id="options[]">
<option value="magnifier">Magnifier
<option value="reducer">Reducer
<option value="timer">Timer
<option value="oscillator">Oscillator</option>
</select>
</td>
</tr>
<tr>
<td align="right" valign="middle">Color:</td>
<td><input type="radio" name="color" checked value="red">
Red<br>
<input type="radio" name="color" value="green">
Green<br>
<input type="radio" name="color" value="blue">
Blue</td>
</tr>
<tr>
<td align="right" valign="middle">Accessories:</td>
<td><input name="extension" type="checkbox"
id="extension" value="extension">Extension<br>
<input name="wallmount" type="checkbox"
id="wallmount" value="wallmount">Wall Mount<br>
<input name="deskmount" type="checkbox"
id="deskmount" value="deskmount">Desk Mount</td>
</tr>
<tr>
<td align="right" valign="top">Comments:</td>
<td><textarea name="MsgBody" cols="40" rows="6"></textarea>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit"
value="Send">
</td>
</tr>
</table>
</form>
<?php
} else {
error_reporting(0);
// initialize a array to
//hold any errors encountered
$errors = array();
// test to see if the form was actually
// posted from the form
$page = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
if (!ereg($page, $_SERVER['HTTP_REFERER']))
$errors[] = "Invalid referer";

// check to see if a name was entered
if (!$_POST['Name'])
$errors[] = "Name is required";
// if there are any errors, display them
if (count($errors)>0) {
foreach($errors as $err)
echo "$err<br>\n";
echo "<br>Please use your browser's Back button to fix.";
} else {
// no errors, so we build the message
switch($_POST['color']){
case 'red':
$recipient = 'redperson@myaddress.com';
break;
case 'green':
$recipient = 'greenperson@myaddress.com';
break;
case 'blue':
$recipient = 'blueperson@myaddress.com';
break;
default:
$recipient = 'me@myaddress.com';
}
$subject = "Widget On Line Order";
$from = stripslashes($_POST['Name']);
$msg = "Message sent by $from\n";
$msg.="\nSize: ".$_POST['size'];
$options=$_POST['options'];
$msg.="\nOptions:";
if ($options)
for ($i=0;$i<count($options);$i++)
$msg.= "\n- $options[$i]";
else
$msg.="\n- None";
$msg.="\nColor: ".$_POST['color'];
$extension=($_POST['extension'])?"Extension: Yes":"Extension: No";
$wallmount=($_POST['wallmount'])?"Wallmount: Yes":"Wallmount: No";
$deskmount=($_POST['deskmount'])?"Deskmount: Yes":"Deskmount: No";
$msg.="\n$extension\n$wallmount\n$deskmount";
$msg.="\n".stripslashes($_POST['MsgBody'])."\n";
if (mail($recipient,$subject,$msg)){
echo "<p>Thanks for your order!</p>";
echo nl2br($msg);
} else
echo "An unknown error occurred.";
}
}
?>
</body>
</html>

Reply With Quote
  #3  
Old March 2nd, 2005, 01:31 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
um, how do you mean "go on to my page"?

I recommend modifying the HTML portion of your script to comply with the template of your page.

Reply With Quote
  #4  
Old March 2nd, 2005, 01:36 PM
Rahh Rahh is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Louisville
Posts: 5 Rahh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 4 sec
Reputation Power: 0
Send a message via AIM to Rahh
Instead of a blank page with just the results and the user having to either close the browser or type in the URL to get back instead have a page with my template displaying the results. Im not sure how its jumping to the blank page displaying the results through php. If you tested the above code you would see what I mean.


edit: basically on the page after you hit submit I want a way to get back to the form or something w/o having to hit the back button in the browser which results in all the fields still filled in.

Reply With Quote
  #5  
Old March 3rd, 2005, 12:58 PM
Rahh Rahh is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Louisville
Posts: 5 Rahh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 4 sec
Reputation Power: 0
Send a message via AIM to Rahh
Quote:
Originally Posted by Rahh
Instead of a blank page with just the results and the user having to either close the browser or type in the URL to get back instead have a page with my template displaying the results. Im not sure how its jumping to the blank page displaying the results through php. If you tested the above code you would see what I mean.


edit: basically on the page after you hit submit I want a way to get back to the form or something w/o having to hit the back button in the browser which results in all the fields still filled in.


Would something with the header be able to do this?

Reply With Quote
  #6  
Old March 3rd, 2005, 01:30 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
You're right, I haven't tested your code.
I guess I'm offering blind advice.

But based on your description, you could use header("Location: form.php");

Doing this, you won't see the results page, and likely won't know for sure that your information was processed.
Maybe even just header("Location: success.php");
And include a link back to the form from the success page.

Reply With Quote
  #7  
Old March 3rd, 2005, 02:27 PM
Rahh Rahh is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Louisville
Posts: 5 Rahh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 4 sec
Reputation Power: 0
Send a message via AIM to Rahh
Thanks for the help Mad.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > PHP Form Validation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway