how to put javascript variable in php array [duplicate] - javascript

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 4 years ago.
I need to add JavaScript variable to PHP array, my code is below and var date1 is JavaScript variable.
Code
$date1="2018-08-02";
$date=explode("-",$date1);
My JavaSript section is here
var date1=new Date(<?=$date[0].",".$date[1].",".$date[2]?>);
date1 place to index of array x
$incoming[$i] = array("x" =>+date1 , "y" => $total_inc);
$outgoing[$i] = array("x" => +date1, "y" => $total_out);
In this situation the value is return 0.

You can't. JS code is executed after PHP finished executing own code. You can pass data from js to php by ajax or by setting a cookie.

Related

How do I pass variables and data from JavaScript to PHP? [duplicate]

This question already has answers here:
How do I edit PHP variable with JavaScript/jQuery?
(2 answers)
jQuery Ajax POST example with PHP
(17 answers)
Closed 2 years ago.
How do I pass variables and data from JavaScript to PHP?
like this:
var client_code = $('#client_code').val();
$client_code = client_code;
You can't pass data from JS to PHP directly. But you can use AJAX functions to pass the data as asynchronous.
You can read from https://www.w3schools.com/jquery/jquery_ref_ajax.asp

How to put a variable inside quotes JS [duplicate]

This question already has answers here:
How do I put a variable in a string using javascript
(4 answers)
jQuery passing element ID into jquery statement?
(6 answers)
How to interpolate variables in strings in JavaScript, without concatenation?
(17 answers)
Closed 4 years ago.
I'm trying to select a specific audio element based on the key that's pressed. The issue is the compiler is reading the variable as a string. How can I get it to read it as a variable?
var hail = e.keyCode
var afile = document.querySelector("audio[data-id='hail']")
You can use Javascript template strings :
var hail = e.keyCode
var afile = document.querySelector(`audio[data-id=${hail}]`)

How to echo a JS variable to php? [duplicate]

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
How to convert Decimal to Double in C#?
(14 answers)
Closed 4 years ago.
Is that possible to pass a JS variable to PHP ?
My code is :
document.getElementById('designation_' + i).value = '<?php echo $list[HERE]; ?>';
where I is in a for loop.
I want to echo the $list array in PHP with the index I of my JS (instead of HERE).
First, is that possible? If yes, how?
You can not pass the JavaScript variable up to PHP to index the $list array. What you can do is to pass the whole array down to JavaScript as JSON encoded and then index into that.
See: Convert php array to Javascript

Set dynamic value to Array in JS like PHP [duplicate]

This question already has answers here:
How to insert an item into an array at a specific index (JavaScript)
(28 answers)
Closed 7 years ago.
My question is how I will set a value an array in JS like PHP?
for example:
in php I have an array named Arr, I set a new value in n position using
Arr[] = value
but I try to do same in JS but display an error
Use the push command
Arr.push(value)

How to put this JS variable into PHP variable? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I would like to know how to make use of the variable hash in a PHP variable
var hash = md5(a);
Here is the equivalent in PHP:
$hash = md5($a);

Categories