homepage Welcome to WebmasterWorld Guest from 54.226.225.215
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Form validation then submit all with PHP.is it possible?
zoegirl2003




msg:4242793
 7:16 pm on Dec 15, 2010 (gmt 0)

I am very new to PHP. I only know a few basics. I am trying to get a form to validate then submit to a database using a insertGFinfo.php file. I figured out how to get the validation, but I can't figure out how to direct the submit to the insertGFinfo.php file and run that file to post to the database and display the second part of the form.

First file is this...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Member Enrollment</title>
<link href="../2IGMainLight.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../includes/Creative_Menus/menuDisplay.js"></script>
<script type="text/javascript" src="../../includes/Creative_Menus/swfobject.js"></script>


</head>


<body>
<div id="container">
<?php include('templates/header.html'); ?>
<div id="main5" class="enroll">
<div id="seperate2" class="fontgreen14">
<p>This is were text can be put to outline the basic benefits for the membership. Pernatem di ut aut as que nima dit lignimusdae pra nis eum aut ut unt arum et enectati dolupti busapictum as quam fuga. Itae non pelliquate verovit empeliae. Arum endis dolluptas eossequi quunt laut ommolendi core cus sum num quunt quam, suntiosam eum non et volorrumquis dus, optatem ut laccusam, sunt.
Caerit fuga. Nam et fugit modi conemquos dolorae. Nam rectotatque es sunt ex exerae list.
<ul>
<li class="bulletTab">Et eos dicaepe ritatecus suntio
</li>
<li class="bulletTab">st
quis que maximus eos nobisquamet laborio </li>
<li class="bulletTab">Riore pa que elentin verunt. Untium inctur sequae erciae</li>
<li class="bulletTab">Et eos dicaepe ritatecus suntio
</li>
<li class="bulletTab">st
quis que maximus eos nobisquamet laborio </li>
<li class="bulletTab">Riore pa que elentin verunt. Untium inctur sequae erciae </li>
</ul>
</div>
<div class="main4">
<h1 class="ltgraygreen"><br />
2 Iron Golf Participating Facility Enrollment<br />
<br /></h1>
</div>
<div>

<?
function error_bool($error, $field) {
if($error[$field]) {
print("<td colspan='3' class='tableindentERROR'>");
}
else {
print("<td colspan='3' class='tableindent'>");
}
}

