Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Http Get url from a html form via jquery

Tags:

html

jquery

forms

Is there any way to retrieve a from url with out submitting it, some how simulating a form submission via HTTP Get. I mean for this example

<from id="frm" method="POST" action='someaction'>
  <input type='text' id='txt1' value='Hello'/>
<form>

I want to get bellow string without submitting form

someaction?txt1=Hello
like image 348
Reza Avatar asked Jan 10 '23 21:01

Reza


1 Answers

Here is the solution:

HTML:

<form id="frm" method="POST" action='someaction'>
  <input type='text' id='txt1' value='Hello'/>
  <input type='text' id='txt2' value='World'/>
</form>

Your GET URL is: <div id="url"></div>

Javascript (using jQuery):

var url = $("#frm").attr("action") + "?";
var urlElements = [];
$("#frm").children().each(function(){
    urlElements.push($(this).attr("id") + "=" + $(this).attr("value"));
});
urlElements = urlElements.join("&");
url += urlElements;
$("#url").html(url);

You can test it here: http://jsfiddle.net/3S9db/

Hope this help :)

like image 129
Johny Avatar answered Jan 17 '23 10:01

Johny