Jump to: navigation, search

PHP Get or Retrieve Form Data

From w3cyberlearnings

Contents

How to get or process the form data

  • When user fill in all the require form and send to the server for processing, the server requires to pare or process the form data.
  • In this demo, you will learn how to convert the form data into array or into object for further processing: storing into the user data into database or request another information from the database.

Form

<html>
<head>
<title>User Register</title>
</head>
<body>
<form method="post" action="process.php">
     <table border="0">
       <tr>
         <td>First Name:</td>
	 <td>
             <input type="text" name="first_name" size="20" maxlength="20"/>
         </td>
       </tr>
       <tr>
	<td>Middle Name:</td>
	<td>
           <input type="text" name="middle_name" size="20" maxlength="20" />
        </td>
       </tr>
       <tr>
	   <td>Last Name:</td>
	   <td>
              <input type="text" name="last_name" size="20" maxlength="20" />
           </td>
       </tr>
       <tr>
	   <td>Tel/Phone:</td>
	   <td>
              <input type="text" name="phone" size="20" maxlength="20" />
           </td>
       </tr>
       <tr>
	   <td>Login Name:</td>
	   <td>
               <input type="text" name="login_name" size="20" maxlength="20" />
           </td>
       </tr>
       <tr>
	   <td>Password:</td>
	   <td>
              <input type="password" name="login_password1" size="20" maxlength="20" />
           </td>
	</tr>
	<tr>
	   <td>Confirm:</td>
	   <td>
              <input type="password" name="login_password2" size="20" maxlength="20" />
           </td>
	</tr>
	<tr>
	   <td colspan="2">
              <input type="submit" name="register_btn" value="Register" />
           </td>
	</tr>
     </table>
   </form>
</body>
</html>

Convert Form data into object

<?php
if ($_POST) {
	$object = new stdClass();
	foreach ($_POST as $name => $value) {
               // we don't want the register_btn 
               // in the object
		if ($name == 'register_btn')
			continue;
		$object->$name = $value;
	}

	print_r($object);
}
?>

Convert Form data for the POST or GET


// for POST method
$aa = array();
if($_POST) {
   foreach($_POST $name=>$value) {
        $aa[$name]= $value;
   }
}
print_r($aa);


// for GET method
$aa = array();
if($_GET) {
   foreach($_GET $name=>$value) {
        $aa[$name]= $value;
   }
}
print_r($aa);

PHP form for processing Form data

  • This function can return object, array or json data generated from the form.
//method: GET, POST
//return: array, json, object
//not_name_frm_aa: list of the form element that not included for the return data

function process_form_data($method="GET", $return="array", $not_name_frm_aa=null) {
	$return_co = "";

	if ($method == "POST") {
		$return_co = array();
		foreach ($_POST as $name => $value) {
			if (in_array($name, $not_name_frm_aa))
				continue;
			$return_co[$name] = $value;
		}
	} else if ($method == "GET") {
		$return_co = array();
		foreach ($_GET as $name => $value) {
			if (in_array($name, $not_name_frm_aa))
				continue;
			$return_co[$name] = $value;
		}
	}
	
	if ($return_co) {
		if ($return == "array")
			return $return_co;
		else if ($return == "object")
			return (object) $return_co;
		else if ($return == "json")
			return json_encode($return_co);
	} else {
		return false;
	}
}

Example 1: Processing GET and return form data as json

<?php
if ($_GET) {
	$re_data = process_form_data("GET", 'json', array('register_btn'));
	print_r($re_data);
}
?>

Example 2: Processing POST and return form data as json

<?php
if ($_POST) {
	$re_data = process_form_data("POST", 'json', array('register_btn'));
	print_r($re_data);
}
?>


Related Links


Dynamic HTML From Array
  1. Dynamic Drop Down List with Array
  2. Dynamic List with Array
  3. Dynamic Radio with Array
  4. Dynamic Checkbox with Array
Dynamic HTML From Database
  1. Dynamic Drop Down List with Database
  2. Dynamic List from Database
  3. Dynamic Radio from Database
  4. Dynamic Checkbox from database
Dynamic HTML with Ajax
  1. Dynamic Drop Down List with Ajax
  2. Dynamic List with Ajax
  3. Dynamic Radio with Ajax
  4. Dynamic Checkbox with Ajax
  5. Dynamic Form with Ajax
Others Related
  1. Dynamic Drop down with default option
  2. How to retrieve Form Data
Navigation
Web
SQL
MISC
References