[eluser]bill19[/eluser]
Hi everyone,
I am working on a project and have come across a CI form generator on github at
https://github.com/ariven/Form_Builder-for-CodeIgniter . I have been trying to get this working with help from the author ( a very nice guy ). I have been able to get the graceful fallback ( without JS ) working, but so far cannot get JS working.
Also, looking at application/config/config.php I dont even have CSRF protection on at all, so that shouldn't be the issue.
Would anyone mind looking at this?
Thanks,
Bill
P.S. My form has one field ('a' ) and a submit button for this test project.
Here is the view:
Code:
<!DOCTYPE html>
<html>
<head>
<base href="<? echo base_url();?>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<fieldset>
<legend>form</legend>
<?php echo form_open("welcome/process", array("id" => "form", "class" => "form-horizontal")); ?>
<div class="control-group">
<label class="control-label" for="a">a</label>
<div class="controls">
<input type="text" value="<?php if (isset($a)) { echo $a; } ?>" name="a" id="a" placeholder="a" />
<span class="form_error" id="a_error"><?php if (isset($error['a'])) { echo $error['a']; } ?></span>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-primary" id="submit">Save</button> or <a >Cancel</a>
</div>
</div>
<?php echo form_close(); ?>
</fieldset>
[removed][removed]
[removed][removed]
[removed][removed]
[removed][removed]
</body>
</html>
Here is the controller:
Code:
function process() {
$this->load->library('form_validation');
$this->load->helper('url');
$is_ajax = $this->input->is_ajax_request();
$ajax_error = FALSE;
$rules[] = array('field' => 'a', 'label' => 'a', 'rules' => 'trim|required');
// load any variables to refill the form
$variables['a'] = $this->input->post('a', TRUE);
// alternately you can load them this way, if you are preloading an item from a database, and
// you just want to overwrite the changed variables...
/*
$variables = $this->model_name->get($id);
$posted = $this->input->post(NULL, TRUE);
if ($posted) {
foreach ($posted as $key => $value) {
$variables[$key] = $value;
}
}
*/
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == FALSE) {
// first load or failed form
$error = array(
'a' => form_error('a'),
);
$variables['error'] = $error;
if ($is_ajax) {
if (count($error) > 0) {
$ajax_error = TRUE;
}
$message = $error;
} else {
//
// change the view name to the name of what you save the view file as
//
$message = $this->load->view('form', $variables, TRUE);
}
} else {
// perform action based on submission of form, if you return $message in this manner
// it defaults to replacing the form in the page with the message that is returned.
$message = '<p>You just successfully submitted <strong>this</strong> form!</p>';
}
// this is where you display results. javascript version or full template version
echo $message;
if ($is_ajax) {
if ($ajax_error) {
echo json_encode($message);
} else {
echo json_encode(array('form_message' => $message));
}
} else {
$this->load->view('form');
//load view here, or render if using a template library
$config['content'] = $message;
// $this->template->add_script('form.js');
// $this->template->render($config);
}
} // process()
}
Here is the js:
Code:
// form handler
$("#form").submit(function() {
// add CSRF token
$('#submit').before('<input type="hidden" name="ci_csrf_token" value="'+$.cookie("ci_csrf_token")+'" />');
$.post("/index.php/welcome/process", $('#form').serialize(), function(data) {
$("#a_error").html(data.a);
if (data.form_message) {
$('#form').html(data.form_message);
}
if (data.redirect_to) {
[removed] = data.redirect_to;
}
},'json'
);
return false;
});