Comrade Raj
Comrade Raj

Reputation: 1

Submit a Form to Custom URL in Laravel 4

I have a simple form which allows the User to enter from_date and to_date.Lets say a user enters 2014-09-01 and 2014-09-10 respectively.

How can I get this form submit to a URL ../from_date/2014-09-01/to_date/2014-09-10

Upvotes: 0

Views: 458

Answers (2)

Superfly
Superfly

Reputation: 601

To change the URL once the page has already loaded, you will need to use javascript, as you won't be able to do this using Laravel/PHP because you need access to the dates, which are only selected after the page has loaded.

If your form is similar to:

<form id="dateForm" onsubmit="return submitDateForm()">
  <input type="text" id="from_date" name="from_date">
  <input type="text" id="to_date" name="to_date">
</form>

Assuming you are using POST for the form submission, and have already imported jQuery, then insert into your view (or a separate .js file which you import) the following jQuery:

function submitDateForm() {
  var from_date = $( '#from_date' ).val();
  var to_date = $( '#to_date' ).val();

  //Send the request
  var request = $.post("../from_date/" + from_date + "/to_date/" + to_date);

  //prevent the form from submitting itself normally
  return false;
}

Upvotes: 0

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

You cannot do that but if you need to do something like this, you need to submit to standard class Controller and then resubmit it using redirection:

public function resubmit() {
   Redirect::to('/from_date/'.Input::get('from_date').'/to_date/'.Input::get('to_date'))->withInput();
}

But to be honest I don't know why you try to do that. Usually you post data to static url and display content using dynamic urls with pretty urls.

Upvotes: 1

Related Questions