Use a JavaScript Function Parameter in PHP [duplicate] - javascript

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;

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

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!

Categories

Resources