function show_selling_price(sp,co)
{
$r=sp;
var e="select * from tbl where id='$r'; ?>";
document.getElementById('selling_price_'+co).value=e;
}
//php assign value does not work why?
//Please correct above code with proper description
You can not set a PHP variable from JavaScript. For example this is not possible:
var js_var = 10;
$php_var = js_var;
However you can set a JS variable from PHP:
var js_var = <?php echo $php_var ?>;
PHP is executed on the server side and then the result shows up on the browser.
JavaScript is executed on the client side once the server returns the response.
If you really need to pass a JS var then use AJAX to submit vars to the server.
It is not possible to assign a javascript variable to a php variable like your example above.
Javascript is a client side language whilst PHP is a server side language.
What you can do is to echo out php variables to a javascript code before it is rendered at the visitors web browser like:
<script>
var text = "<?php echo $variable; ?>";
</script>
If you are setting a variable in Javascript first, and then want to pass it on to PHP, you either have to use ajax, or use a html-form to post the information to the server
Related
I have some javascript code that is executed by php. The reason for this is I tried to print to a printer but I couldn't get php code to do it.
But the javascript code can. And I need to post a variable to the javascript so it knows what to print. Therefore I know I can post to php and from there echo the javascript to connect and print to my label printer. The last hurdle is passing in the variable received by POST to the javascript.
$val = "variable";
echo '<script>
var val = "<?php echo $val; ?>"; \\ trying to put the variable into the javascript
var format_start = "^XA^LL200^FO80,50^A0N36,36^FD";
var format_end = "^FS^XZ";
BrowserPrint.getDefaultDevice(\'printer\', function(printer) {
default_printer = printer
...
While I agree with the comenters about not doing this, there is a simple misunderstanding in your code:
You are running PHP to output JS from the echo, you do not need to output the PHP tags, you can just output the variable, and when the page renders, the variable substitution will occur.
So, this:
var val = "<?php echo $val; ?>";
Should be
var val = "$val";
The JS at page render, visible via view source, will now look like:
var val = "myValVarContents";
I hope that helps, even though as said, this is generally a bad idea
D
I want to use javascript variable as php variable. I am echo php variable then its print. but when i am use for fetching data from database its show an error
Notice: Undefined index: document.write(i)
here my code
javascript
var i=0;
function inc()
{
i+=1;
}
<?php $foo="<script>document.write(i)</script>"; ?>
php
code work for
echo $foo
code not work for
$i=$foo;
$query="select * from TABLE where id = $i";
$result=mysqli_query($conn,$query);
while($row=mysqli_fetch_row($result))
{
echo $row[0];
}
Then It show This Error Notice: Undefined index: document.write(i)
PHP is server-side code that is run to generate a page. Javascript is client-side code that is run after the page is sent to the visitor's browser. Javascript can't affect the server-side code because the server code is done running by the time the Javascript runs. If you want to have a user's selection change the behavior of the PHP code the next time the form is loaded, pass a variable through a $_POST variable when the form is submitted.
If you want your PHP and Javascript code to be using the same value, have the PHP code write a Javascript variable initialization into the page's <head> section before any Javascript would run that would need to use it.
<script>
var i=0;
function inc()
{
i+=1;
return i;
}
</script>
<?php
$foo = '<script type="text/javascript">document.write(inc());</script>'; //Script function call which return the var i value to php variable
echo $foo;
?>
How can I get the value of the productid in the php code?
function getunitstock(){
var productid = document.getElementById('product').value
<?php $prodbyid = $this->pos_model->get_productbyid(+productid+)?>
document.getElementById('unitprice').value = <?php echo $prodbyid['price'];?>
}
You can't use javascript variable in php code. Php code run's on the serverside and javascript code runs in the client side. You can't ask the browser to run php code.
<?php $prodbyid = $this->pos_model->get_productbyid(+productid+)?>'
So, this line won't get value of productid.
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
How to pass javaScript values to PHP, my code is following
<script>
function a{
var b = a;
}
</script>
<button onclick="a(2)">Values</button>
<?php
$select = mysql_query("select * from tabl1 where id='values'"); // values comes here
echo $select;
?>
There's a lot of thing you could do.
Principal things you have to know is that javaScript run on the client side (browser), while PHP is running on the server.
Then If you want to pass a variable from your JS to your PHP you have to make a server call.
There's various way you can use in order to send variable from client to server.
As I understand from your example, it looks like your php code and your javascript on the same file. so maybe call your file another time will be enough for you.
Let's say your file's name is index.php.
<script>
function yourJavascriptFunction(id) {
window.location.href = "index.php?id=" + id;
}
</script>
Then in change your PHP code to this:
<?php
$select = mysql_query("select * from tabl1 where id='".$_GET['id']."'"); // values comes here
echo $select;
?>
$_GET will get the variable you've sent in your Js function.
Doing like this will refresh the page.
May be you don't want to refresh the page? Then look at the ajax way.
I hope it helps you
I want to set the value of a JavaScript variable into a PHP variable. How can that be done?
Kindly give me some suggestions.
My code is :
document.getElementById("dummy").value= <?php echo ("document.getElementById('txtplan').value"); ?> //document.formn.txtplan.options[document.formn.txt'+fld+'.selectedIndex].value //eval(''); $_POST['txtplan']
alert(document.getElementById("dummy").value);
<?php
$planname = $_POST["dummy"];
$plandetails = mysql_query("SELECT * FROM membershipplan where planname='$planname'");
while($row1 = mysql_fetch_array($plandetails))
{?>
document.getElementById("txtduration").value=<?php echo $row1['planduration']; ?>
document.getElementById("txtplancontact").value=<?php echo $row1['plannoofcontacts']; ?>
document.getElementById("txtplanamount").value=<?php echo $row1['planamount']; ?>
<?php
}
?>
});
am not do with this means kindly give me the alternative way for the above process
JS -> PHP = impossible (only if you send that info to PHP using POST or GET)
PHP -> JS = possible var text = <?php echo( $text ); ?> ;
The only reason for that is that the PHP code is executed on your server.
You cant do that i.e you cant assign a javascript variable to a php variable
(unless you are using cookie or ajax) but u can assign a php variable to a javascript variable using
var a=<?php echo $a ?> ;
but be careful with that as the php variables will be executed in the server.
The answer is simple. Impossible. Because,
Java script is a client side scripting. Client machine can handle
this script, If it is possible to assign javascript value to PHP
variable that leads to some sort of vulnerability.
So this can't be done.
If you want to accomplish something by doing this kind of assignment, Definitly there would be a way of doing it. Post another question what do you want to do, you will be showered with answers.