Screen size in PHP resolution [duplicate] - javascript

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.

Related

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 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.

Use a JavaScript Function Parameter in PHP [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'm creating a function like this :
function DoSomething(var Var1)
{
<?php
echo Var1; //This will probably not work.
?>
//Note : Not using the echo command originally , using fopen , fputs & fclose at the real one... Because the real function is long i won't put it.
}
Note : I saw the possible duplicates and didn't get my answer...
What is the best solution ?
Thanks for your attention.
Note II : Please don't set this as a duplicate of irrelevant posts...
This Post is not about differences between server-side and client-side languages !
I looked at the comments for more clarification, and it looks like you are trying to do something in PHP with the argument to the function.
You cannot achieve this goal with the code you currently have. PHP runs on the server and sends the HTML and JavaScript to the browser, which then renders the HTML and runs the JS. So, the JS runs after the PHP is run, and the JS cannot send info to the PHP because they are running on seperate entities.
Solution
However, you can use an AJAX request or an HTTP POST request. What these will do is send another request to the server with some data (Var1), and the server can respond with other information.
For this situation I suggest using AJAX, because you can send requests from JavaScript (like in thefunction DoSomething). HTTP POST is used when you submit a form or do something in HTML.
I suggest you look at JQuery, because it makes AJAX requests easy. Specifically, look at Jquery.ajax() and Jquery.post()
Hope this cleared things up! #mention me in the comments if you have any questions.
put your variable in an hidden html input and read it in javascript.
<?php
echo <input type="hidden" id="mystuff" value="your stuff">
?>
javascript
var stuff = document.getElementById('mystuff').value;

PHP- Wait until a value is stored in mySQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a PHP running that uploads a file to my server, I then do some calculations with this file with another script and upload some data (the results of the calculations) to mySQL.
After that, I read this data (the results of the calculations) from mySQL in this same PHP that I used to upload the file to the server.
The problem is that I don't know how long it will take to process this file in my server, it may take 5 seconds or 2 minutes, so now I'm using a sleep(40), because most files process in less time (but not all...) to make sure the values I'm looking for in mySQL will be there, but that's not the way it should be done.
Is there any way to make the program wait until the values I'm looking for (the results of the calculations that are made with the file I just uploaded) are stored in mySQL?
Something that constantly checks if the values are there and when they do continues running the program?
I think it may not be possible with PHP and I may need a javascript.
This is my code:
$uploaddir = '/opt/lampp/htdocs/stl';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$link = mysql_connect("whatever", "whatever...", "whatever...whatever");
$nombre=basename($_FILES['userfile']['name']);
$tamany=basename($_FILES['userfile']['size']);
$sql="INSERT INTO `wordpress`.`uploads` (`autoinc`, `nombre`, `tamany`, `fecha` , `cantidad`) VALUES (NULL, '$nombre', '$tamany', CURRENT_TIMESTAMP , '$cantidad')";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
mysql_select_db('wordpress');
mysql_query($sql);
rename ( $uploadfile,$uploaddir."/".mysql_insert_id().".stl");
$uploadfile_we=basename($uploadfile,".stl");
$numero=mysql_insert_id();
echo '<pre>';
sleep(40);
$sqls= "SELECT `cond` FROM `cm` WHERE `id`='$numero'";
$result = mysql_query($sqls);
$row = mysql_fetch_assoc($result);
$condicion = $row['cond'];
}
As you can see, a file is uploded and then I look in the database. How can I know if the database has already been updated or not?
Without getting into code you could use the database to synchronize your processes.
Place a row in the database upon upload. This will (can be used to) generate an ID value for the new file.
Process the file, updating a flag on the file's record (identified by ID) which indicates that processing is complete.
In the original code, have a loop that will wait some small period of time, perhaps a second, before checking the database state. Once the file has been processed, or too much time has passed, take appropriate action.
There are other ways to pass information back and forth between processes but it seems like you already are doing things in the database that are related to the file being uploaded.
This may get you started in the right direction: http://php.net/manual/en/session.upload-progress.php
Edit in response to comment: The only way I can this is to use JavaScript (Get file size before uploading) to get the file size and then have PHP compare that size to $_FILES['filename']['size'].
Mysql will return true after successful insert statement. So check the value is true and execute rest of your code.
You don't need this. The cm table isn't updated. The insert query don't affect the values stored in this table, you can select data from it at any time, the data will be the same.
After reading your comments, I can't understand why would you run two different PHP script at the same time? I gues you call them via ajax, right after the other? But why would you do it? Just include/require the process script after move_uploaded_file returns ture, update the tables, and then use the data. You save one query, you don't need to select what you've just upldated.

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!

Categories

Resources