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.
Related
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
This question already has answers here:
Send POST data using XMLHttpRequest
(13 answers)
How to ajax POST to php
(6 answers)
Closed 2 years ago.
i'm trying to build a browser extension that reads the "users online counter" on a specific website. I then want my browser extension so send this number to my server (i have my own top domain). I want this number to be saved in an number.html file on the server. I did some research and think that this can be done with a XMLHttpRequest POST request, but i am a beginner in coding and can't get it to work.
xhttp.open("POST", "www.mydomain.com/number.html", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(numbervariable);
Or do i need to point that request to a .php file on my server, that can process the request and save the incoming number in a .html file? I would really appreciate some help what code i need in my browser extension and what needs to be done on my server to process the incoming data.
Maybe a .php file like this?
$html=base64_decode(urldecode($_POST['innerHTML']));
file_put_contents(numbers, $html);
I am completely lost...
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.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm kind of new to javascript but I'm currently working on my website:
When I press a button, javascript generates a random number (for example: Your Coins: 25) and then I need to connect to my 'members' table and add 25 to the 'coins' field. (I'm already connected with mysql in the php code if this matters.)
Could anyone help me?
If it's "coins" then you probably won't want it to generate client side, otherwise someone would be able to call your java script function with any number they like and add in millions of coins!
The other way is to have PHP generate the number for you.
You can use something like jQuery's $.get function to call your php script with the action of "adding a random number of coins" and the php script can return the random number to java script via JSON for it to be displayed.
First the browser sends a request to the server, which, on its turn parses it. This is the time when your HTML is generated and your PHP runs. When the HTML is generated and ready, it is being sent to the web-browser. The HTML might contain script tags which are pointing to js files via the src attribute, or script tags which contain Javascript code, but before the server sends the response, the Javascript files are not loaded, Javascript code inside the scripts will not be executed. When the response arrives to the web-browser, it parses the HTML, loads the external js, css files and pictures and executes the Javascript code.
So, when your Javascript generates the value, it is running on the user's web-browser, remote from the server. Therefore, from this point the Javascript code should send an AJAX request to the server. This will post a request to the server, which, on its turn will receive and parse it. You can post parameters when you send an AJAX request. jQuery has an easy-to-use variation. Your server will receive the request with your parameters and you will be able to read the parameters via $_GET or $_POST, which are associative arrays containing parameters with their names used as indexes. You can use those to write your query.
All this is well-documented, if you watch a few tutorials, you should be able to solve the problem. On the other hand the commenters and the other answerer are right when they tell you that you should never trust the browser to generate sensitive data, as hackers could easily see what requests are being sent from the web-browser and would send similar posts where they would be "lucky".
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!