This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have that code:
function saveData(position, map) {
var latlng = marker.getPosition();
var late = latlng.lat();
var longe = latlng.lng();
<?php
require("sql.php");
session_start();
$name = 'prova';
$address = 'temare';
$type = 'bar';
$late = "<script>document.getElementByID('late').value</script>";
$longe ="<script>document.getElementByID('longe').value</script>";
I need to get late and longe variables in PHP but this doesn't work, anyone knows how I can do it?
You can't access javascript variables using PHP as JavaScript is executed on client side ( browser ) and PHP on server side.
The best what you can do is send data from those variables as request to server and read them, for example using jQuery ajax request:
Good example you can find here:
jQuery Ajax POST example with PHP
Or you can use this:
javascript
function sendData(variable1, variable2) {
$.ajax({
type: 'post',
url: 'myphpscript.php'
data: 'var1='+variable1+'&var2='+variable2
});
myphpscript.php
<?php
$variable1 = $_POST['var1'];
$variable2 = $_POST['var2'];
<script type="text/javascript">
<?php $abc = "<script>document.write(late)</script>"?>
<?php $def = "<script>document.write(longe)</script>"?>
</script>
Related
This question already has answers here:
how to assign javascript variable value to php variable [duplicate]
(11 answers)
Closed 4 years ago.
i'm using a onchange function to post value to ajax.
function viewTest() {
jQuery("#empy_card").hide()
var test_type= jQuery('#test_type').val();
var myvals = jQuery("#table").html('');
jQuery.ajax({
type: "POST",
url: "test.php?smt=command",
dataType: 'json',
data: {test_type:test_type},
success: function(data){
//alert(data.date);
//alert(data.book);
<?php
$date = data.date;
$book = data.book;
?>
}
});
}
its work successfully. i got in alert form data.date
"18-Dec-10","20-Apr-11"
i got in alert form data.book
"book A","book C"
test.php
<?php
if(isset($_GET['smt'])=='command'){
$test= $_POST['book_name'];
include('connection.php');
$data=array();
$result ="SELECT * FROM books WHERE Type_of_books='$test'";
$ftc = mysqli_query($conn,$result)or die(mysqli_error($conn));
while($row = mysqli_fetch_array($ftc)) {
$data['date'][] ='"'.$row['date'].'"';
$data['book'][] = $row['name'];
}
echo json_encode($data);
}
?>
how can i get this data to php variable like $dates = data.date; And $book = data.book
Why are you using php in Ajax and you are assigning JavaScript variable to php. If you really want that response use cookies or session variables.
Remember that your PHP code is evaluated by the server, not the client. That means that you can access your $date and $year in various ways.
Server side (PHP)
You have to delete your Ajax request and write it using PHP, then you can access the variables.
Client side (JavaScript)
You already got them in data!
This question already has answers here:
how to convert javascript variable to PHP variable? [closed]
(2 answers)
How to get JavaScript function data into a PHP variable
(5 answers)
Closed 9 years ago.
i am trying to get the javascript variable value to php.
here is my javascript code:
function getResults()
{
var radios = document.getElementsByName("address");
for (var i = 0; i < radios.length; i++)
{
if (radios[i].checked) {
var a = radios[i].value
alert(a);
break;
}
}
}
from this javascript i want to get variable a's value inside php code onclick on submit button.
how can i do this?
i tried this
$var1 = $_GET["a"];
Do this..
$var1 = <?=$_GET["a"]?>;
to communicate a value from JavaScript to PHP do following
$var1 = <?=$_POST["a"]?>;
What you tried is almost correct:
var a = <?=$_GET["a"]?>;
Since javascript code runs on client-side, but PHP is server-side code, you need to send your JS variables to the server. You can do this using AJAX.
Hint: AJAX calls usually use POST instead of GET.
Replace:
$var1 = $_GET["a"];
on:
$var1 = <?=$_POST["a"]?>;
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I want to set session inside the javascript. But all the time this function return false value. Please tell me the error of this code.
<script>
function myFunction(){
var r=confirm("Would you like to add another item ?");
if (r==true) {
alert('ok');
var varname = '<?php echo $_SESSION["redirect"]=1; ?>';
}
if(r==false) {
alert('bad');
var varname = '<?php echo $_SESSION["redirect"]=2; ?>';
}
}
</script>
You can't evaluate PHP on the client. Do an AJAX request to the server setting the session variable instead.
Javascript runs on the client side, so you'll need to perform an AJAX call to a server side script that will change the sessions value.
Something in the lines of:
function changeSession()
{
$.ajax({
url: /change_session.php?value=1&key=redirect,
dataType:"html"
});
}
And in change_session.php:
<?php $_SESSION[$_REQUEST['key']]=$_REQUEST['value'] ?>
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
Why does this not work?
var data1 = "<? http_build_query($_GET); ?>";
var data2 = "buy.php?";
var url = data2+data1
document.getElementById('framebox').src = url;
Thanks.
Because data1 is empty (PHP is not outputing anything), try:
var data1 = "<?= http_build_query($_GET); ?>"; // or
var data1 = "<?php echo http_build_query($_GET); ?>";
Any reason you're using PHP to build the query string instead of doing it directly in Javascript?
You can also archive it the plain old pure javascript way:
var data1 = location.href.split('?').pop();
var data2 = "buy.php?";
var url = data2+data1
document.getElementById('framebox').src = url;
But it's much more fun to mix stuff.....
This question already has answers here:
Convert php array to Javascript
(21 answers)
Closed 9 years ago.
I would like to concatenate a php array into javascript array.
And use all of this to create jquery function.
Help me...
Instructions for in the loop are => descriptif[i] = .$description[i].;
<?php echo '<script> var descriptif = new Array ();
for(i=0 ; i<16 ; i++)
{
descriptif[i] = "'.$description["'"+ i +"'"].'";
}
</script>';
?>
You cannot access PHP arrays using client-side Javascript.
PHP is a server-side language. Everything happens on the server.
Javascript is a client-side language. Everything happens in the browser.
I guess you're thinking about:
<script> var descriptif = new Array ();
<?php for(i=0 ; i<16 ; i++) {
echo "descriptif[$i] = '$description[$i]';"
} ?>
</script>
It will print:
<script> var descriptif = new Array ();
descriptif[0] = value1;
descriptif[1] = value2;
descriptif[2] = value3;
...
</script>
PS if you want to dynamize something (javascript, css ,etc.) with a scripting language (php, jsp), use , <% %> only in the parts that differ (the loop part). The rest is the same so it doesn't have to be echoed - it makes your code less clear.