Thursday, March 16, 2017

how to assign jquery variable to php variable in Laravel

Inside a toolbar.blade I have this:

<script>
$('.form-control').on('change',function(){
var TourDate = document.getElementById('TourDate').value;
document.cookie = " var1 = TourDate"
if($('#TourDate_hidden').val()=="" || $('#TourID').val()==""){
    return;
  }
keyword= '_kfdTourDate:equal:'+$('#TourDate_hidden').val()+'|';
keyword+= '_kfnTourID:equal:'+$('#TourID').val();

reloadData( '#',"/data?search="+keyword);    
console.log(TourDate);
});
</script>

Also I have a text input and a selectbox in toolbar.blade. the input and selectbox look like this:

    <input type="text" class="form-control" name="TourDate" 
id="TourDate" data-value="">

@if(count($tourTypes))
  <select name="_kfdTourID" id="TourID" onkeyup='saveValue(this);'>
    @foreach($tourTypes as $tourType)
      <optgroup label="">
          @if(count($tourType->tours))
          @foreach($tourType->tours as $tour)
          <option value=""></option>
          @endforeach
          @endif
      </optgroup>
    @endforeach
  </select>
@endif

When I click on text input, a boostrap calendar is poping up, after selecting a date it's showing the selected date inside the input and the page is reloading with the new filtered data trough ajax

e.g. `XHR finished loading: GET "http://www.website.com/bookings/data?search=_kfdTourDate:equal:2017-03-11|_kfnTourID:equal:4".

After the page is reloaded trough ajax the value inside the input is becoming empty and the first option in the selectbox is being selected automatically.

When I write

@php
        $testdate = "16/03/2017";
@endphp

above the input I can see the date 16/03/2017 inside the input. Also I can print the selected tour date using console.log(TourDate) Even after I refresh the page the date 16/03/2017 stays inside the input. So, my question is How can I keep the data-value for the input and mark the selected option as selected after the page is refreshed? Can I assign jquery variable TourDate to php variable testdate? It would be easier to show the last selected date in the input if it is possible.

I tried this: keep input value after refresh page but couldn't make it work. I tried almost everything the whole day. I desperately need help to make this work. Thanks in advance for help.



via hijacker83

Advertisement