Problems with javascript inside php [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 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)

Related

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

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.

Get localStorage value into php [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Passing HTML5 Local Storage Value to PHP error [duplicate]
(4 answers)
Closed 5 years ago.
I have a variable set in localStorage and I want to get it into php. When php is executed the value is already set in local storage, but how should I get into php?
I tried something like this:
$myvar = "<script> localStorage.getItem('productinfo')['s3_url']</script>";
But this for some reasons gives a Uncaught SyntaxError: missing ) after argument list
How should I approach this? Thanks.
You can not directly get a localStorage value in PHP, when script is executed, because localStorage is contained in user's browser, and PHP is running on server. However, you can send the value from localStorage to a server with GET or POST request.
You can't get local storage values from the server. You should use cookies instead. Local storage is client-side only. Cookies are for both. You can get the value with Javascript and then send it to the server in a form or with ajax, or set a cookie with the value and use $_COOKIE to access it.

Screen size in PHP resolution [duplicate]

This question already has answers here:
Getting the screen resolution using PHP
(23 answers)
Closed 5 years ago.
I want to retrieve the screen size of my screen in PHP . I want to say in my file php :
if(screenSize==1024x600){ ... }
I know how i can retrieve resolution in JS but how i can use it in PHP ?!
Thanks for advance !
You can get the screen resolution with Javascript and post it to your server as a parameter. Then, on PHP you will receive it as a POST parameter and store it in your $_SESSION
$_SESSION["resolution"] = $_POST["resolution"];
Later you will be able to use this value referring $_SESSION["resolution"].
You might want to be able to detect changes, so you could have this code
<script type="text/javascript">
var resolution = <?php echo ((session_status() != PHP_SESSION_NONE) && (isset($_SESSION["resolution"]))) ? $_SESSION["resolution"] : "undefined;" ?>;
//Get the current resolution, compare it to resolution and if different, POST it to the server
</script>
The only problem remaining is that resolution will not be known in the very first request, but you will be able to solve it either by reloading the page after the first calculation or handling anything related to it inside the callback of the POST request.
You cant. 'cause screenSize is browser's property. php is server side. Can not know that property.
But you can retrieve information with javascript and send it to php with an http request.

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!

passing javascript variable value to JSP variable [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
passing value to JSP via javaScript
i want to pass value of a javascript varible to jsp variable is this possible?
This will require two things:
Your Javascript to somehow post the variable's value, either normally (e.g. in a HTTP POST field) or with AJAX to the web server.
The controller needs to fish it out of the request (there are numerous ways of doing that) and supply it to the JSP.
Well, you can't. Javascript is executed on the browser & what comes out there is only HTML. So you can use javascript to set form fields or send them as request variables using AJAX.
You can use your jsp scriplets inside of java script which gets parsed on your server & the values can then be used. for ex:
var i = <%=initialValue%>
When the jsp is processed on the server the scriplets are going to be parsed. What comes out to your browser will be
var i = 10; // In case your initialValue was 10 in your jsp.

Categories

Resources