Welcome Guest, Not a member yet? Register   Sign In
using ajax and jquery to insert or update to database
#1

Hi again, I am really sorry to open a new topic for this question, i did read the existing ones but still did not understand what I should do.
So what I would like to ask your help for is in how do I go about setting ajax requests on a site using codeigniter, is there a good way or better way...like should i not use jquery for example.

This is what i have done, but in the end it does not work.
And I have confirmed jquery is working, also controller path is correct as well.
this code is in the view:
PHP Code:
<script src="<?php echo VEN_ASSETS; ?>js/jquery.min.js"></script>

<
script>
  $(document).ready(function() {

    $('#form').submit(function(event) {
      event.preventDefault();
      $.ajax({
        url'<?php echo base_url('/test/insert'); ?>',
        type'post',
        dataType'json',
        data: {
          cod_areaatuacod
        
},
        success: function(data) {
          alert(data);
        }
      });
    });
  });
</
script>

<
div class="card col-md-8">
  <div class="card-body">
    <form id="form" action="<?php echo base_url('/test/insert') ?>" method="post">
      <div class="form-row">
        <div class="col-md-8 mb-3">
          <label for="cod">First name</label>
          <input type="text" class="form-control" id="cod" placeholder="cod" required>
        </div>
      </div>
      <button class="btn btn-primary" type="submit">Submit form</button>
    </form>
  </div>
</
div

and this is the simple method inside the controller to give me feedback if it is working or not:
PHP Code:
public function insert()
  {
    echo '<p>insert controller</p>';

    if ($this->request->isAJAX()) {
      echo $this->request->getPost('post_cod');
  }


Thank you for your time and any advice you can give is more than welcome.
Reply
#2

Hi, check out this thread, I wrote a good example there: https://forum.codeigniter.com/thread-76104.html
Obviously, you can omit the csrf cookie stuff in case you're not using it, or how my script reacts based on the submit button clicked (if you have more than one submit button in form)
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply
#3

(04-25-2020, 04:17 AM)Leo Wrote: Hi, check out this thread, I wrote a good example there: https://forum.codeigniter.com/thread-76104.html
Obviously, you can omit the csrf cookie stuff in case you're not using it, or how my script reacts based on the submit button clicked (if you have more than one submit button in form)

So I have omited the csrf and so far it works, here is the current code with somethings omitted to stay understandble:
-first is the ajax function, I placed it on a helper file
PHP Code:
function load_ajax_post($args)
{
    echo '
<script>
  $(document).ready(function() {
    $("#confirm").on("click", function() {

      var data = ' 
$args['
      data '
] . ';

      $.ajax({
        type: "post",
        url: "' 
$args['url'] . '",
        data: data,
        success: function(response) {

          if (response == "success") {
            console.log("success!");

          } else if (response == "fail") {
            console.log("fail!");
          };
        }
      });
    });
  });
</script>
'


here is how i call it in the view
PHP Code:
<?php
$form_field_data 
'{
                cod_areaatua: $("#cod_areaatua").val(),
                desc_areaatua: $("#desc_areaatua").val()
                }'
;
$args = array(
  'data' => $form_field_data,
  'url' => base_url('/definicoes/area_atuacao/insert')
);
?>
<?php load_ajax_post
($args); ?>

and here is how i recieve in the controller:
PHP Code:
public function insert()
  {
    if ($this->request->isAJAX()) {

      $session session();

      $data_array['cod_areaatua'] = $this->request->getPost(['cod_areaatua']);
      $data_array['desc_areaatua'] = $this->request->getPost(['desc_areaatua']);
      $data_array['user_areaatua'] = $_SESSION['login_utiliz'];
      $cod_areaatua $data_array['cod_areaatua']['cod_areaatua'];

      if (Area_atuacao_model::insert_area_atuacao($data_array)) {
        echo 'success';

      } else {
        echo 'fail';

      }
    }
  

so, I was wondering if I could have some help on how to load pages with ajax in codeigniter.
I was able to load a page by addressing the controller and method in charge of it, but I had this warning about "promise" or something.
Also, I was trying to use url parameters "?id=1" which seemed to break it.
Is there a "Codeigniter" way of loading pages with ajax?

Also, thanks for the help Leo, it pointed me in a good direction.
Reply
#4

(This post was last modified: 04-28-2020, 03:25 PM by Leo.)

Yah, no prob. I'm not familiar of a "CodeIgniter" way of loading AJAX, I haven't encountered a need to do anything like that so far with my CI4 project.

I remember loading a view of products with AJAX in CI3. I loaded a view in a controller, passed data to it, turned the whole thing into a string, and sent it back with JSON response - then appended it to some div within the html document. More or less.
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB