Assigning php variable to javascript alerts PHP source code - javascript

I want to assign the php variable $stop to the javascript variable stopped
<?php
$stop = $_POST['stop'];
?>
<script>
var stopped = "<?php echo($stop) ?>";
alert(stopped); //Output: alertbox with the text: <?php echo($stop) ?>
</script>
How can I solve this?

Since the PHP source code is being alerted, the document is not being run through the PHP preprocessor before being delivered to the browser.
Make sure that:
You're using a web server and not loading from a local file
The webserver supports PHP
The file the PHP code is in is one the server expects to find PHP in (typically this will be a file with a .php extension, but it is configurable).

Related

Codeigniter insert id model using javascript

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 convert this code to insert it in .js file

I'm trying to make my website multilingual, i have the php code also and i have translated files, but my site has also js file where is this words which i want to translate, there is example of this php code and how to insert it to js file?
There is JS code
// Update search list
rsParent.html($('<div>').attr({id: 'relatedSearches', class: 'contentBox'})
.append($('<div>').addClass('cbHead').text('Related Searches'))
.append($('<div>').addClass('cbBody').html(list)));
}
});
and the word "Related Searches" i want to replace with this php code
<?php echo $lang['CHARTS']; ?>
It is not possible to do it directly since PHP is a serverside language which is executed once on a webserver, unlike javascript that is executed in client's browser.
What you can do is encode your PHP array $lang to JSON and then output it as inline javascript and assign it to a variable in javascript.
<?php
echo "<script>";
echo "var lang = " . JSON_encode($lang) . ";";
echo "</script>";
?>
Make sure this php code is placed (executed) before your javascript file because variable lang has to be declared before your javascript is executed.

Assign js variable in PHP variable

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

Is it possible to use PHP to generate JavaScript to generate PHP successfully? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
Using PHP to generate JavaScript works as expected, and using JavaScript to generate PHP works as expected, however using PHP to generate JavaScript to generate PHP does not seem to work.
Is there a problem with my code which is causing this to fail, or is it some sort of limitation?
<!-- Use JavaScript to generate PHP -->
<script type='text/javascript'>
function addComment()
{
alert("About to add some content");
document.write("<?php testFunction(); ?>");
}
</script>
<!-- Use PHP to generate JavaScript to generate PHP -->
echo "<script type='text/javascript'>";
echo "function addComment2()";
echo "{";
echo 'alert("About to add some content");';
echo 'document.write("<?php testFunction(); ?>");';
echo "}";
echo "</script>";
(testFunction() simply contains echo "Test";)
PHP is code executed on the server side. JavaScript is code executed on the client side. They don't know nothing about the other.
You can generated with PHP nearly whatever you want.
You problem is the fourth echo:
echo 'document.write("<?php testFunction(); ?>");';
You tell echo to print this string:
document.write("<?php testFunction(); ?>");
This string is sent to the client (and browser). The browser (the javascript engines) runs this code and results in an output like this:
<?php testFunction(); ?>
We have already left the php interpreter on your server, because we are already on the client. The client can't to anything with this.
If you replace your line with this:
echo 'document.write("', testFunction() ,'");';
your code will executed as expected.
But for this you have change your testFunction. Just replace
function testFunction() {
return "Test";
};
and the output to the server will be
document.write("Test");
Using JavaScript to generate PHP does NOT work. In the first case it's executed as php first, so your testFunction() is run, this resulting javascript is sent to the browser and executed.
You don't seem to understand the Server-Client model. Javascript is executed on the client, while PHP is executed on the server. The client cannot execute PHP and also cannot send PHP code to the server to be executed. It is just not possible.

How to get PHP string value in javascript?

Hi have looking on various questions but none of them seem to help me. I have a php variable in my php code and I am trying to access that in my javascript when I do. . .
var thing = "<?php echo($phpvariable); ?>";
then when I do
alert(thing);
It comes out to be "<?php echo($phpvariable); ?>" in the alert statement
What am I doing wrong?
Your PHP is obviously not being parsed. Are you in a .php file? If you're in a .js file, you'll need the server to parse those (or, more safely, put the PHP part somewhere in the DOM that the JS can access)
However, you're doing it wrong:
var thing = <?php echo json_encode($phpvariable); ?>;
Note: no quotes. json_encode will take care of that for you.
If this code is in a function in javascirpt that executes on click or at a specific event, then:
You are writing PHP Syntax in javascript, there is no way that you load the page then you run the php code. PHP code runs on the server side, so before any other HTML Javascript code executes
Else if you want to dynamically set the variable thing in javascript when the page is first loaded, then most probably you meant to write in the php file:
var thing = <?php echo '"'.$phpvariable.'"'; ?>;

Categories

Resources