2

I have a URL something like this : foo?country=usa&state=ny and in foo I have a form which gives me values of firstname & lastname and few more with a submit button.

But when i click submit it gives foo?firstname=john&lastname=doe And the other query i.e country and state get disappear or in other word i am losing them.

Can anyone please tell me how can i peserve them even when i am submiting a form ?

Please note: I can not use any hidden field because there are many forms in foo blade as well as as there can be many query that can come to foo blade not just country and state, there can be age, zipcode etc.

My form in foo.blade.php is like this

<form method="get" action ="{{Request::fullUrl()}}">
   <div class="form-group">
      <input type="text" class="form-control" name="firstname" placeholder="first name">
    </div>

    <button type="submit" class="btn btn-green-small">Apply</button>
</form>

    //Another form
 <form method="get" action ="{{Request::fullUrl()}}">

       <div class="form-group">
          <input type="text" class="form-control" name="lastname" placeholder="first name">
           </div>
        <button type="submit" class="btn btn-green-small">Apply</button>
    </form>
user7747472
  • 1,874
  • 6
  • 36
  • 80
  • You can append in the existing request. check this: https://stackoverflow.com/questions/37107903/laravel-is-there-a-way-to-add-values-to-a-request-array – tech2017 May 28 '18 at 20:03
  • @tech2017 That is adding value to request array but what i am asking is from the page – user7747472 May 28 '18 at 20:04

1 Answers1

0

Step 1 - You can hook an onSubmit event to your form using jQuery, there you can serialize() the form fields to generate a string of format www-form-urlencoded containing your firstname & lastname fields.

Step 2 - Since you have your country,state info residing on URL you can easily strip that off from window.location.href.

Then manually combine a new query string to append your request URL.

Example:

Say your form has id ="myform"

$("#myform").on("submit",function(){
  event.preventDefault(); //prevent submission
  let formData = $(this).serialize(); //outputs firstname=blah&lastname=moreblah

  let fullUrl = window.href.location;
  let queryPart = fullUrl.split("?")[1]; //here you have country=usa&state=ny

  let finalForm = queryPart + "&" formData;  //country=usa&state=ny&firstname=blah

  // submit here using 'finalForm' after your request endpoint
});

I omitted the null/empty checking for the sake of clarity.

alegria
  • 931
  • 2
  • 10
  • 25
  • I think this is the only way. But i am not sure how i can submit serialized array, or new form data without ajax. can you please add that too. As of now, with your example i am doing this way.But would love to know how its can be done that way `var formData=$(this).serialize(); var fullUrl = window.location.href; var finalUrl = fullUrl+"&"+formData; window.location.href = finalUrl;` – user7747472 May 28 '18 at 20:40
  • Is there any specific reason for not using AJAX ? I actually answered having that in mind lol. If you share your laravel get route I'll try to help. – alegria May 28 '18 at 20:46
  • This is basically a search page like an ecommerce site where u can search and then add filter's which are also shown in the url .That is the reason nothing else. But this answer helped me a lot thank you :) – user7747472 May 28 '18 at 20:50
  • Maybe you can tweak your Route to be like : `Route::get('search/{query}', 'Controller@method')->where('query','.+')` and in the controller method you handle the business. Once you're able to write generic URLs you can call `this.document.location.href = yourDomain + "/" + finalQuery; ` Since this will initiate full page load it may suit your need. – alegria May 28 '18 at 20:55
  • Yes, but some filter might present might not present some filters might have multiple value , filter poisition might be different.For eg country can have multiple values such as usa,uk etc that is why i think this is the only option :P – user7747472 May 28 '18 at 20:58
  • Then I would definitely handle this with AJAX. Wrapping my requests in infinitely nestable objects etc. Also IMHO I'd prefer an e-commerce site which is not making full page reloads on query ;) Believe me people do like tweaking those options. – alegria May 28 '18 at 21:03