1
<script>
$(document).ready(function(){
$("#date").click(function(){
  start = $("#date_start").val();
  end = $("#date_end").val();
});
});
</script>

<input name='date_A' type='text' id='date_start' />
<input name='date_B' type='text' id='date_end' />
<input type="button" id="date" class="button" value="check" />

The question is how to get value of "start" and "end" from the jquery to put that in PHP variable without post it first to database?

<?php
   $start_date = "";
   $end_date = "";
?>

The both variables above need the value of the variable "start" and "end" from jquery above

Cœur
  • 37,241
  • 25
  • 195
  • 267
awanlamp
  • 11
  • 3

3 Answers3

0
$(document).ready(function(){
$("#date").click(function(){
  start = $("#date_start").val();
  end = $("#date_end").val();

  $.ajax({
        type: 'post',
        url: '/your_page_link/',
        data: {start : start, end : end},
        success: function (res) {

        }
    });
});
});

In PHP:

<?php
if(!empty($_POST)){ // If you are using same page, then it'll help you to detect ajax request.
   $start_date = $_POST['start'];
   $end_date = $_POST['end'];
}
?>
RNK
  • 5,582
  • 11
  • 65
  • 133
0

I fear you want to read values from jQuery and inject them in PHP in the same page, which is impossible. You have to understand what PHP is first. It generates the page, that is then sent to the browser, that then executes jQuery code. PHP code completely disappeared from the generated page. jQuery has no idea of PHP's presence, they just can't interact this way. You must use a GET or POST query in order to send values to a PHP page, back to the server.

You want to read things like this : Difference between Javascript and PHP

Community
  • 1
  • 1
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
0

This is what you have to do.

        <form id="form" name="form" action="" method="post">
        <div id="block">
            <input type="text" name="startDate" id="startDate" placeholder="Start date" required/>
            <input type="text" name="endDate" id="endDate" placeholder="End date" required />
            <input type="submit" id="Download" name="Download" value="Download"/>
        </div>
    </form>

and put this code in the same page

<?php
        $startDate = $_POST['startDate'];
        $endDate   = $_POST['endDate'];

?>
Puya Sarmidani
  • 319
  • 2
  • 7