Post by Joe1234 » Fri Mar 01, 2024 12:40 am

I have a mod that has multiple forms in it. Each form is destinguished by a checkbox and a submit button with a specific ID attached to it. So check X ID and click it's button, then X form was submitted. The problem is the browser is always registering the last form on the module. When I go into developer console to check the form data it also shows me the last form on the module is what was submitted. Here is the pertinent html form and control functions. If more is needed let me know, thanks.

Form

Code: Select all

              {% for task in tasks %}
                <tr>
                  <td class="text-left">
                    <form action="{{ edit }}" method="post" id="form-edit-{{ task.task_id }}" style="width:100%;">
                      <div style="padding:0; float:left; width:5%;">
					  
					  {{ task.task_id }} <!--THIS IS VISUALLY SHOWING ME ON THE FORM ID BEING CHECKED IS CORRECT-->
					  
                        {% if task.task_id in selected %}
                          <input type="checkbox" name="dashboard_task_delete" value="1" class="form-control" style="margin-top:12px; float:left;" checked="checked" />
                        {% else %}
                          <input type="checkbox" name="dashboard_task_delete" value="0" class="form-control" style="margin-top:12px; float:left;" />
                        {% endif %}
                        <input type="text" name="dashboard_task_id" value="{{ task.task_id }}" id="dashboard_task_id" class="form-control" style="float:left; width:28px; visibility:hidden;" />
                      </div>
                      <div style="padding:0 12px; float:left; width:15%;">
                        <select name="dashboard_task_status" id="input-task-status" class="form-control">
                          {% if task.task_status == 2 %}
                            <option value="2" selected="selected">{{ text_task_completed }}</option>
                            <option value="1">{{ text_task_pending }}</option>
                            <option value="0">{{ text_task_inactive }}</option>
                          {% elseif task.task_status == 1 %}
                            <option value="2">{{ text_task_completed }}</option>
                            <option value="1" selected="selected">{{ text_task_pending }}</option>
                            <option value="0">{{ text_task_inactive }}</option>
                          {% elseif task.task_status == 0 %}
                            <option value="2">{{ text_task_completed }}</option>
                            <option value="1">{{ text_task_pending }}</option>
                            <option value="0" selected="selected">{{ text_task_inactive }}</option>
                          {% endif %}
                        </select>
                      </div>
                      <div style="padding:0 12px; float:left; width:50%;">
                        <input type="text" name="dashboard_task_description" value="{{ task.task_description }}" placeholder="{{ task.task_description }}" id="input-task-description" class="form-control" />
                      </div>
                      <div style="padding:0 12px; float:left; width:20%;">
                        <input type="datetime" name="dashboard_task_deadline" value="{{ task.task_deadline }}" placeholder="{{ task.task_deadline }}" id="input-task-deadline" class="form-control" />
                      </div>
                      <div style="padding:0 12px; float:left; width:10%;">
                        <button type="submit" form="form-edit-{{ task.task_id }}" formaction="{{ edit }}" class="btn btn-warning" id="dashboard_task_edit" data-toggle="tooltip" title="{{ button_edit }}">
                        
						{{ task.task_id }} <!--THIS IS VISUALLY SHOWING ME ON THE FORM ID BEING SUBMITTED IS CORRECT-->
						
						 <span class="fa fa-save"></span>
                        </button>
                      </div>
                    </form>
                  </td>
                </tr>
              {% endfor %}

