Incorrect URL when changing dropdown value

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Incorrect URL when changing dropdown value

Post by Kailey »

I'm ripping my hair out over this one and can't figure out why. What's happening is when I change the dropdown from the default (0), the URL is incorrect. The value doesn't stick either as the default is selected.

Expected result: http://www.site.local/traq/project?p=2&s=3 (3 is just an example, it corresponds to values in my DB)
Actual result: http://www.site.local/traq/project?ticket_status=3

HTML:

Code: Select all

<div class="search-box">
	<form id="ticket_status_form" action="{U_FILTER}" method="get"><div>
		<label for="ticket_status">
			<strong>{L_FILTER_TICKETS}{L_COLON}</strong>
		</label>
		<select id="ticket_status" name="ticket_status" onchange="document.getElementById('ticket_status_form').submit()">
			<option value="-1"<!-- IF STATUS_ID == -1 --> selected="selected"<!-- ENDIF -->>{L_ALL_TICKETS}</option>
			<option value="0"<!-- IF STATUS_ID == 0 --> selected="selected"<!-- ENDIF -->>{L_ALL_OPEN}</option>
			<option value="-2"<!-- IF STATUS_ID == -2 --> selected="selected"<!-- ENDIF -->>{L_ALL_CLOSED}</option>
			<!-- IF .ticket_status -->
			<!-- BEGIN ticket_status -->
			<option value="{ticket_status.ID}"<!-- IF ticket_status.ID == STATUS_ID --> selected="selected"<!-- ENDIF -->>{ticket_status.NAME}</option>
			<!-- END ticket_status -->
			<!-- ENDIF -->
		</select>
		<noscript><div style="display: inline;"><input type="submit" name="submit" value="submit" class="button2" /></div></noscript>
	</div></form>
</div>
Relevant PHP:

Code: Select all

$project_id = $this->request->variable('p', 0);
$status_id = $this->request->variable('s', 0);

$sql = 'SELECT project_name, tracker_id
	FROM ' . $this->traq_project_table . '
	WHERE project_id = ' . (int) $project_id;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
	$project_name = $row['project_name'];
	$tracker_id = $row['tracker_id'];
}
$this->db->sql_freeresult($result);

switch ($tracker_id)
{
	case '0':
		$traq = 'BUG_TRAQ';
	break;

	case '1':
		$traq = 'FEATURE_TRAQ';
	break;

	case '2':
		$traq = 'ISSUE_TRAQ';
	break;
}

$sql = 'SELECT *
	FROM ' . $this->traq_status_table . '
	WHERE tracker_id = ' . (int) $tracker_id;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
	$this->template->assign_block_vars('ticket_status', array(
		'ID'	=> $row['status_id'],
		'NAME'	=> $row['status_name'],
	));
}
$this->db->sql_freeresult($result);

$this->template->assign_vars(array(
	'PROJECT_NAME'	=> $project_name,
	'STATUS_ID'		=> $status_id,

	'S_DISPLAY_POST_BUTTON'	=> $this->auth->acl_get('u_traq_post') ? true : false,

	'U_FILTER'			=> $this->helper->route('kinerity_traq_main_controller', array('page' => 'project', 'p' => (int) $project_id, 's' => (int) $status_id)),
	'U_POST_NEW_ISSUE'	=> ($this->auth->acl_get('u_traq_post')) ? $this->helper->route('kinerity_traq_main_controller', array('page' => 'posting', 'mode' => 'post')) : '',
));

return $this->helper->render('project_body.html', $this->user->lang($traq) . ' - ' . $project_name);
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Incorrect URL when changing dropdown value

Post by mrgoldy »

That's cause you're using the in-line javascript.
It adds the form's data to the url, and your form data is just the select box, named "ticket_status".
So it is not using the action="" attribute but its own generated url.
Also, I do not believe there is a 'return' value/response for the js's .submit()

Alernatively, you can just use jQuery.

Code: Select all

(function($) {

'use strict';

$(function() {
	$('#ticket_status').on('change', function() {
		$.ajax({
			url: $('#ticket_status_form').attr('action'),
			type: 'POST',
			data: { ticket_status: $(this).find(':selected').val() },
			/*success: function(response) {
				...
			},
			error: function(response) {
				...
			},*/
		});
	});
});

}) (jQuery);
(not tested and wrote it on the fly)
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Incorrect URL when changing dropdown value

Post by Kailey »

