Incorrect URL when changing dropdown value

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

Incorrect URL when changing dropdown value

Post by kinerity » Sun Mar 04, 2018 8:04 pm

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 Truscott - Community Team

User avatar
mrgoldy
Jr. Extension Validator
Posts: 739
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Incorrect URL when changing dropdown value

Post by mrgoldy » Sun Mar 04, 2018 11:36 pm

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)
Formerly known as posey.
''Proud member of the Studio"

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1972
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Incorrect URL when changing dropdown value

Post by kinerity » Mon Mar 05, 2018 2:50 am

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 Truscott - Community Team

User avatar
mrgoldy
Jr. Extension Validator
Posts: 739
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Incorrect URL when changing dropdown value

Post by mrgoldy » Mon Mar 05, 2018 9:16 am

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
Formerly known as posey.
''Proud member of the Studio"

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1972
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Incorrect URL when changing dropdown value

Post by kinerity » Mon Mar 05, 2018 2:14 pm

Hmm, still not getting it. Sorry, my js and jquery skills are lacking.
Kailey Truscott - Community Team

User avatar
mrgoldy
Jr. Extension Validator
Posts: 739
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Incorrect URL when changing dropdown value

Post by mrgoldy » Mon Mar 05, 2018 2:24 pm

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?
Formerly known as posey.
''Proud member of the Studio"

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1972
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Incorrect URL when changing dropdown value

Post by kinerity » Mon Mar 05, 2018 2:29 pm

Reloading the page entirely is fine.
Kailey Truscott - Community Team

User avatar
mrgoldy
Jr. Extension Validator
Posts: 739
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Incorrect URL when changing dropdown value

Post by mrgoldy » Mon Mar 05, 2018 2:43 pm

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);
Formerly known as posey.
''Proud member of the Studio"

User avatar
mrgoldy
Jr. Extension Validator
Posts: 739
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Incorrect URL when changing dropdown value

Post by mrgoldy » Mon Mar 05, 2018 11:18 pm

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 &.
Formerly known as posey.
''Proud member of the Studio"

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1972
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Incorrect URL when changing dropdown value

Post by kinerity » Mon Mar 05, 2018 11:28 pm

Thanks! I'll give it a go later.
Kailey Truscott - Community Team

User avatar
mrgoldy
Jr. Extension Validator
Posts: 739
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Incorrect URL when changing dropdown value

Post by mrgoldy » Wed Mar 07, 2018 9:54 pm

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

Anyways, you got it working?
Formerly known as posey.
''Proud member of the Studio"

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1972
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Incorrect URL when changing dropdown value

Post by kinerity » Thu Mar 08, 2018 1:53 pm

I haven't had a chance to test it. I should have time later today.
Kailey Truscott - Community Team

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1972
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Incorrect URL when changing dropdown value

Post by kinerity » Thu Mar 08, 2018 2:14 pm

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 Truscott - Community Team

User avatar
mrgoldy
Jr. Extension Validator
Posts: 739
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Incorrect URL when changing dropdown value

Post by mrgoldy » Thu Mar 08, 2018 3:45 pm

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.
Formerly known as posey.
''Proud member of the Studio"

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1972
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Incorrect URL when changing dropdown value

Post by kinerity » Thu Mar 08, 2018 3:55 pm

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 Truscott - Community Team

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: margau and 12 guests