How can I use PHP $_SESSION in javascript function? [duplicate] - javascript

This question already has answers here:
Why PHP script is not workig in a web browser?
(9 answers)
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
Okay, here are something I have now.
I'd like to use $_SESSION['pass'] in my other php files to make it as a condition to check whether an if condition is true. But it seems that the system only shows the alert part and never runs the code in the PHP tag.
Thanks a lot!
document.getElementById('pass').onclick = function(){
<?php $_SESSION['pass']=1; ?>
alert('here!');
}
document.getElementById('fail').onclick = function(){
<?php $_SESSION['pass']=0; ?>
alert('there!');
}

PHP is a server-side scripting language whereas JavaScript is a client-side scripting language. PHP runs in the back end on the server whereas JavaScript runs in the front end on the browser. PHP can be used to generate dynamic content for the browser. In your case, it seems like you want to set value of a session variable based on condition in the JavaScript code. PHP blocks in your code are executing in first place on the server which runs all PHP blocks by first setting $_SESSION['pass'] as 1 and then to 0 in the next PHP block. The resulting HTML is then sent to the browser for display. If you want to change the value of session variable based on the button user clicks, then you will need to do it using AJAX request.

Related

How to trigger php code, in Javascript if condition? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 months ago.
How to trigger php code, in JavaScript if condition? I need to start some shortocode in WordPress if the condition is yes.
I have this code. Is it ok?
<script src="https://ssp.seznam.cz/js/ssp.js"></script>
<script>
if (sssp.displaySeznamAds()) {
// display ads
<?php echo do_shortcode('[seznam-ads id="164107"]'); ?>
} else {
// another ads
document.write('do 30min');
}
</script>
PHP is entirely server side. All of the PHP code written to display your page is run once per page-load, and that's it. You cannot directly trigger any more PHP code execution after the browser has received the page.
If you need to update your page in real time with some data presented to you from PHP, you can do that with a method called "AJAX", which initiates a new request to a page from JavaScript, allowing you to retrieve the results of that page request in your script and do what you like with it inside of your current page.
Here's an introduction to AJAX that will get you started: W3 Schools AJAX Introduction

Javascript Understanding when PHP has changed something [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
I'm trying to get javascript to know when my PHP echoes something into an empty div. I tried the code below
<div id = "container" onchange = "changeHandle()" style = "display: none">
<?php
if($condition){ //condition was something from a different section of my PHP
echo "stuff from a database";
}
?>
</div>
<script>
function changeHandle(){
alert("div has changed");
}
</script>
But the changeHandle() function doesn't run.
I tried looking up documentation on W3 schools about Jquery but can't find anything that could help with this (I'm also a beginner at this stuff, so maybe I misunderstood things). Any help would be appreciated!
EDIT: So Bit of context, I'm using this in conjunction with some forms. My user inputs data in some forms and I'm using PHP to query a database after they click enter, then put some of the data I get back into the container. Is there a way to take the data I echo'd and have it in JS?
PHP is server side code and runs on the server before the request is sent to the client. JS is client side code and runs on the client (i.e. the web browser) after the request is sent to the client.
In other words, as far as JS is concerned the value of your div hasn't changed. It was already set from PHP when the page first loaded.

how to access JavaScript variable in php i have tried following but doesn't work [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
i have try to perform select query from tag with JavaScript variable but it is not work.
What wrong with it
function method12()
{
var value12=document.getElementById('period').value
<?php
$con=mysql_connect("localhost","root","");
if(!$con)
{
die('could not connect:'.mysql_error());
}
mysql_select_db("db_loan",$con);
$sqlstr="select bankcode from tbl_master where loanaccountnumber='".value12."'";
$result=mysql_query($sqlstr);
$row=mysql_fetch_array($result);
echo $row['bankcode'];
?>
alert(value12);
}
PHP is run server-side. JavaScript is run client-side in the browser of the user requesting the page. By the time the JavaScript is executed, there is no access to PHP on the server whatsoever. Please read this article with details about client-side vs server-side coding.
What happens in a nutshell is this:
You click a link in your browser on your computer under your desk
The browser creates an HTTP request and sends it to a server on the Internet
The server checks if he can handle the request
If the request is for a PHP page, the PHP interpreter is started
The PHP interpreter will run all PHP code in the page you requested
The PHP interpreter will NOT run any JS code, because it has no clue about it
The server will send the page assembled by the interpreter back to your browser
Your browser will render the page and show it to you
JavaScript is executed on your computer
In your case, PHP will write the JS code into the page, so it can be executed when the page is rendered in your browser. By that time, the PHP part in your JS snippet does no longer exist. It was executed on the server already. It created a variable $result that contained a SQL query string. You didn't use it, so when the page is send back to your browser, it's gone. Have a look at the sourcecode when the page is rendered in your browser. You will see that there is nothing at the position you put the PHP code.
The only way to do what you are looking to do is either:
do a redirect to a PHP script or
do an AJAX call to a PHP script
with the values you want to be insert into the database
You can't use JS variables in PHP. JS code is run on the client and PHP code is run on the server.
To do what you want you have to send the JS value via a GET or POST request to the server and then get that value via $_POST["varname"] or $_GET["varname"]

How to save a JS variable value on PHP site? [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'm writing a little online programm that must save a football tournament results. I'm adding a team names using JS, and i want to save them on server site (in text file). For this i need PHP, i think. JS looks like this:
<script>
function addTeam(){
var name = $("#FormForNameEntering").val();
if (name != ""){
$("#TableDataNameContainer").append("<p class='teamName'>" + name + "</p>");
$('#FormForNameEntering').val("");
}
}
</script>
So i need to save variable "name" in text file. For this i need to tranfer its value to PHP? How?
You have three options. You would make an Ajax call, form submission of some kind with a submit button and action to be set to a php file, or finally have your whole page in a php file and after receiving data in JavaScript do your php there.
You need to send it to the server! e.g:
window.location = www.example.com?variableName='+yourJSVariable
And just retrieve it via PHP's $_GET. Other option you have is sending it to the server asynchronously (=in background lets say). Have a look at jQuery's ajax/get/post functions, or play around with XHR obj in js. Or try submiting via html forms.
Dont worry we've all started somewhere!

Problems with javascript inside php [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
Here I need in some way to concatenate my js variable num with php query if it's possible?
Because, I have two select tags (in my html page) the second one depends from the first one. On blur I can get the value of the first, but I don't know how to concatenate this value to the php query which needs to load the second select.
<?php
echo "<script>";
echo "var num = 5;";
$query2="SELECT * FROM region WHERE IDNation=";
echo "</script>";
$res2 = mysqli_query($con,$query2);
?>
Basically php is server side language. You can assign any value to php variable during page loading.
Javascript works on the user's computer, PHP works on your server. In order to send variables from JS to PHP and vice versa, you need communication such as a page load.
2nd option is you can use cookies
1) First add a cookie jquery plugin.
2) Then store that window width in a cookie variable.
3) Access your cookie in PHP like $_COOKIE['variable name'].(http://www.w3schools.com/php/php_cookies.asp)

Categories

Resources