Getting error 500, processing form

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
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Getting error 500, processing form

Post by mikeashfield »

Here my code:

Code: Select all

<?php
if($_POST)
{
$name = $_POST['fname'];
$same = $_POST['sname'];
$tel1 = $_POST['tel1']; 
$email = $_POST['email']; 
$valid_street1 = $_POST['street1']; 
$postcode = $_POST['postcode']; 

//FIRST NAME VALIDATION
if (eregi('^[A-Za-z]{3,20}$',$name))
{
$valid_fname=$fname;
}
else
{ 
$error_fname='Enter valid name.'; 
}

//SURNAME VALIDATION
if (eregi('^[A-Za-z]{3,20}$',$sname))
{
$valid_sname=$sname;
}
else
{ 
$error_sname='Enter valid name.'; 
}

// TELEPHONE VALIDATION
if (eregi('^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$', $tel1))
{
$valid_tel1=$tel1; 
}
else
{ 
$error_tel1='Enter valid telephone number.'; 
}

//E-MAIL VALIDATION
if (eregi('^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$',$email))
{
$valid_email=$email;
}
else
{ 
$error_email='Enter valid e-mail address. (For no e-mail enter nnn@nnn.nnn)'; 
}

//POSTCODE VALIDATION
if (eregi('^((([A-PR-UWYZ](\d([A-HJKSTUW]|\d)?|[A-HK-Y]\d([ABEHMNPRVWXY]|\d)?))\s*(\d[ABD-HJLNP-UW-Z]{2})?)|GIR\s*0AA)$',$postcode))
{
$valid_postcode=$postcode;
}
else
{ 
$error_postcode='Enter valid UK postcode.'; 
}

if((strlen($valid_fname)>0)&&(strlen($valid_sname)>0)
&&(strlen($valid_tel1)>0)&&(strlen($valid_email)>0)&&(strlen($valid_street1)>0)&&(strlen($valid_postcode)>0)
{
$link = mysql_connect('localhost', 'uname', 'pwd
');
mysql_select_db('computer', $link);
mysql_query("INSERT INTO clients (fname, sname, tel1, e-mail, street1, postcode)
VALUES ($valid_fname, $valid_sname, $valid_tel1, $valid_email, $valid_street1, $valid_postcode)");
header("Location: http://192.168.0.222:1991/dev/computer_repair_manage_clients/add_client_success.html");
}
else{
}
}
?>
<html>
  <head>
  	<script type="text/javascript" src="9lessons.js"></script>
		<link rel="stylesheet" type="text/css" href="http://192.168.0.222:1991/dev/computer_repair/global_style.css"/>
  </head>
  <body>

    <div id="topNav">
    <a href='http://192.168.0.222:1991/dev/computer_repair/'><strong>HOME   </strong></a>
    <a href='http://192.168.0.222:1991/dev/computer_repair/manage_repairs/'>Manage Repairs   </a>
    <a href='http://192.168.0.222:1991/dev/computer_repair/manage_clients/'>Manage Customers   </a>
    <a href='http://192.168.0.222:1991/dev/computer_repair/reports/'>Reports</a>
    </div>
    <div id="subNav">
    <a href='http://192.168.0.222:1991/dev/computer_repair/manage_clients/'><strong>HOME</strong></a>
    <a href='http://192.168.0.222:1991/dev/computer_repair/manage_repairs/add_client.php'>Add New Customer</a>
    <a href='http://192.168.0.222:1991/dev/computer_repair/manage_repairs/update_client.php'>Update Customer Details</a>
    <a href='http://192.168.0.222:1991/dev/computer_repair/manage_repairs/delete_client.php'>Delete a Customer</a>
    </div>
    <div id="content">
    <form autocomplete="off" enctype="multipart/form-data" method="post" action=""  name="form">
    <h3>Add New Customer</h3>
    <table border="0" bordercolor="#FFCC00" style="background-color:#FFFFFF" width="350" cellpadding="3" cellspacing="3">
	<tr>
		<td><label for="fname">First Name</label></td>
		<td><input type="text" name="fname" id="fname" placeholder="First Name" length="30" value="<?php echo $error_fname;?>"></td>
	</tr>
	<tr>
		<td><label for="sname">Surname</label></td>
		<td><input type="text" name="sname" id="sname" placeholder="Surname" length="30" value="<?php echo $error_sname;?>"></td>
	</tr>
	<tr>
		<td><label for="tel1">Telephone Number</label></td>
		<td><input type="text" name="tel1" id="tel1" placeholder="Telephone Number" length="30" value="<?php echo $error_tel1;?>"></td>
	</tr>
	<tr>
		<td><label for="e-mail">E-mail Address</label></td>
		<td><input type="text" name="email" id="email" placeholder="E-Mail Address" length="50" value="<?php echo $error_email;?>"></td>
	</tr>
	<tr>
		<td><label for="street1">Street 1</label></td>
		<td><input type="text" name="street1" id="street1" placeholder="Street Address" length="30"></td>
	</tr>
	<tr>
		<td><label for="postcode">Postcode</label></td>
		<td><input type="text" name="postcode" id="postcode" placeholder="Postcode" length="30" value="<?php echo $error_postcode;?>"></td>
	</tr>
</table>
<br>
    <input  type="submit" value="Submit" style=" background:#0060a1; color:#FFFFFF; font-size:14px; border:1px solid #0060a1"/>
    </form>
    </div>
  </body>
</html>
But I'm getting an Error 500 code "HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request." and the page wont load! Essentially I'm trying to copy this tutorial http://www.9lessons.info/2009/07/server ... gular.html across to my own project! Please could somebody tell me where the error is in my code? :(
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Getting error 500, processing form

Post by twinedev »

Code: Select all

if((strlen($valid_fname)>0)&&(strlen($valid_sname)>0)
&&(strlen($valid_tel1)>0)&&(strlen($valid_email)>0)&&(strlen($valid_street1)>0)&&(strlen($valid_postcode)>0)
is missing closing parenthesis (gotta love a good IDE like PhpEd that instantly highlighted it when I pasted in your code) Also, a line like this is good for development purposed to break up for easier reading, which would have made it more noticeable:

Code: Select all

if (strlen($valid_fname)>0 &&
    strlen($valid_sname)>0 &&
    strlen($valid_tel1)>0 &&
    strlen($valid_email)>0 &&
    strlen($valid_street1)>0 &&
    strlen($valid_postcode)>0   ) {
(Note I put extra spaces before the closing ) to make it more noticeable when scanning the code.

To clean up the code, I would suggest starting above validation with:

Code: Select all

$valid_fname = FALSE;
$valid_sname = FALSE;
$valid_tel1 = FALSE;
$valid_email = FALSE;
$valid_street1 = FALSE;
$valid_postcode = FALSE;
then in the if statement, you can simply do the following since your validations already to make sure these are set if good data was there (and keeps from generating notices for calling undefined variables in the if statement)

Code: Select all

if ($valid_fname && $valid_sname && $valid_tel1 && $valid_email && $valid_street1 && $valid_postcode) {
Also, for "future proofing" your code, look to replace the eregi's with preg_match since they are deprecated.

If you want, I have posted here a sample form that does PHP validation in the following thread (the third code chunk in my post). It allows you to post back to the same file, and handles validation and redisplaying the form with error messages and repopulate the form with what they already entered.

A note is that the code lists all the errors at the top of the form, however, since they are in an array, indexed with the same name as the inputs, you can easily display them with their inputs instead.

viewtopic.php?f=1&t=132752#p664993

-Greg
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Getting error 500, processing form

Post by mikeashfield »

Hi Greg, that's some great help you've given me there!

But, I get an error when I try your code (editing my way) the code I have is:

Code: Select all

<?php

    if (count($_POST)>0) { // The form was sumitted

        $aryErr = array(); // Contains form errors
        
        // BEGIN: Validate data....
            if (!isset($_POST['fname']) || !preg_match('[a-zA-Z]+', $_POST['fname']) {
                $aryErr['fname'] = 'Invalid name.';
            }
            if (!isset($_POST['sname']) || !preg_match('[a-zA-Z]+', $_POST['sname']) {
                $aryErr['sname'] = 'Invalid name.';
            }
            if (!isset($_POST['tel1']) || !preg_match('[0-9]+', $_POST['fname']) {
                $aryErr['tel1'] = 'Invalid telephone number.';
            }
            if (!isset($_POST['email']) || !preg_match('/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i',$_POST['email'])) {
                $aryErr['email'] = 'Invalid Email Address';
            }
            if (!isset($_POST['street 1'])) {
                $aryErr['street1'] = 'You cannot leave address 1 blank.';
            }   
            if (!isset($_POST['postcode']) || !preg_match('(((^[BEGLMNS][1-9]\d?) | (^W[2-9] ) | ( ^( A[BL] | B[ABDHLNRST] | C[ABFHMORTVW] | D[ADEGHLNTY] | E[HNX] | F[KY] | G[LUY] | H[ADGPRSUX] | I[GMPV] | JE | K[ATWY] | L[ADELNSU] | M[EKL] | N[EGNPRW] | O[LX] | P[AEHLOR] | R[GHM] | S[AEGKL-PRSTWY] | T[ADFNQRSW] | UB | W[ADFNRSV] | YO | ZE ) \d\d?) | (^W1[A-HJKSTUW0-9]) | (( (^WC[1-2]) | (^EC[1-4]) | (^SW1) ) [ABEHMNPRVWXY] ) ) (\s*)? ([0-9][ABD-HJLNP-UW-Z]{2})) | (^GIR\s?0AA)',$_POST['postcode'])) {
                $aryErr['postcode'] = 'Invalid Postcode';
            }
            else {}

        // END: Validate data....

        if (count($aryErr)==0) { // No errors on form

            $link = mysql_connect('localhost', '', '');
			mysql_select_db('computer', $link);
			mysql_query("INSERT INTO clients (fname, sname, tel1, e-mail, street1, postcode)
			VALUES ('$_POST[fname]','$_POST[sname]','$_POST[tel1]','$_POST[email]','$_POST[street1]','$_POST[postcode]')";
			header("Location: http://192.168.0.222:1991/dev/computer_repair_manage_clients/add_client_success.html");
            
        } // END: if (No errors on form)
            
    } 
    else { // The form was not posted (ie first vist)

        // Optionally, you can load up values from a database, (ie, editing existing record)
        // And also set default values (ie. $_POST['State'] = 'Ohio'; )
        
    } // END-ELSE: if(Data posted)

    // These just makes less typing later and the code easier to read
    function echoHSC($strText) {
        echo htmlspecialchars($_POST[$strText],ENT_QUOTES);
    }
    function echoPost($strIndex) {
        if (isset($_POST[$strIndex])) {
            echoHSC($_POST[$strIndex]);
        }
    }

?><html>
<head>
    <title>Form Template</title>
</head>
<body>
    <h1>Register a new customer</h1>
    <?php if (isset($aryErr)): ?>
        <?php if (count($aryErr)==0): ?>
        
            <h3>Thanks for registering a new customer</h3>

        <?php else: // There were errors ?>
            
            <div id="errors">
                <p>We found the following error(s):</p>
                <ul>
                    <?php foreach($aryErr as $strErr): ?>
                        <li><?php echoHSC($strErr); ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>

        <?php endif; unset($aryErr); // Unset it for next IF block  ?>
    <?php endif; // end of was $aryErr set, (ie. form was submitted) ?>

    <?php if (!isset($aryErr)): ?>
    
                <form method="post" action="">
            <fieldset>
                <ul>
                    <li>
                        <label for="fname">First Name*</label>
                        <input type="text" name="fname" id="fname" value="<?php echoPost('Name'); ?>" />
                    </li>
                    <li>
                        <label for="sname">Surname*</label>
                        <input type="text" name="sname" id="sname" value="<?php echoPost('sname'); ?>" />
                    </li>
                    <li>
                        <label for="tel1">Telephone Number*</label>
                        <input type="text" name="tel1" id="tel1" value="<?php echoPost('tel1'); ?>" />
                    </li>
                    <li>
                        <label for="email">E-Mail Address*</label>
                        <input type="text" name="email" id="email" value="<?php echoPost('email'); ?>" />
                    </li>
                    <li>
                        <label for="street1">Street Address*</label>
                        <input type="text" name="street1" id="street1" value="<?php echoPost('street1'); ?>" />
                    </li>       
                    <li>
                        <label for="postcode">Postcode*</label>
                        <input type="text" name="postcode" id="postcode" value="<?php echoPost('postcode'); ?>" />
                    </li>                    
                </ul>
            </fieldset>
    <input  type="submit" value="Submit" style=" background:#0060a1; color:#FFFFFF; font-size:14px; border:1px solid #0060a1"/>
        </form>

    <?php endif; // END: Display form ?>

</body>
</html>
I get an error 500 again :-(
Thanks in advance
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: Getting error 500, processing form

Post by icesolid »

500 error would typically point to a server configuration error or something to that effect? Check your server log files to try and locate this errors specifics.
Post Reply