Welcome Guest, Not a member yet? Register   Sign In
Model Array help.. At a complete loss..
#1

[eluser]Unknown[/eluser]
Hey Guys,

I am really struggling with this problem..

Quote:What I want to achieve

I would like to be able to fill out an "add site" form from my front-end.

The inputs are account_id(hidden), site_title, site_address, site_description, site_category.

The inputs will then be passed via json to my controller which passes that to my model then into my database.. The front-end then updates.

I have got as far as inserting data into the database and then the front-end updates but there is a glitch.

Whenever I put http:// in the site_address input field, the database does not update, nor will it update in I put spaces into any of the input fields..

I have tried urlencode on the site_address field and that now passes the data nut I have no clue how to disassemble the array, urldecode and reassemble the array in the model..

Could someone give me a few pointers please?

MODEL

Code:
public function getsites($account_id) {
$query = $this->db->get_where('usersites', array('account_id' => $account_id));
if( $query->num_rows() > 0 ) {
  return $query->result();
} else {
  return array();
}
}

CONTROLLER

Code:
public function addUserSites() {
$account_id = trim($this->input->post('account_id'));
$site_title = trim($this->input->post('site_title'));
$site_description = trim($this->input->post('site_description'));
$site_address = urlencode($this->input->post('site_address'));
$site_category = trim($this->input->post('site_category'));
  $array = array(
  'account_id' => $account_id,
  'site_title' => $site_title,
  'site_description' => $site_description,
  'site_address' => $site_address,
  'site_category' => $site_category,
  
);
  
echo json_encode($this->usersites_model->insertsites($array));
}

FORM IN VIEW

Code:
<form action="" method="post" class="addsite_form">
<input id="account_id" type="hidden" value="<?php echo $this->session->userdata('account_id');?>"/>
    <label for="site_title"><strong>Site Name:</strong></label>
    &lt;input id="site_title" type="text" maxLength="25" tabindex="1" name="site_title" /&gt;
    <label for="site_address"><strong>Site Address:</strong></label>
    &lt;input id="site_address" type="text" tabindex="2" name="site_address" /&gt;
    <label for="site_description"><strong>Site Description:</strong></label>
    &lt;input id="site_description" type="text" tabindex="3" name="site_description" /&gt;
    <label for="site_category"><strong>Site Category:</strong></label>
    &lt;input id="site_category" type="text" tabindex="4" name="site_category" /&gt;

    &lt;input id="addsite_form_submit" type="submit" value="Add Site" name="submit" tabindex="5" /&gt;
&lt;/form&gt;

JQUERY

Code:
$(function() {  
$('.addsite_form').submit(function() {
  var account_id = $("#account_id").val();
  var site_title = $("#site_title").val();
  var site_address = $("#site_address").val();
  var site_description = $("#site_description").val();
  var site_category = $("#site_category").val();
  $.ajax({
   type: 'POST',
   url: '/usersites/addusersites',
   dataType: 'json',
   data: 'account_id='+ account_id +'&site;_title='+site_title+'&site;_address='+site_address+'&site;_description='+site_description+'&site;_category='+site_category,
  });
});
});


Kindest Regards





Theme © iAndrew 2016 - Forum software by © MyBB