function show_form() {
global $HTTP_POST_VARS, $print_again, $error;
?>

<form method="post" action="" class="mem_table" >
<table width="600" border="0" cellspacing="0" cellpadding="4">

<!--Golf Facility Information -->

<tr>
<td colspan="5" class="tableHeader">Enter the golf facility's information</td>
</tr>
<tr>
<?php error_bool($error, "GFacilityName"); ?> Golf Facility:</td>
<td colspan="2"><input name="GFacilityName" type="text" value="<? echo $_POST["GFacilityName"]; ?>" class="fieldtext" id="GFacilityName" accesskey="g" tabindex="1" size="50" maxlength="100" /></td>
</tr>

<tr>
<td colspan="3" class="tableindent">Golf Facility's Address Line 1:</td>
<td colspan="2"><input name="GFAddress1" type="text" value="<?php $_POST[GFAddress1]; ?>" class="fieldtext" id="gfAddress1" accesskey="a" tabindex="2" size="50" maxlength="100" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent">Golf Facility's Address Line 2:</td>
<td colspan="2"><input name="GFAddress2" type="text" value="<?php $_POST[GFAddress2]; ?>" class="fieldtext" id="gfAddress2" accesskey="a" tabindex="3" size="50" maxlength="100" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent">Golf Facility's City:</td>
<td colspan="2"><input name="GFCity" type="text" value="<?php $_POST[GFCity]; ?>" class="fieldtext" id="gfCity" accesskey="c" tabindex="4" size="30" maxlength="50" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent">Golf Facility's State:</td>
<td colspan="2">

<?php
$states_arr = array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"District Of Columbia",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming");
?>

<?php
function showOptionsDrop($array){
$string = '';
foreach($array as $k => $v){
$string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";
}
return $string;
}
?>

<select name="GFState" class="fieldtext">
<option value="0">Choose a state</option>
<?php echo showOptionsDrop($states_arr); ?>
</select></td>
</tr>
<tr>
<td colspan="3" class="tableindent">Golf Facility's Zip Code:</td>
<td colspan="2"><input name="GFZip" type="text" value="<?php $_POST[GFZip]; ?>" class="fieldtext" id="gfZip" accesskey="z" tabindex="5" size="12" maxlength="12" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent"> Golf Facility's Phone 1:</td>
<td colspan="2"><input name="GFPhone1" type="text" value="<?php $_POST[GFPhone1]; ?>" class="fieldtext" id="gfPhone1" accesskey="p" tabindex="6" width="150" size="16" maxlength="16" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent"> Golf Facility's Phone 2:</td>
<td colspan="2"><input name="GFPhone2" type="text" value="<?php $_POST[GFPhone2]; ?>" class="fieldtext" id="gfPhone2" accesskey="p" tabindex="7" width="150" size="16" maxlength="16" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent">Golf Facility's Website:</td>
<td colspan="2"><input name="GFWebsite" type="text" value="<?php $_POST[GFWebsite]; ?>" class="fieldtext" id="gfWebsite" accesskey="w" tabindex="8" size="50" maxlength="100" /></td>
</tr>

<!--Golf Facility Contact information -->

<tr>
<td colspan="5" class="tableHeader">Enter your contact info</td>
</tr>
<tr>
<td colspan="3" class="tableindent">First Name:</td>
<td colspan="2"><input name="First" type="text" value="<?php $_POST[First]; ?>" class="fieldtext" id="First" accesskey="f" tabindex="9" size="30" maxlength="50" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent">Last Name:</td>
<td colspan="2"><input name="Last" type="text" value="<?php $_POST[Last]; ?>" class="fieldtext" id="gfLast" accesskey="l" tabindex="10" size="30" maxlength="50" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent">Title / Position:</td>
<td colspan="2"><input name="Title" type="text" value="<?php $_POST[Title]; ?>" class="fieldtext" id="gftitle" accesskey="t" tabindex="11" size="50" maxlength="100" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent">Address Line 1:</td>
<td colspan="2"><input name="Address1" type="text" value="<?php $_POST[Address1]; ?>" class="fieldtext" id="gfAddress1" accesskey="a" tabindex="12" size="50" maxlength="100" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent">Address Line 2:</td>
<td colspan="2"><input name="Address2" type="text" value="<?php $_POST[Address2]; ?>" class="fieldtext" id="gfAddress2" accesskey="a" tabindex="13" size="50" maxlength="100" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent">City:</td>
<td colspan="2"><input name="City" type="text" value="<?php $_POST[City]; ?>" class="fieldtext" id="gfCity" accesskey="c" tabindex="14" size="30" maxlength="50" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent">State:</td>
<td colspan="2">

<select name="State" class="fieldtext">
<option value="0">Choose a state</option>
<?php echo showOptionsDrop($states_arr); ?>
</select></td>
</tr>
<tr>
<td colspan="3" class="tableindent">Zip Code:</td>
<td colspan="2"><input name="Zip" type="text" value="<?php $_POST[Zip]; ?>" class="fieldtext" id="gfZip" accesskey="z" tabindex="15" size="12" maxlength="12" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent"> Phone 1:</td>
<td colspan="2"><input name="Phone1" type="text" value="<?php $_POST[Phone1]; ?>" class="fieldtext" id="gfPhone1" accesskey="p" tabindex="16" width="150" size="16" maxlength="16" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent"> Phone 2:</td>
<td colspan="2"><input name="Phone2" type="text" value="<?php $_POST[Phone2]; ?>" class="fieldtext" id="gfPhone2" accesskey="p" tabindex="17" width="150" size="16" maxlength="16" /></td>
</tr>
<tr>
<?php error_bool($error, "Email"); ?> E-mail Address:</td>
<td colspan="2"><input name="Email" type="text" value="<? echo $_POST["Email"]; ?>" class="fieldtext" id="Email" accesskey="e" tabindex="18" size="50" maxlength="100" /></td>
</tr>

<!--Create Username/password -->

<tr>
<td colspan="5" class="tableHeader">Enter your desired username and create your password</td>
</tr>
<tr>
<td height="35" colspan="3" class="tableindent">Username:</td>
<td colspan="2"><input name="Username" type="text" value="<?php $_POST[Username]; ?>" class="fieldtext" id="gfUsername" accesskey="u" tabindex="19" size="50" maxlength="50" /></td>
</tr>
<tr>
<td colspan="3" class="tableindent">Password:</td>
<td colspan="2">
<input name="Password" type="password" value="<?php $_POST[Password]; ?>" class="fieldtext" id="gfPassword1" accesskey="u" tabindex="20" size="50" maxlength="50" />
</td>
</tr>
<tr>
<td colspan="3" class="tableindent">Password Confirmation:</td>
<td colspan="2">
<input name="Password2" type="password" class="fieldtext" id="gfPassword2" accesskey="u" tabindex="21" size="50" maxlength="50" />
</td>
</tr>
<tr>
<td colspan="5" class="tableindent"><input name="Authorized" type="checkbox" value="1" id="tweet" checked="checked" />
Yes, I am authorized to sign up our golf facility with 2 Iron Golf's Participating Golf Facilities!</td>
</tr>
<tr>
<td colspan="5" class="tableindent"><input name="RecEmails" type="checkbox" value="1" id="gfnews" checked="checked" />
Yes, I would like to receive emails and news from 2 Iron Golf!</td>
</tr>

<tr>
<td colspan="5" class="tableHeader">Click &quot;Register&quot; to create your new account. </td>
</tr>
<tr>
<td width="20" class="tableindent2">&nbsp;</td>
<td width="82"><input type="submit" name="submit" id="submit" value="Register" /></td>
<td width="54"><input type="submit" name="cancel1" id="cancel1" value="Cancel" /></td>
<td colspan="2" class="fontgray12noindent">Reset this <a href="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>">form</a>.</td>
</tr>
<tr>
<td class="tableindent2">&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan="2" class="fontgray12noindent">&nbsp;</td>
</tr>
</table>
</form>

<?
}
if(isset($_POST["submit"])) {
check_form();
} else {
show_form();
}

