This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 5 years ago.
Please help me on how to concatenate php code on jquery html:
$('div#divID').html('<?php echo $fib->getFibonacci( " + limit + "); ?>");
in order to pass the value: var limit = $('#inputID').val() of input after clicking the button. Thank you!
PHP Code
class Fibonacci {
public function getFibonacci($num){
...
return $fib;
}
}
$fib = new Fibonacci();
HTML
<input type="text" id="inputID">
<button id="buttonID">
<div id="divID">
JS
$('#buttonID').click(function() {
var limit = $('#inputID').val();
$('div#divID').html("<?php echo $fib->getFibonacci( " +limit+");?>");
});
Goal: to pass the value on input when calling Php function using jquery syntax.
Is there possible way? Thank you.
In your example you are trying to pass JS variable as argument to PHP function. It is not possible this way, because PHP is server side, and JS is client side (in your case) scripting language.
What you can do is on click to calculate "limit" value, pass it using AJAX to "getFibonacci" function in php and to append into html tag returning value. That is one way of interaction between PHP and Front-end JS.
Related
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 1 year ago.
I am new in PHP and javascript and need help with the following code
I want to get the value from browser URL that tag with "org" and use it in javascript; as example "http://localhost/nmt/index.php?org=table1" I want to put the "org" value inside the URL/ inside the POST $.post("ch1.php?org=table1", instead of table1 put the GET value
This is the part from code
$(document).ready(function () { showGraph1(); }); function showGraph1() { { $.post("ch1.php?org=table1", function (data) { console.log(data);
That If possible replace table1 with the value from URL
You can inject your variable into the javascript.
<script>
var data = <?php echo json_encode($_GET["org"]); ?>;
</script>
You also can get the url parameter directly from js.
This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 5 years ago.
I'm trying to concatenate a link in a javascript function with a php variable. But whatever a try to write after ""+ doesn't get displayed and the whole program stops working. The variable is defined in a different page. I tried REQUEST to access to it, but it's not working.
function popupEvent(){
$('#btnClose').unbind("click");
$('#btnClose').click(function(){
window.location = SITEURL+"cart/rollPdf/?POrderNo="+implode(',', $orderIDArr);
window.open(ord,'winname','directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=350');
return false
});
}
Whenever you want to use PHP variable into JS, you need echo it. Ex, <?php echo implode(',', $orderIDArr);?>
In your code try Like this,
function popupEvent(){
$('#btnClose').unbind("click");
$('#btnClose').click(function(){
window.location = SITEURL+"cart/rollPdf/?POrderNo=<?php echo implode(',', $orderIDArr);?>"; // Changed here
window.open(ord,'winname','directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=350');
return false
});
}
Hope this will help you to solve your problem...
EDIT:
function popupEvent(){
$('#btnClose').unbind("click");
$('#btnClose').click(function(){
ord = SITEURL+"cart/rollPdf/?POrderNo=<?php echo implode(',', $orderIDArr);?>"; // Changed here
window.open(ord,'winname','directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=350');
return false
});
}
This question already has answers here:
Pass Javascript Value to Java in JSP
(2 answers)
Closed 7 years ago.
New to learning JSP, and trying out passing data between two pages.
I'm wondering if it is possible to pass a javascript variable to session.setAttribute()
At the moment, I can pass a string of text through 2 jsp files like so:
JSP1:
<% String text = "hello";
session.setAttribute("test", text);%>
JSP2:
var someText = "<%=session.getAttribute("test")%>"
which works fine.
However, is it possible to pass through a var into session.setAttribute instead? I store some data in a javascript variable and would like to send it across to the second JSP file.
So for example:
var number = 7;
<%session.setAttribute("test", number);%>
I've tried this out and I get the error "number cannot be resolved to a variable"
Thanks!
You cannot do that since javascript executes on client & JSP executes on server side.
If you want to set javascript variable to JSP session, then you pass this variable through the URL like this
var number = 7;
window.location="http://example.com/index.jsp?param="+number;
Now receive this var in your JSP page like this
String var = request.getParameter("param");
Now set it in session
session.setAttribute("test", var);
EDIT :
var number = 7;
<%session.setAttribute("test", number);%>
In the above code, server will only execute the code inside <% %>. It does not know anything outside of the JSP tags. So, it will also dont know about your javascript variable number.
Server executes the code & the result will be sent to the browser, then your browser will execute that javascript code var number=7;.
Hope, now it is clear for you.
This question already has answers here:
javascript variable into php
(3 answers)
Closed 8 years ago.
i have php code like this
<?php
$queryitemkits="select item_id, cost_price, unit_price from items where item_id='JAVASCRIPTVARIABLE'";
$resultqueryitemkit = mysql_query($queryitemkits) or die('Query failed item kits!');
while($rowitem = mysql_fetch_assoc($resultqueryitemkit))
{
$itemcostprice=$rowitem['cost_price'];
$itemunitprice=$rowitem['unit_price'];
}
?>
and i have javascript variable named ui.item.value
how to insert that ui.item.value javascript variable to replace JAVASCRIPTVARIABLE in above PHP code?
Remember PHP is on the server, and the variable "JAVASCRIPTVARIABLE" in the client, you need to submit the variable to the server, also you need input validation, JAVASCRIPTVARIABLE it can be empty when is received on the server.
This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 8 years ago.
I am trying to use a php variable in my javascript code, but i cant seem to get it working. Here is my js and php code:
<?php $s = "woo"; ?>
var images = <?=$s?>;
What i want to achieve, is a php variable in javascript ENCLOSED with ['']; With other words, so that the javascript code reads it like this: ['woo'];
I could really need some help, as i am very new to javascript. Thanks in advance.
I recommend using json_encode for all values "passed" to JavaScript - this will prevent against injection, accidental or otherwise. It also trivially handles quotes and allows complex object graphs to be supplied. If not already, I imagine that images is really, or should be, an array ..
var images = <?= json_encode($s) ?>;
Or
var images = <?php echo json_encode($s); php?>;
Look at the actual HTML to see what is being emitted, and that it is valid - the original yields JavaScript akin to var images = woo;, which will result in a ReferenceError (on woo).
One trick you can use is this, when you are inside a .js file it can be helpful, because php doesnt work.
Put the value of your php value inside a div as content, and then grab that in the javascript.
in the php file:
<div id="sVar" style="display:none;"><?php echo $s; ?></div>
in js:
var images = document.getElementById('sVar').innerHTML;