How to get javascript variable to php [duplicate] - php

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"]?>;

Related

How I can use the Javascript variable in PHP? [duplicate]

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>

How to obtain page number from html web address [duplicate]

This question already has answers here:
GET URL parameter in PHP
(7 answers)
Closed 6 years ago.
I'm trying to get page number from web address like beds.html?page=3
but, if I use php get method, then it's not returning anything.
I hope to find any easy solution.
cheers
If You Want that parameter on html page then you have to use javascript for it
<script>
var param1var = getQueryVariable("page");
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
</script>
if you are on php page simply use
<?php
echo $_GET['page'];
?>
you can get page like this:
<?php
$_REQUEST['page'];
?>
and also you can type echo before $|_REQUEST to check you are getting proper page number or not
get is the right method to use.
<?php
$_GET['page'];
?>

Set Session inside the javascript [duplicate]

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'] ?>

Concatenation using javascript' script from php [duplicate]

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.

How to use my PHP function in JavaScript code [duplicate]

This question already has answers here:
Call php function from JavaScript
(5 answers)
Closed 7 years ago.
I'm trying to use my PHP function in JavaScript like this:
PHP code:
function checkevents($date){
$ev = new CalendarModelEventss();
$wh = "`event_date` = '".$date."'";
$all = $ev->getAllEvents('event_date', $wh);
if($all == $date){
//echo 'event is available'.$all;
return true;
} else {
return false;
//echo 'event is NOT available' .'--'. $all;
}
}
and JavaScript code:
var yyyy = date.getLocalFullYear(true, this.dateType);
var mm = date.getLocalMonth(true,this.dateType);
var dd = iday;
var ddddd = yyyy+'/'+mm+'/'+dd;
if(<?php checkevents( ?>ddddd<?php ) ?>){
cell.className += " eventsss ";
}
but it doesn't work, and I have PHP error (syntax error, unexpected ';', expecting ')' in ...).
As the previous speaker said, it's impossible to directly call php functions from javascript. PHP could be used to output javascript though.
In your case you would need to send an ajax-request from the client side (with javascript) that executes the php code (on the server side), fetches the output and puts it into your if-statement.
If you do not know alot about javascript/AJAX you could take a look at jQuery or another similar framework.
Javascript is parsed on client side and PHP on Server side.
Without AJAX this is impossible.
Example with JQuery:
PHP:
...
echo "true";//or something else that indicates the success
} else {
echo "false";//or something else that indicates the failure
}
...
Javascript:
$.ajax({
url: "URL TO YOU PHP SCRIPT",
cache: false
}).done(function( html ) {
if(html.match(/true/g)){
cell.className += " eventsss ";
}
});
You cannot directly call php function using javascript directly as both of them are on different sides.So what you do is make an AJAX call with certain parameters and after that call that php function directly from that php file with the parameters to be validated.
And Send back the output back to javascript by echoing the output and do whatever with it on javascript.

Categories