function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}


function check_form()
{
global $HTTP_POST_VARS, $error, $print_again;
$error['GFacilityName'] = false;
if($_POST["GFacilityName"]=="") {
$error['GFacilityName'] = true;
$print_again = true;
$message="The name field is empty<br>";
}
if(!check_email_address($_POST['Email'])) {
$error['Email'] = true;
$print_again = true;
$message.="Either Field Empty or Invalid Email ID <br>";
}
if($print_again) {
show_form();

} else {
header('Location:insertGFinfo.php');

}
echo "$message";
}
?>

<?php include('templates/footer.html'); ?>
</div>

</div>


</div> <!-- Container end -->

</body>
</html>


Second page is... insertGFinfo.php

<?
$username="myusername";
$password="mypassword";
$database="mydatabase";
$servername = "myservername";

//Golf Facility info
$GFacilityName=$_POST['GFacilityName'];
$GFAddress1=$_POST['GFAddress1'];
$GFAddress2=$_POST['GFAddress2'];
$GFCity=$_POST['GFCity'];
$GFState=$_POST['GFState'];
$GFZip=$_POST['GFZip'];
$GFPhone1=$_POST['GFPhone1'];
$GFPhone2=$_POST['GFPhone2'];
$GFWebsite=$_POST['GFWebsite'];

//Golf Facility Contact info
$First=$_POST['First'];
$Last=$_POST['Last'];
$Title=$_POST['Title'];
$Address1=$_POST['Address1'];
$Address2=$_POST['Address2'];
$City=$_POST['City'];
$State=$_POST['State'];
$Zip=$_POST['Zip'];
$Phone1=$_POST['Phone1'];
$Phone2=$_POST['Phone2'];
$Email=$_POST['Email'];
$Username=$_POST['Username'];
$Password=$_POST['Password'];
$Authorized=$_POST['Authorized'];
$RecEmails=$_POST['RecEmails'];

//connect to the database
$connect=mysql_connect($servername,$username,$password);
mysql_select_db($database, $connect) or die ("Database not selected : " . mysql_error());


$queryGF = "INSERT INTO GFFacility (GFacilityName, GFAddress1, GFAddress2, GFCity, GFState, GFZip, GFPhone1, GFPhone2, GFWebsite)
VALUES('$GFacilityName','$GFAddress1','$GFAddress2','$GFCity','$GFState','$GFZip','$GFPhone1','$GFPhone2','$GFWebsite')";

$queryGFC = "INSERT INTO GFacilityContact (First, Last, Title, Address1, Address2, City, State, Zip, Phone1, Phone2, Email, Username, Password, Authorized, RecEmails)
VALUES('$First','$Last','$Title','$Address1','$Address2','$City','$State','$Zip','$Phone1','$Phone2','$Email','$Username','$Password','$Authorized','$RecEmails')";


// run the query for Golf Facility
$return1=mysql_query($queryGF);

if($return1){echo " Command is successful ";}
else
{echo "Command is not successful ";}

echo mysql_error();

// run the query for Golf Facility Contact
$return2=mysql_query($queryGFC);

if($return2){echo " Command is successful ";}
else
{echo "Command is not successful ";}

echo mysql_error();

