Tushar
Tushar

Reputation: 166

HTML form onSubmit attribute issue with location.href

I have a form whose opening tag looks like this

<form name="adv_search_form" action="" onSubmit="location.href = location.href.split('#')[0]+'&adv_search=adv_search'" method="POST">

actually, the value of location.href is http://localhost/projectcode12may2014/ampanel/index.php?rel=common_listing&module=company#

I want to append a &adv_search=adv_search to the url when the form is submitted, that too after removing the # from the url to make it $_GETable. So when I do

<form name="adv_search_form" action="" onSubmit="location.href = location.href+'&adv_search=adv_search'" method="POST">

things work pretty well but the hash persists in the url. So I had used the form opening tag as the former one. But when I do so, nothing happens and the url doesn't change at all. Can anyone help me out on that?

Upvotes: 0

Views: 884

Answers (3)

Tushar
Tushar

Reputation: 166

I got the solution of my problem. The url was changing but it was being overwritten by the action when the form gets submitted. I had to deprecate the default behavior of the form by adding return false in the onSubmit attribute. Anyways, Thanks for your answers.

Upvotes: 0

thirkan
thirkan

Reputation: 26

use the substring-method to eliminate the '#' at the end:

location.href = location.href.substring( 0, location.href.length-1 ) +
                '&adv_search=adv_search';

Upvotes: 1

Stefan
Stefan

Reputation: 1258

I suggest that you handle with PHP the correct onSubmit URL and don't rely on the client location.href for that. Can result in unwanted behavior.

If you really want it then you can use str.replace() for this.

original = location.href+'&adv_search=adv_search';
original.str_replace('#', '');

Upvotes: 0

Related Questions