|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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> |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
Quote:
Would something with the header be able to do this? |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
Thanks for the help Mad.
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > PHP Form Validation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|