How do I show my script value from javascript to php - javascript

I have a script (javascript)
<script>
$(function(){
$(".editagunan").click(function(){
var id_agunan = $(this).attr("data_id_agunan");
$("#value_id_agunan").val(id_agunan);
$("#myModalEdit").modal("show");
});
});
</script>
how to display 'value_id_agunan' from javascript to php
<?php echo id="value_id_agunan" ?>
but the script is wrong, how to fix ? Thanks

Assuming, you are asking on how to write html inside a php file.
Just write HTML directly, like this:
<p id="value_id_agunan"></p>
If you want to show the above html code on some condition then use php like this:
<?php if(showParagraph){ /* some variable */ ?>
<p id="value_id_agunan"></p>
<?php } ?>

Related

Is it possible to change HTML within PHP code?

What is the easiest way to run Javascript code inside PHP, preferably without using external libraries? Specifically, I need the innerHTML of a div to change depending on some calculations performed using the user's form inputs that were captured earlier with $_POST in a separate form. How can I get the following JS code to run inside the following PHP code? (My most basic attempt was using echo command, but it's throwing up errors)
HTML:
<div id="output">
</div><!--#output -->
PHP (desired JS included):
if (count($valid_locations)<$num_of_locations){
echo '<script>const output = document.querySelector("#output");</script>;';
echo '<script>output.innerHTML = "<div class="alert-danger"> Trip unable to be created. Please adjust the budget/number of travelers/duration etc.</div>;";</script>';
}else{
createTrip($trips);
}
Right now, the IF condition is True and the code does run, but it echos ";" for some reason. I have verified that the condition is met and it runs by adding in a simple echo "this runs";, which did appear upon refresh. I just don't understand why it's outputting to the default echo space and the script isnt running to change the innerHTML of the output div. Is this even possible to do with PHP? If so, what's the best way? (doesn't need to be secure, won't be used publicly). I've heard of a few things such as ajax but it seems so complicated. Any explanations on stuff like that specific to this scenario would be greatly appreciated.
Following up on the comments, you don't need JS to achieve what you want. You could just use PHP
<?php
$output = null;
if (!empty($_POST)) {
//...
//... do stuff
//...
if (count($valid_locations)<$num_of_locations){
$output = 'Error occured - bla bla bla';
}
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php if (isset($output)) {?>
<div id="output"><?= $output; ?></div>
<?php } ?>
</body>
</html>

Insert code with JS just like echo in php

I want to get some code snippets from a PHP server to be "injected" in HTML. Basically when I open page.php I get some text like "_off".
The injection works with this code, however I can't use php in this html since it is local.
<img src="images/test
<?php
{ echo "_off"; }
?>
.jpg">
JavaScript seems to be the logical step. But if I just enter this at the position where I want the text of course it doesn't work:
<script type="text/javascript"> $.get( "page.php", function( data ) { document.write(data); } ); </script>
Any ideas?
You need to use $('#someElement').html(data) or $('#someElement').text(data) if you want to write the data to a specific place on the page. You should avoid using document.write
Here is a fiddle to demonstrate: https://jsfiddle.net/yd9mx4d3/

cakePHP scriptStart() and scriptEnd(), how to use?

why this code is not working?
Seemsm like i don't understand the usage of scriptstart() and scriptEnd().
// view
<?php
$this->Html->scriptStart(array("block"=>true,"inline"=>FALSE));
?>
$().ready(function(){
alert("dd");
});
<?php
$this->Html->scriptEnd();
?>
// layout
echo $this->fetch('script');
edit
Some more info:
I expect it to popup the alert..
In the example nothing happens. seems like the javascript is not being added to the page. (i checked the source)
Try removing the "block"=>true option, or setting it to 'script' instead:
// view
<?php
$this->Html->scriptStart(array("block"=>'script',"inline"=>FALSE));
?>
$().ready(function(){
alert("dd");
});
<?php
$this->Html->scriptEnd();
?>
Make sure you include the php tags in the layout:
// layout
<?php echo $this->fetch('script');?>

Jquery is not working in php block

<body>
<ul id="M8" class="Mh"><li ><a id="atagid" class="msub" href="javascript:;
"onclick="trackchange();" >Track Change(OFF)</a></li></ul>
<?php
if(isset($_SESSION['track'])){
echo "<script>alert('working');</script>";
echo "<script>
$(document).ready(function(){
$('#atagid').css('color','green');
$('#atagid').html('Track Change(ON)');
});</script>";
}
?>
<script>
function trackchange(){
$('#atagid').css('color','green');$('#atagid').html('Track Change(ON)');}
//This function is working but in php block it is not working
</script>
</body>
If $_SESSION['track'] is set then the html element a color should be changed to green and the content should be changed to Track change(ON).
In this script alert function is working which confirms the session variable, but content and color of a tag is not getting changed. I couldn't to find where am I doing wrong.
The probable issue with your code is misplacement of jQuery file, Just try to put it in the <head>...</head>.
You need to include jQuery first by using
<script src="point/to/your/jquery.min.js"></script>

How can I use a theme option in a JS file?

I'm creating a Wordpress theme and have added an option which allowed users to change a font family using (simplified code): update_option('mytheme_font', $_POST['mytheme_font']);
How do I then get the value of that option in a JS file of the theme? I need it because I'm using Cufon to replace some H1's and H2's. Thanks!
You have four options I suppose.
Output your javascript to your page inside script tags. Easiest
<script>
<?php echo 'var x = 3;' ?>
</script>
Output the variable to your page and then read that from your javascript file. Clunky, but does mean you don't have to create some js globals.
<div id="x" style="display: none;">3</div>
var x = document.getElementById('x').innerHTML();
[Added] - Use AJAX to request the data and parse after page load.
Last and I don't recommend it, but setup php to parse .js files to dynamically produce javascript files. This way you could place a <?php ?> call in your .js files.
You could echo some thing like this in the <head> of the header.php
<?php
$defaultThemeFont = "myDefaultValue";
$userThemeFont = get_option("mytheme_font");
if($defaultThemeFont == NULL)
$userThemeFont = $defaultThemeFont
?>
<script>
<?php echo "var mytheme_font = $userThemeFont;"; ?>
</script>
Now you can access this variable from any JS file

Categories

Resources