1

I want to send the date parameter to php but don't know how to do it

var date = new Date();
var day = date.getFullYear();

How do I echo the var day in php ?

bharatk
  • 4,202
  • 5
  • 16
  • 30
Joe
  • 21
  • 2
  • Learn [how to use code block](http://stackoverflow.com/editing-help) – Circle Hsiao Sep 24 '19 at 02:59
  • Put variables in the form and submit it or send via ajax. – Chaska Sep 24 '19 at 03:00
  • 4
    Possible duplicate of [How do I pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript) & [How can I use a JavaScript variable as a PHP variable?](https://stackoverflow.com/questions/2379224/how-can-i-use-a-javascript-variable-as-a-php-variable) – Nishal K.R Sep 24 '19 at 08:02

4 Answers4

0

You need to create a function in PHP callable from Ajax. But there is no way for you to "see" the echo directly. Don't forget that PHP is executed on the SERVER side, and Javascript on the Client side. So they can't exchange information directly. Instead, you can get the HTML result of an ajax query. There is an exemple in the jquery documentation.

Ajax doc

Eiji
  • 454
  • 6
  • 15
0

If you just want to echo javascript variable in php without ajax follow this

<script>
    var date = new Date();
    var day = date.getFullYear();
</script>

<?php echo "<script>document.write(day)</script>"; ?>

you can verify this code at http://phpfiddle.org/

  • On the first look this might seem to work, but this does not echo the JavaScript variable in php, nor does it send the value of `day` to php. Writing `` without the `` would be equivalent to the shown code. @Joe – t.niese Sep 30 '19 at 17:25
0

Set value in this input box using the Javascript

<script>
    var date = new Date();
    var day = date.getFullYear();
    document.getElementById("dateParam").value=day;    
</script>

Here is the form

<form action="yourPHPfile" method="post">
    <input type="text" id="dateParam" name="day"/>
    <input type="submit">
</form>

When you will submit this you will get the data in as a parameter.

In your PHP file

if(isset($_POST['day'])){
 echo $_POST['day'];
}

So you can try this.

Puneet Sharma
  • 305
  • 3
  • 14
  • If we say if is set($_POST[day]) ,day is the input name but the submit button does not have a name – Joe Sep 25 '19 at 03:38
  • Hey Joe, We did not need the name of submit button we need only the input text name that why I did not give any name to submit button. I have changed the "stype" to "type" its a typo in the submit button. – Puneet Sharma Sep 25 '19 at 04:01
  • If this answer helpfull Please rate me up – Puneet Sharma Oct 07 '19 at 17:11
-1

You need to use Ajax because php is server side and javascript is client side so there is only one way to send parameter to php is Ajax, Ajax is a bridge of client to server-side.

sid busa
  • 191
  • 1
  • 5