Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:38:58+00:00 2026-06-05T16:38:58+00:00

I’m trying to submit a form (it’s a dynamic form with fields added via

  • 0

I’m trying to submit a form (it’s a dynamic form with fields added via jQuery) to CodeIgniter for a db insert. Part of it works, the other part doesn’t.

Here’s the jQuery:

function submitForm() {
    $.ajax({
        type: 'POST',
        url: '/raffle/save/',
        data: $('#raffle').serialize(),
        success: function (response) {
            alert(response);
        },
        error: function() {
            alert('Failed'); // This is what I get unless I comment out the entry insert
        }
    });
}

The CI controller:

class Raffle extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('raffle_model');
        $this->load->library('form_validation');
    }

    public function index() {
        $data['title'] = 'Create a Raffle';
        $this->load->view('header', $data);
        $this->load->view('raffles/create_view', $data);
        $this->load->view('raffles/bottombar_view', $data);
        $this->load->view('footer', $data);
    }

    public function save() {
        foreach($_POST as $k => $v) {
            if($k == 'entrant' || $k == 'tickets') {
                foreach ($v as $i => $vector) {
                    $this->form_validation->set_rules('entrant[' . $i . ']', 'Entrant name', 'trim|required|min_length[1]|max_length[100]|xss_clean');
                    $this->form_validation->set_rules('tickets[' . $i . ']', 'Number of tickets', 'trim|required|max_length[2]|is_natural_no_zero|xss_clean');
                }
            } else {
                $this->form_validation->set_rules('raffle-name', 'Raffle name', 'trim|required|min_length[4]|max_length[100]|xss_clean');
                $this->form_validation->set_rules('winners', 'Number of winners', 'trim|required|max_length[2]|is_natural_no_zero|xss_clean');
            }
        }

        if($this->form_validation->run() == FALSE) {
            echo 'Validation failure!';
        } else {
            if($this->raffle_model->add_raffle()) { // It does pass validation and goes to the model
                echo 'Data added successfully!';
            }
        }
    }
}

And the CI model:

class Raffle_model extends CI_Model {
    public function __construct() {
        parent::__construct();
    }

    public function add_raffle() {
        // This works
        $meta = array(
            'user_id'    => $this->session->userdata('user_id'),
            'name'       => $this->input->post('raffle-name'),
            'winners'    => $this->input->post('winners'),
            'created_ip' => $_SERVER['REMOTE_ADDR']
        );

        // This works and is a multidimensional array for insert_batch()
        $entrants = array(
            array(
                'date'      => date(DATE_ATOM),
                'raffle_id' => '1'
            )
        );

        foreach($_POST['entrant'] as $name => $n) {
            array_push($entrants,
                array(
                    'name'    => $n,
                    'tickets' => $_POST['tickets'][$name]
                )
            );
        }

        $this->db->insert('raffle', $meta);
        $this->db->insert_batch('entry', $entrants); // This one returns error 500

        return true;
    }
}

Here’s the problem: Submitting the form, the meta portion does get saved to the raffle table, but the entrants portion does not get saved to the entry table. I have tried using a simple dummy array (sample data, no post data, no loops) to see if it would work, but it still doesn’t. Console logs say POST http://rafflegrab.dev/raffle/save/ 500 (Internal Server Error).

CSRF is off in CI config.

The table is set up as follows:

  1. Table name: entry
  2. InnoDB
  3. id – bigint(12) – UNSIGNED – not_null – AUTO_INCREMENT – PRIMARY
  4. user_id – int(10) – UNSIGNED
  5. name – varchar(100)
  6. tickets – smallint(5) – UNSIGNED
  7. date – datetime
  8. raffle_id – int(10) – UNSIGNED
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:39:00+00:00Added an answer on June 5, 2026 at 4:39 pm

    I’m assuming you’re using unsafe input methods to be able to access $_POST ?

    What happens if you mock up a noddy form with those fields and post it to the Save action on the controller?

    Next question is the usual stuff but what do the server logs say? Have a look in the Event Log (windows) or /var/log/apache2/error_log (SUSE) or the equivalent on your system. It should tell you why it’s not working.

    If not, make sure your php log level is high enough to output errors.

    If you’re not in a production environment, consider displaying PHP errors until you’ve tracked down the problem

    Edit: Always better to know the problem than guess but my first thought is that your 2-dimensional array as inconsistent. You seem to have:

    array(
        array('date'=>blah, 'raffleId'=>blah),
        array('name'=>blah, 'tickets'=>blah),
        array('name'=>blah, 'tickets'=>blah)
        ...
    )
    

    Did you in fact want

    array(
        array('date'=>blah, 'raffleId'=>blah, 'name'=>blah, 'tickets'=>blah),
        array('date'=>blah, 'raffleId'=>blah, 'name'=>blah, 'tickets'=>blah),
        array('date'=>blah, 'raffleId'=>blah, 'name'=>blah, 'tickets'=>blah),
        ...
    )
    

    ? Perhaps something like:

        $entrants = array();
        $recordedDate = date(DATE_ATOM);
        foreach($_POST['entrant'] as $name => $n) {
            array_push($entrants,
                array(
                    'date'      => $recordedDate,
                    'raffle_id' => '1'
                    'name'    => $n,
                    'tickets' => $_POST['tickets'][$name]
                )
            );
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.