|
Posted by petersprc on 07/19/07 17:50
This form shows how to do it:
<?
error_reporting(E_ALL);
require_once('MDB2.php');
require_once('MDB2/Date.php');
class RegistrationPage
{
var $ref = '';
var $flyer = '';
var $other = '';
var $done = false;
var $errors = array();
function run()
{
if (isset($_POST['sub'])) {
$this->ref = isset($_POST['ref']) ? trim($_POST['ref']) : '';
$this->flyer = trim($_POST['flyer']);
$this->other = trim($_POST['other']);
if ($this->ref == '') {
$this->errors[] = 'How you heard about us is required.';
} else {
if ($this->ref == 'flyer' && $this->flyer == '') {
$this->errors[] = 'Flyer name is required.';
} elseif ($this->ref == 'other' && $this->other == '') {
$this->errors[] = 'Please fill in the field indicating ' .
'how you heard about us.';
}
if (!count($this->errors)) {
$db =& $this->getDb();
$item = array(
'ref' => $this->ref,
'flyer' => $this->flyer,
'other' => $this->other,
'creationTime' => MDB2_Date::mdbNow()
);
$db->extended->autoExecute('registration', $item,
MDB2_AUTOQUERY_INSERT);
$this->done = true;
}
}
}
}
function &getDb()
{
static $db = null;
if (is_null($db)) {
$dsn = 'mysql://user:pass@host/db';
$db =& MDB2::singleton($dsn);
$db->setOption('portability', MDB2_PORTABILITY_FIX_CASE);
$db->setFetchMode(MDB2_FETCHMODE_ASSOC);
$db->loadModule('Extended');
}
return $db;
}
function checkRef($opt)
{
return $this->ref == $opt ? ' checked' : '';
}
}
$page =& new RegistrationPage;
$page->run();
?>
<? if ($page->done) { ?>
<p>Thanks!</p>
<? } elseif (count($page->errors)) { ?>
<p>Sorry an error occurred.</p>
<ul>
<? foreach ($page->errors as $err) { ?>
<li><?= $err ?></li>
<? } ?>
</ul>
<? } ?>
<form action=test.php method=post>
<p>How did you hear about us?</p>
<input type=radio name=ref value=friend<?=
$page->checkRef('friend') ?>> Friend<br>
<input type=radio name=ref value=flyer<?=
$page->checkRef('flyer') ?>> Flyer
<input name=flyer value="<?= htmlentities($page->flyer) ?>"><br>
<input type=radio name=ref value=web<?=
$page->checkRef('web') ?>> Web<br>
<input type=radio name=ref value=other<?=
$page->checkRef('other') ?>> Other
<input name=other value="<?= htmlentities($page->other) ?>"><br><br>
<input type=submit value=" OK" >
<input type=hidden name=sub>
</form>
On Jul 18, 5:42 pm, hermanh...@gmail.com wrote:
> I have a form which asks:
>
> How did you know about us?
>
> --friend
> --flyer, where_______
> --web
> --other, _______
>
> I want to store in the db whether the user checked the box and on
> certain questions, store the additional description, as indicated by
> the _____
>
> How do I go about in doing this? I need help getting the values to
> perform an INSERT in the mysql table using php.
[Back to original message]
|