posey wrote: Sun Mar 04, 2018 11:36 pm (not tested and wrote it on the fly)
Well, it allows me to change the filter (thank you!), but it doesn't update to show the correct data in the url. It stays at http://www.site.local/traq/project?p=2 without adding the &s=3 value. Is there a way to resubmit the page with the expected url when I change the filter? I feel like I'm close.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Incorrect URL when changing dropdown value

Post by mrgoldy »

You can probably use phpBB's core.js functions for that.
Have a look here: https://github.com/phpbb/phpbb/blob/f20 ... re.js#L708
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Incorrect URL when changing dropdown value

Post by Kailey »

Hmm, still not getting it. Sorry, my js and jquery skills are lacking.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Incorrect URL when changing dropdown value

Post by mrgoldy »

Well, it depends on what you want to do?
Are you fine with reloading the page entirely or do you want to show the content entirely through jQuery?
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Incorrect URL when changing dropdown value

Post by Kailey »

Reloading the page entirely is fine.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Incorrect URL when changing dropdown value

Post by mrgoldy »

Alright, as I PM'd you, I could have a look at the code so I can play around with it myself and get it working.
As I do not know the definitive answer, or what should definetely make it work.

Some useful links:
https://github.com/phpbb/phpbb/blob/f20 ... re.js#L237
https://wiki.phpbb.com/JavaScript_Funct ... bb.ajaxify
https://wiki.phpbb.com/Using_AJAX

An example would be;

Code: Select all

$json_response = new \phpbb\json_response;

$json_response->send(array(
	'REFRESH_DATA'	=> array(
		'url'	=> $url,
		'time'	=> 3
	),
));
But I believe that would also require that the form has a data-refresh="true" attribute.
But then it might also need a data-ajax attribute, either with ="true" or with your own callback ="kinerity_traq_example".
That would also mean you will have to add a custom callback in jQuery (see: https://github.com/phpbb/phpbb/blob/f20 ... re.js#L901)
example:

Code: Select all

phpbb.addAjaxCallback('kinerity_traq_example', function(response) {
	// ...
});
Like I said, I do not know the definitive answer, above are just some 'things' to fiddle with ;-)

----

Another option would be to change the form action to exclude the &s= parameter and do something like this, but do not now how fool-proof this is and whether it follows the best coding practices.

Code: Select all

(function($) {

'use strict';

$(function() {
	$('#ticket_status').on('change', function() {
		window.location.replace($('#ticket_status_form').attr('action') + "&s=" + $(this).find(':selected').val());
	});
});

}) (jQuery);
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Incorrect URL when changing dropdown value

Post by mrgoldy »

Alright, I got it working.

HTML:

Code: Select all

<form id="ticket_status_form" action="{U_FILTER}" method="post" data-ajax="true" data-refresh="true"><div>
jQuery:

Code: Select all

(function($) {

'use strict';

$(function() {
	$('#ticket_status').on('change', function() {
		$('#ticket_status_form').submit();
	});
});

}) (jQuery);
PHP:

Code: Select all

$ticket_status = $this->request->variable('ticket_status', 0);

if ($ticket_status)
{
	$json_response = new \phpbb\json_response;

	$json_response->send(array(
		'REFRESH_DATA'	=> array(
			'url'	=> $this->helper->route('kinerity_trackers_main_controller', array('page' => 'projects', 'p' => (int) $project_id, 's' => $ticket_status), false),
			'time'	=> 0,
		),
	));
}
Note the false in the helper->route, otherwise the &amp; is not properly parsed to a &.
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Incorrect URL when changing dropdown value

Post by Kailey »

Thanks! I'll give it a go later.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Incorrect URL when changing dropdown value

Post by mrgoldy »

Forgot to mention that I exuded the s parameter from the Form action url on php-side.

Anyways, you got it working?
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Incorrect URL when changing dropdown value

Post by Kailey »

I haven't had a chance to test it. I should have time later today.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Incorrect URL when changing dropdown value

Post by Kailey »

Just gave it a quick go. I get this: https://dev.project-w.org/trackers/proj ... t_status=0 <-- working development board

It should be grabbing project_id 1 and whatever ticket status I select. Current code is here.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Incorrect URL when changing dropdown value

Post by mrgoldy »

You have a different refesh data url. I meant to omit the s parameter from the FORM action, that is assigned to the template further down.
Otherwise I can send the adjusted code back,later tonight.
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Incorrect URL when changing dropdown value

Post by Kailey »

OK, I fixed that part but it's still pulling the project_id as 0 and still giving me that REFRESH_DATA code page.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
Post Reply

Return to “Extension Writers Discussion”