I am new to CI and have been reading documentation and following a form tutorial. However I'm struggling to get my form written to my database. Actually, the data is being written, but I'm getting the error...
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant firstname - assumed 'firstname'
Filename: controllers/form.php
Line Number: 25
I also ge the error for the second field I'm writing lastname. I think I'm close enough that MySQL is figuring out what I'm trying to do, but I'm still missing something.
My Form/View...
<html>
<head>
<title>Pledge Details</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>First Name</h5>
<input type="text" name="firstname" value="<?php echo set_value('firstname'); ?>" size="50" />
<h5>Last Name</h5>
<input type="text" name="lastname" value="<?php echo set_value('lastname'); ?>" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<h5>Kms/Miles</h5>
<select name="myselect">
<option value="Kms" <?php echo set_select('myselect', 'Kms', TRUE); ?> >Kms</option>
<option value="Miles" <?php echo set_select('myselect', 'Miles'); ?> >Miles</option>
</select>
<h5>Pledge</h5>
<input type="text" name="pledge" value="<?php echo set_value('pledge'); ?>" size="50" />
<br><br>
<div><input type="submit" value="Submit" /></div>
</form>
<?php echo form_close() ?>
</body>
</html>
My controller...
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|max_length[12]|xss_clean');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|max_length[12]|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('pledge', 'Pledge', 'trim|required|max_length[12]|decimal|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->database();
$sql = "INSERT INTO donations (firstname, lastname)
VALUES (".$this->db->escape($_POST[firstname]).", ".$this->db->escape($_POST[lastname]).")";
$this->db->query($sql);
echo $this->db->affected_rows();
$this->load->view('formsuccess');
}
}
}
?>
$_POSTwhen you can use$this->input->post('firstname');