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


Messages In This Thread
using ajax and jquery to insert or update to database - by joseCarlos - 04-25-2020, 02:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB