Form To Collect User Email PHP

Form To Collect User Email PHP

 

This post  will show you how to create  a sample form using PHP to collect user Email

(1) 2 x PHP file will be created “userCreate” “userSave ”

(2)First PHP File-> userCreate-> This PHP file will connect the Database, -> When Successful Connected it will echo out the User Form

(3)Second PHP File->userSave -> This PHP File  will connect the Database -> Collect the User input via $_POST function ->Insert the User input information into the Data Base table

 

Prior to that you need to complete  the items listed Below

  1. Mamp or xampp with phpMyAdmin install into your Computer , check out here
  2.  A Sample Database created Check out Here
  3.  New User Created . Check out Here
  4. A New table created , check out Here
  5. Insert Data into the Table Here

Reference

The “userCreate ” PHP File

 


<?php

$hostName  ="localhost";
$userName ="jane";
$userPassword ="jane";
$database ="janedb";

$dbConnectionStatus  = new mysqli($hostName, $userName, $userPassword,$database);

//Connection Error
if ($dbConnectionStatus->connect_error){     

        
     die("Connection failed: " . $dbConnectionStatus->connect_error);

}
// Connected to Database JaneDB
// Object oriented  -> pointing 
if($dbConnectionStatus->query("SELECT DATABASE()")){
	
	$dbSuccess =true;
	//
	$result = $dbConnectionStatus->query("SELECT DATABASE()");
	$row = $result->fetch_row();
    printf("Default database is %s.\n", $row[0]);
    $result->close();

    
	
	
	
}





if ($dbSuccess) {

	echo '<h2 style="font-family: arial, helvetica, sans-serif;" >
				User Form Assignment
			</h2>';

	echo "<br />";
	
	{	//		FORM post User 
		echo '<form name="postUser" action="userSave.php" method="post">';
		
				echo '
				<table>
					<tr>
						<td>First Name</td>
						<td><input type="text" name="firstName" /></td>
					</tr>
					<tr>
						<td>Last Name</td>
						<td><input type="text" name="lastName" /></td>
					</tr>
					<tr>
						<td>Email</td>
						<td><input type="text" name="yourMail" /></td>
					</tr>
					
					
					<tr>
						<td></td>
						<td align="right"><input type="submit"  value="Save" /></td>
					</tr>
				</table>
				';
					
		echo '</form>';
	}

}

?>


 

The “userSave ” PHP File

 



<?php


/* Declare User connecting Credential*/

$hostName  ="localhost";
$userName ="jane";
$userPassword ="jane";
$database ="janedb";

$dbConnectionStatus  = new mysqli($hostName, $userName, $userPassword,$database);

//Connection Error
if ($dbConnectionStatus->connect_error){     

        
     die("Connection failed: " . $dbConnectionStatus->connect_error);

}
// Connected to Database JaneDB
// Object oriented  -> pointing 
if($dbConnectionStatus->query("SELECT DATABASE()")){
	
	$dbSuccess =true;
	//
	$result = $dbConnectionStatus->query("SELECT DATABASE()");
	$row = $result->fetch_row();
    printf("Default database is %s.\n", $row[0]);
    $result->close();

    
	
	
	
}



//Setup Field Name array . value and key
    {	//	setup ARRAY of field names 
		$personField = array(
					'id' => 'id',
					'firstname' => 'firstname',
					'lastname' => 'lastname',
					'email' => 'email',	
					

					
		);
	}






if ($dbSuccess) {
		
		{  //   collect the data with $_POST
		
			$firstName = $_POST["firstName"];	
			$lastName = $_POST["lastName"];	
			$yourMail = $_POST["yourMail"];	
				
			
		}
		
		
		
		
		
			
		{  //   SQL:    Insert String
		
			$user_SQLinsert = "INSERT INTO testtable (";			
			$user_SQLinsert .=  "firstname, ";
			$user_SQLinsert .=  "lastname, ";			
			$user_SQLinsert .=  "email ";	
			$user_SQLinsert .=  ") ";
			
			$user_SQLinsert .=  "VALUES (";
			$user_SQLinsert .=  "'".$firstName."', ";
			$user_SQLinsert .=  "'".$lastName."', ";
					
			$user_SQLinsert .=  "'".$yourMail."' ";
			$user_SQLinsert .=  ") ";
		}
		
		
		
		
		{	//		check the data and process it 
			
			if (empty($firstName )) {
				echo '<span style="color:red; ">Please Enter Your Name</span><br /><br />'; 
			} else {
					echo '<span style = "text-decoration: underline;">
							SQL statement:</span>
							<br />'.$user_SQLinsert.'<br /><br />';
							
					if ($dbConnectionStatus->query($user_SQLinsert))  {	
						echo 'Register Successful<br /><br />';
					} else {
						echo '<span style="color:red; ">FAILED to Register.</span><br /><br />';
						
					}	
			}
		}

}

echo "<br /><hr /><br />";

echo '<a href="userCreate.php">Register Another User</a>';
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo '<a href="../index.php">Quit - to homepage</a>';

?>

 

Results

userCreate.php

 

Browser View

userform
userform

 

userSave.php

userInserted
userInserted

 

MySQL

 

 

 

 

Leave a Comment

fifteen − 14 =