FacebookTwitterGoogle+Share

PHPing a very simple form

We’re going to create the same form as last time, but enhance it a bit using PHP and MySQL. Where applicable, I’ve linked parts of the code to the appropriate documentation where you can find more information and possibly cake.

We’ll be using more than one PHP file, just to show off require_once.

The first step is to connect to the database. I’ll include a couple of simple functions for dealing with the resources returned.

This file is called mysql.inc.php:

<?php
// This is the magical start tags that ends with ?>. Between these two guys is where we place our PHP code. You can have more than one PHP block per file, as you'll see below, and the "php" succeeding the ? character isn't actually necessary.
// These two slashes start a single-lined comment. Anything following those two characters, until the line ends is ignored by PHP - you know, for documentation? :p
//configuration information
$server = 'localhost';
$username = '';
$password = '';
$database = 'quizes';
// Variables begin with the $ character, and all statements are terminated using the ; character.
//connect to the database
mysql_connect($server, $username, $password);
mysql_select_db($database);
// Retrieve rows from a table as an array of associative arrays
function sql_getRows($query) { //$query is a line of SQL we will pass to the function
	$rows = mysql_query($query);
	return sql_assoc($rows); //this function is defined 3 lines from now
}
//turn a mysql resource into an array of associated arrays
function sql_assoc($resource) {
	$i = 0;
	$values = array();
	while ($row = mysql_fetch_assoc($resource)) {
  		foreach($row as $k => $v) $values[$i][$k] = $v;
  		$i++;
	}
	return $values;
}
?>

Okay, and now for the form! We’ll call this file index.php:

<?php
require_once('mysql.inc.php');
// We're telling php to require the inclusion of the file we just made (above). We could have used include_once, but it won't work without that file anyway.
// create the html for a select box populated with clients from a database
// we want all of the clients
$clients = sql_getrows('SELECT client_id, client_name FROM clients');
// This is the SQL select query we're sending to the database through our sql_getrows function. Since we want all the clients, we left off the WHERE clause. We are also explicitly specifying fields by name - the * character can be used if you'd like to grab all the fields.
// The format of the data we get back will be an array of associative arrays - so what we are getting is a nested array. The first level is indexed numerically; the second by they database table field name. To access the client_id of the first record returned, you would use the syntax $clients[0]['client_id']. Or, you can use a loop to run through the data:
$select_html = "<select name=\"clientId\">\n";
//Right now we're creating a string (stored as the variable $select_html) which consists of the HTML necessary for a select box.
// \n is an escape character. One set of quotes tells PHP where our string starts and where it ends, another is part of the HTML we'll be outputting. If we have quotes within quotes, we have to escape them using the character. Similarly, it is used for special characters like n - which is the new line character. The new lines won't change how your form is displayed within the web browser, but they can be important if you're going to be viewing the source created. The same goes for t, except that it is the tab character. Outputting well-formatted code can help with debugging.
foreach($clients as $c) {
	$select_html .= "t<option value="". $c['client_id'] ."">". $c['client_name'] ."</option>n";
	// Foreach loops can take the form ($array as $key=>$value), or just ($array as $value) - this is the latter. Each time it loops through the clients list, we use the variable $c to reference that client's information. $c is itself an associative array keyed by the client table's field names (that we requested via SQL).
}
$select_html .= "</select>n";
// the . character is for string concatenation. Placing it before the = symbol is the same as writing $select_html = $select_html . "</select>n";. So we're just appending to the string already held in the variable.
?>
<!-- Here ends our first php block for the file -->
<h1>Quiz maker</h1>
<form method="post" value="form-handler.php" >
Client: <? echo $select_html; ?>
<!-- This is a single statement within a php block. Echo just tells php to output the value of $select_html variable here. -->
Name: <input type="text" name="QuizName" value="MyFirstQuiz" />
Date: <input type="text" name="QuizDate" value="<? echo date("Y-m-d"); ?>" /> <small>YYYY-MM-DD</small>
<table>
<tr>
<th>Question</th>
<th>Answer</th>
</tr>
<tr>
<td><input type="text" name="questions[]" /></td>
<td><input type="text" name="answers[]" /></td>
</tr>
<tr>
<td><input type="text" name="questions[]" /></td>
<td><input type="text" name="answers[]" /></td>
</tr>
</table>
<input type="submit" value="Create ยป" />
</form>

Now we have a form!

Next time, maybe the form submission handler? Or maybe a blog about perfume? Or maybe installing Magento on Dreamhost? I don’t even know!

 

Comments

  1. Shaun says:

    I like this post a lot, primarily because of the blue arrow boxes. I demand more blue arrow boxes in the future.

  2. Brad says:

    The last one had those guys, too. But okay, maybe i’ll do the submission handler next then?