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!
Related
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.
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.
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;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Making a hack proof game in Javascript
I'm building a very simple web game
In this game when player clearing all missions the total score should be posted to server .
question is : ajax params could be modified easily .
How to check if datas modified ?
There are several diferent solutions.
You can check you server script that processing your AJAX data, and then logged them.
Another way is to use your browser console. The most of the new Web Browsers are having console, that allowing you to check all the data of your web page, even the data you send or receive from the server with AJAX Calls.
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.