controller (I stripped some of the visual aspects of the index away so it's easier to see the main components)

Code: Select all

	public function index() {
		$this->load->language('extension/dashboard/tasks');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('setting/setting');
		$this->load->model('extension/dashboard/tasks');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('dashboard_tasks', $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('extension/dashboard/tasks', 'user_token=' . $this->session->data['user_token'] . '&type=module', true));
		}

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		$data['action'] = $this->url->link('extension/dashboard/tasks', 'user_token=' . $this->session->data['user_token'], true);
		$data['add'] = $this->url->link('extension/dashboard/tasks/add', 'user_token=' . $this->session->data['user_token'], true);
		$data['edit'] = $this->url->link('extension/dashboard/tasks/edit', 'user_token=' . $this->session->data['user_token'], true);
		$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=dashboard', true);

		if (isset($this->request->post['dashboard_tasks_width'])) {
			$data['dashboard_tasks_width'] = $this->request->post['dashboard_tasks_width'];
		} else {
			$data['dashboard_tasks_width'] = $this->config->get('dashboard_tasks_width');
		}

		$data['columns'] = array();

		for ($i = 3; $i <= 12; $i++) {
			$data['columns'][] = $i;
		}

		if (isset($this->request->post['dashboard_tasks_status'])) {
			$data['dashboard_tasks_status'] = $this->request->post['dashboard_tasks_status'];
		} else {
			$data['dashboard_tasks_status'] = $this->config->get('dashboard_tasks_status');
		}

		$data['user_token'] = $this->session->data['user_token'];

		// List tasks
		$data['tasks'] = array();

		$filter = 'task_id';

		$results = $this->model_extension_dashboard_tasks->getTasks($filter);

		foreach ($results as $result) {
			$data['tasks'][] = array(
				'task_id'    				=> $result['task_id'],
				'task_description' 	=> $result['task_description'],
				'task_status' 			=> $result['task_status'],
				'task_deadline' 		=> $result['task_deadline'],
			);
		}

		$this->response->setOutput($this->load->view('extension/dashboard/tasks_form', $data));
	}


	public function edit() {
		$this->load->model('extension/dashboard/tasks');

	//	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {

			$task_id = $this->request->post['dashboard_task_id'];
			$status = $this->request->post['dashboard_task_status'];
			$description = htmlentities($this->request->post['dashboard_task_description'], ENT_QUOTES);
			$deadline = $this->request->post['dashboard_task_deadline'];
$this->log->write($task_id);
			if (isset($this->request->post['dashboard_task_delete'])) {
				$this->model_extension_dashboard_tasks->deleteTask($task_id);
			} else {
				$this->model_extension_dashboard_tasks->editTask($task_id, $status, $description, $deadline);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('extension/dashboard/tasks', 'user_token=' . $this->session->data['user_token'] . '&type=module', true));
	//	}
	}
Last edited by Joe1234 on Mon Mar 04, 2024 6:50 am, edited 1 time in total.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by Joe1234 » Fri Mar 01, 2024 3:06 am

I went about it a slightly different way instead of using the looping forms and am getting the results I expect, but if anyone is interested in looking into this, I'd still like to know why this isn't working the way I think it should.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by ADD Creative » Fri Mar 01, 2024 6:06 pm

You are using the same IDs for some of the form elements. IDs should be unique. You don't need the form and formaction on the button, if the button is in the form. These should't make a difference as the names are used to submit per form.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by Joe1234 » Fri Mar 01, 2024 11:12 pm

ADD Creative wrote:
Fri Mar 01, 2024 6:06 pm
You are using the same IDs for some of the form elements. IDs should be unique.
No not the same IDs, I thought so as well, but they are different IDs iterated within the loop. I printed the IDs on the page within each form beside the elements to verify and make sure. And hypothetically, if they were the same IDs I would expect all the elements to be the same as the first, not the last form IDs.
ADD Creative wrote:
Fri Mar 01, 2024 6:06 pm
You don't need the form and formaction on the button, if the button is in the form. These should't make a difference as the names are used to submit per form.
I went a different route with it, but I'll look back at this part to experiment. Thanks

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by ADD Creative » Fri Mar 01, 2024 11:26 pm

Not the form IDs, the element IDs. For example id="dashboard_task_id", id="input-task-status" which will be repeated in each form. IDs must be unique withing the DOM.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by Joe1234 » Mon Mar 04, 2024 1:23 am

Good to know, thanks. I didn't see that as an issue since they fell within the form being submitted at the time.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am
Who is online

Users browsing this forum: No registered users and 18 guests