Yeru
Yeru

Reputation: 115

how to passing datepicker value to controller?

i new to use laravel framework,, i want to passing datepicker value after user select the date

i try to give button, to submit that value, but i got error. Anybody have idea to passing value without button ?

this is my blade :

<form id="myform" name="myform">
<input type="text" id="datepicker" name="datepicker" value="Date"/>
<input placeholder="Submit" type="button" id="submitMe" style="width: 
100px;"/>
//can i passing this value without button?
<script>
$('#datepicker').datepicker({
format : 'yyyy-mm-dd'
}); 
</script>
</form>

<script>
$("#submitMe").click(function() {
$.ajax({
type: 'POST',
url: "/index/getDate",
method: "POST",
dataType: "json",
data: $('#myform').serialize(),
success: function(data) {
console.log("Done");
}
});
return false;
})
</script>

this is my route :

Route::post('index/getDate', 'UserController@getDate')- 
>name('Usercontroller.getDate');

this is my controller :

function getDate(Request $request){
$date = urldecode ($_GET['datepicker']);
echo "chosen date is: ".$date;
}

i try to get output like this :

enter image description here

thankyou so much, if somebody want to help me :))

Upvotes: 0

Views: 3006

Answers (2)

Kapitan Teemo
Kapitan Teemo

Reputation: 2164

try this in your controller:

function getDate(Request $request){
   $date = $request->datepicker;
   return "chosen date is: ".$date;
}

then in your view:

<input type="text" id="datepicker" name="datepicker" value="Date"/>
<label id="dateSelected"></label>

<script>
   $("#submitMe").click(function() {
       $.ajax({
          type: 'POST',
          url: "/index/getDate",
          method: "POST",
          data: $('#myform').serialize(),
          success: function(data) {
             console.log(data);
             $('#dateSelected').text(data);
          }
       });
       return false;
    })
</script>

Upvotes: 1

shirne
shirne

Reputation: 81

you pass the form data with POST method, so you can get the parameter with

$request->input('datepicker');

Upvotes: 0

Related Questions