mysql_close($connect);
include('gf_enroll_pg2Light.php');
?>
<!-- include your own success html here -->





What I get is a Warning: Cannot modify header information - headers already sent by (output started at /home/content/t/w/o/twoirongolf/html/beta/gf_enroll_pg1Light2.php:7) in /home/content/t/w/o/twoirongolf/html/beta/gf_enroll_pg1Light2.php on line 278 error and so I don't know how to direct to the second script after the validation. If I put the insertGFinfo.php as the action in the form, it goes directly to the insertGFinfo.php file instead of validating. I hope I am making since.

Thanks for any help

Lorie

 

Matthew1980




msg:4242798
 7:43 pm on Dec 15, 2010 (gmt 0)

Hi there zoegirl2003,

Welcome to WebmasterWorld!

I don't have time to sift through your post, but from the initial paragraph I can see what your referring to - I think!

Your form tag needs to have the action attribute pointing at your php file, then when the submit is pressed (actioned) it uses the referenced file to direct the actioned form to for processing with the software you have written.

Also, for compatibility purposes, use the full tags (<?php ?>) so that your software will work on all php installations.

Cheers,
MRb

zoegirl2003




msg:4242837
 8:55 pm on Dec 15, 2010 (gmt 0)

Thanks MRb,

I know I don't have the action pointing to the php file. When I do this, it will post to my database and skip all the validation. If I take it out the validation works. So I need a way for the validation to run and then when all is good it will go to the php file to post to the database. Does that make since? I'm not even sure if it can be done. I tried the header() function but I get the above error.

Thanks
Lorie

zoegirl2003




msg:4242928
 1:28 am on Dec 16, 2010 (gmt 0)

Anyone?

Anyango




msg:4242995
 7:03 am on Dec 16, 2010 (gmt 0)

You will have to write a javascript function in this html page that gets called on form submission, it will validate all the fields and then submit to your php file

like

<script language="javascript">
function validate()
{
//validate all fields here 1 by one, if an error just return false


//if all good return true
}

</script>


<form action="yourfile.php" method="post" onsubmit="return validate();">

g1smd




msg:4243054
 10:54 am on Dec 16, 2010 (gmt 0)

One comment about the overall method.

The first line should NOT be the DOCTYPE followed immediately by passing things to the browser and only later invoking some PHP logic.

The majority of the PHP logic should be first, because sometimes you will want to send some HTTP HEADER data before you output an HTML page, and it is too late to send headers once the DOCTYPE has been sent.

zoegirl2003




msg:4243155
 3:53 pm on Dec 16, 2010 (gmt 0)

g1smd...so are you saying that the php scripting should go before the DOCTYPE instead of at the end? I do want to learn what the proper way to write all this is. I did take online classes, but they really didn't teach much. It was almost a learn on your own with very simple scripting so I am having difficulties now trying to do the more complex scripting. Without someone to show me how it is hard to figure it out. I am doing a lot of trial and error :)

Anyango...I was trying to stay away from Javascript because I read it wasn't as secure as using php scripting and some people turn javascript off on their computers I want to try and stay with more server side applications.

Any other comments on using Javascript vs php on validation and submit?

rocknbil




msg:4243223
 5:21 pm on Dec 16, 2010 (gmt 0)

Return the error state from your check_form,


function check_form () {
$myErrors = null;
// go through the list . . .
if ([some error]) {
$myErrors .= "<li>The so and so is in the wrong format.</li>";
}
return $myErrors;
}


and . . . .


$errors=null;
if(isset($_POST["submit"])) {
$errors =check_form();
if ($errors) { show_form($errors); }
// The previous should exit, no else required
add_database_update_here();
add_response_page_here();

} else {
show_form();
}


then in show form,


function show_form ($errorList=null) {
// =null makes it optional for first call
if ($errorList) {
// output your errors, populate the form vars
// that were previously input.
echo "<ul>$errorList</ul>";
}
}


If you keep it all in one script there's no reason to redirect all over the 'net to do a task. You could even do,

if(isset($_POST["submit"])) {
include($_SERVER['DOCUMENT_ROOT'] . "/database-script.php");
....

with only a few modifications. It gets even better if you separate HTML from PHP with templating, but that's another direction.

Anyango




msg:4243237
 5:49 pm on Dec 16, 2010 (gmt 0)

Yep simple, then post directly to your script without any client side validation and then your php script checks if all the values are present, only then it saves to db or does other processing, incase something is missing your script sends back to the form like suggested in the post above.

But just for information, javascript is not bad at all, and we do not need it to be a very secure form of scripting, it does its job just perfectly and then you can validate the values again in php

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved