Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to pass JS variable to php echo statement.
here is my code
<?php print 'test'; ?>
how can I do that
I just test this and it works but how can I write the value within the href
alert(myvalue); ";
This is impossible from a temporal point of view.
When the PHP code is run, there is no JavaScript. The page is not yet loaded into the browser. There is no JavaScript engine being run. There are no JavaScript variables. By the time the browser renders the page, the server code has already done its job.
With that said, you can render HTML which refers to a JavaScript function. For example:
<?php print 'test'; ?>
Then, implement DoNav accordingly:
function DoNav(url)
{
location.href = url + '?id=' + my_JS_value; // Navigate to the new URL with the JavaScript variable
}
JavaScript and PHP cannot directly communicate as JavaScript is client-side and PHP is server-side, i.e. PHP is executed before the JavaScript is sent to the browser.
The only way to achieve what you want is to sent a request from JavaScript that calls a PHP script (e.g. AJAX) and passes the variable via GET (like your example) or POST.
Javascript code is executed on the client side, so you do not have access to it.
You can just create the link using Javascript, like this:
var my_JS_value = 0;
document.write('test');
You can insert the element with php and set the href attribute later with JS. Anyway there's a ton of other ways to achieve the same.
<?php
print '<a id="mylinkelement" >test</a>
<script>
var mylinkelement=getElementById("mylinkelement");
mylinkelement.href="thisTest.html?id="+my_JS_value;
</script>';
?>
You don't even need php for that :D
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to pass a variable from one php function to another.
1st function creates a button.
2nd function needs to be able to receive variable that button sent to the 2nd function.
Nothing happens when i click the button. Page just flickers.
<?php
function myFunc(){
$filename = "123";
echo '<form method="post"><input type="submit" name="button1"class="button" value="Button1" onclick="myOtherFunc("tomato")" /></form>';
}
myFunc();
function myOtherFunc($filename){
echo '$filename'."anyting ?" ;
}
Onclick will not execute php code. PHP code is executed on the server BEFORE your page is rendered. Once your page is rendered, the only code that can be executed without reloading the page is Javascript. The onclick attribute on your button will look for a Javascript function called func_ImageView, which does not exist. If you want the content if your func_ImageView function to be called, you need to call that code in php, but you should instead use javascript for this from what it looks like.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I just messed up with is a problem from the last couple of hours and still didn't get the solution i m new in php.what I m trying to do is display and hide the dive according to php condition.
here is the scenario:
note: by default DIV(display:none)
if(condition is satisfied ) //condition in PHP only
//call the javascript function which displays the div
else
//call the javascript function which hides the div
if you have the solution with example please must share it's a humble request
thank you
You want to mix Javascript, HTML and CSS as little as possible. It might look easier to mix it right now, but that will change as you get more experience.
There are multiple ways, but one I prefer is running PHP and passing the variables to a JS variable and let JS decide:
const showSpecificElement = <?php echo funtionWhichDoesSomething(); ?>;
if( showSpecificElement ){
// Javascript magic here
}
// Or, alternatively:
if(<?php echo funtionWhichDoesSomething(); ?>){
// Javascript magic here
}
// Or, slightly better:
$classWhichHidesThisDivOrNot = rand(1,2)==1 ? 'hideMe' : 'showMe';
<div class="<?=$classWhichHidesThisDivOrNot?>"> ... </div>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to put a shortcode inside a php variable like this.
<?php
var Variable = "<div class='container-uppotential'>
</div>
<div class='container-historialdepagos'>
</div>
<?php echo do_shortcode('[mper_account_form]');?>
";
return Variable;
?>
But it doesn't work like that. Any idea how can I make this code work as it is in the example?
first of all you are in php, then your variables should start with the $ sign, then you save the html markup, in a variable, and output it, finally, after all the markup your insert your shortcode. (By the way you have a typo in the shorcode, is mepr not mper)
<?php
$variable = "<div class='container-uppotential'>
</div>
<div class='container-historialdepagos'>
</div>";
echo $variable;
echo do_shortcode('[mepr_account_form]');
?>
PHP is executed before anything is sent to the browser. PHP is not executed in the browser, only by the server.
You're trying to emit PHP code in Javascript. Javascript is executed in the browser. By the time the Javascript is executed, the PHP execution is long over. The server never sees the PHP code.
Basically you can't emit PHP in Javascript and expect it to work. You can emit Javascript in PHP, because the PHP will be executed first, and the resulting javascript will be sent to the browser, where the javascript will then be executed.
Imagine running the microwave for a minute, then putting a glass of water in it. The water will not be heated, because you added the water after the water-heating process.
If I were you, I would make the do_shortcode() call and save the result in a PHP variable, then emit the contents of that variable into the Javascript. That way the PHP execution happens at the PHP execution stage.
Now, if you depend on the Javascript to set some state that the PHP function depends on, you're next bet would be to use something like AJAX, and write a standalone PHP servlet that takes the state information from the AJAX call, executes PHP to generate the desired output, then the Javascript displays the result in the AJAX component.
The only problem I see with your code is that Javascript doesn't allow strings to contain newlines, so you need to split it up if you want it on multiple lines.
var Variable = "<div class='container-uppotential'>" +
"</div>" +
"<div class='container-historialdepagos'>" +
"</div>" +
"<?php echo do_shortcode('[mper_account_form]');?>";
return Variable;
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
So now i now this was a dumb question but does anyone now the best way to solve it without an ajax request, becaus I think it would be stupid to make a new file for just one line of code
I am making a gallery and I want two types of views one grid-view and a slide view. My slide and my grid view both work but they stand in different files. So I wrote a simple jquery function to load some php code into the html code. So that when I click a button the html code dissappears and there is php code. The jquery funtion works but the problem is that when the code is changed it doesn't recognise it as php code but just as a string.
This is what I see, you can see the text but it needs to execute that code:
Here is the jquery function:
$(document).ready(function(){
$("img.grid_icon").click(function() {
$(".werkvenster").html("<?php include_once('resources/UberGallery.php'); $gallery = UberGallery::init()->createGallery('img/gallerij/'); ?>");
})
});
I use the < and >, otherwise the browser sees it as php code it has to execute instead of seeing it as a string.
Here is the html code it has to be in:
<body>
<section class="werkvenster">
<img src="img/website/icon_grid.png" height="30px" class="grid_icon">
<div class="galerij" id="galerij">
</div>
<button class="omlaag" id="galerij_knop">Terug</button>
<button class="omhoog" id="galerij_knop">Verder</button>
</section>
</body>
So everything between the section tags with class "werkvenster" needs to be changed by
<?php include_once('resources/UberGallery.php'); $gallery = UberGallery::init()->createGallery('img/gallerij/'); ?>
When someone clicks on the grid icon.
Thanks a lot in advance and please don't mark this as a duplicate because I have been searching for over an hour to find an answer to this question.
It seems you need some clarification how PHP and JS work.
a) PHP-code is executed on the server-side. all of it. and nothing else
b) The output, completely free of PHP, is sent to the browser
c) JS-code is executed on the client-side. all of it. and nothing else
If you use JS to write PHP-code, it only happens inside the browser, which doesn't know what to do with it. and long after every bit of PHP-execution.
You do NOT write anything back to the server with this.
what you CAN do is to do an AJAX-request to a specific PHP file that returns your desired output. but you can't just mix JS and PHP like that. it just doesn't work.
edit: in response to your edit about how to solve the problem without using AJAX
to put it simply: not at all. you have to get the data from the server, the best way to do this is AJAX (it was made for this and nothing else) and on the server you have to generate the data somehow. and the cleanest way is a new file.
it doesn't recognise it as php code but just as a string
That's because it is a string:
"<?php include_once('resources/UberGallery.php'); $gallery = UberGallery::init()->createGallery('img/gallerij/'); ?>"
PHP doesn't run in the browser, it runs on the server. By the time the page is delivered to the browser, the PHP code has executed and completed and produced its output. PHP is no longer involved after that point.
If you want to execute code client-side, that's what JavaScript is for. If you need code to execute server-side, the browser will have to make a request to the server to make that happen. A page load, a form post, AJAX, etc. They all take the form of an HTTP request (potentially with some data sent to the server) which invokes a PHP script and receives that script's output as the response.
You could do what you are trying to do without ajax. You just have to create a hidden <section> that contains the php code (which you have to do on the server-side before it gets sent to the browser). And then in the browser, use jquery to get the contents from the hidden <section> (or just swap the sections)
Here is an example:
Server-side PHP:
<body>
<section class="werkvenster">
<img src="img/website/icon_grid.png" height="30px" class="grid_icon">
<div class="galerij" id="galerij">
</div>
<button class="omlaag" id="galerij_knop">Terug</button>
<button class="omhoog" id="galerij_knop">Verder</button>
</section>
<section id="hiddenStuff" style="display:none">
<?php include_once('resources/UberGallery.php'); $gallery = UberGallery::init()->createGallery('img/gallerij/'); ?>
</section>
</body>
jQuery:
$(document).ready(function(){
$("img.grid_icon").click(function() {
$(".werkvenster").html($("#hiddenStuff").html());
})
});
Okay, I have created a new question to clarify my old one, which is available here: Check if certain text was outputted to the screen PHP
Currently I have this code:
<?php
echo "
<noscript><h2>! JavaScript is not enabled!!! Features will not work !</h2></noscript>
<script type=\"text/javascript\">
document.cookie= \"jsEnabled=true\";
</script>
";
if (isset($_COOKIE['jsEnabled'])) {
// Javascript is enabled!
}
else {
die("JavaScript is not enabled!");
}
?>
I am not sure why this wont work! It should kill the PHP if JS is disabled! Thanks!
JavaScript processes after PHP has fully given out the page, not before, and not in symbiosis. As such, your PHP call will only work for the second call to the page, not the first.
That is, if you accept cookies in the first place.
If you want to prevent users without JS from using the interface on a page, consider generating the interface in pure JS instead. More reliable.