I need to populate a field with the email address of the person that logged in. Since this app is used by various clients, I want to add the code as needed to the external javascript code. I have been working with json, but now i wonder if this is the answer.
In my php code I have this:
<?php
$myemail = $this->session->userdata('USER_EMAIL');
//echo $myemail;
next I have this:
var $jsonEmail = trim(json_encode($myemail));
Then in my js custom page I have this:
var jsonObj = $jsonEmail;
document.getElementById("email-12").innerHTML=jsonObj.value;
It's not working, but since I am new to this, I don't know what I am doing wrong. Any help would be appreciated.
You're mixing PHP and JavaScript which doesn't work. Your PHP should look like this:
$jsonEmail = trim(json_encode($myemail)); // Get rid of 'var'
Your JavaScript should look like this:
var jsonObj = <?php echo $jsonEmail; ?>
You could do something like this, It will be dirty, but I think will work.
var jsonObj = <?php echo $jsonEmail ?>;
document.getElementById("email-12").innerHTML=jsonObj.value;
You can't mix serverside PHP with localside Javascript. PHP write text to SDIO, and the browser interpret it like things, like JS code, or HTML.
You will need to echo your $jsonEmail variable to your JavaScript, eg:
var jsonObj = <?php echo $jsonEmail; ?>
document.getElementById("email-12").innerHTML=jsonObj.value;
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
Can somebody please explain to me where can PHP script get this functions from, I have a PHP script that has this few lines:
<?php
$args1 = array();
$gethosts = get_xml_host_objects($args1); //grabbing internal xml data from backend
$args2 = array();
$gethoststatus = get_xml_host_status($args2);
$args3 = array();
$getparenthosts = get_xml_host_parents($args3);
So I do not understand from where and how could this PHP script be referencing those functions, could somebody just give me few examples suggestions of where to look?
Maybe, there is a file that includes the two scripts. Somethings like that
<?php
include 'file_with_function.php';
include 'your_file.php';
?>
These lines can help. Place them at the beginning of your file
echo '<pre>';
debug_print_backtrace();
echo '</pre>';
It looks like those were self defined functions, maybe you can try grep thru entire source tree to see if which .php file define these functions and include it in this script.
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.'"'; ?>;
i need to capture the value of an javascript code. To be more precise the document.location.href, but i need to do this with PHP.
Actually i'm doing this:
$html = file_get_html("http://server.camgate.ru/snapshot.php?camid=111013191059");
$element = $html->find('img');
$img = $element->src;
and it shows progress.gif, that's cool, the problem is that i need to get the href of the javascript inside that webpage to show the image inside.
Like this:
$url = "http://server.camgate.ru/snapshot.php?camid=111013191059";
$html = file_get_html($url);
$element = $html->find('script');
$link = $element->href;
$data[] = array("camara" =>"Camara 1", "imagen"=>$link);
the $html shows this:
<script> \n width=document.body.clientWidth; \ndocument.location.href = \"http:\/\/camgate.ru\/showpix.php?id=111013191059&m=1013&d=171013&pix=171013035306.jpg\"; \n<\/script>
So i need to get the url posted there. But if i use the $element->href it shows null.
How can i get it?
Thanks in advance
You can draw Javascript with PHP but you can't do the reverse. If you need to pass data back to your PHP, the only way would be to use AJAX. Either that, or figure out the URL end on the PHP end beforehand.
For example i declare some variable like test in server side of my PHP
echo('var test = ' . json_encode($abc));
Now i want to use this test variable in Jquery ..how can i use it?
What function do i need to use it?
For Example i have:
I have back end PHP code something like this
$abc = no
echo "var test= ".json_encode($abc);
I want jquery to do the following action(client side)
$(document).ready(function(){
function(json) {
if($abc == no )//this i what i want to be achieved
}
}
I think, you dont understand the diference between frontend (JavaScript) and backend (PHP). You can not directly access php variables from javascript. You need to make Ajax-request to some php file, that will return some data that you need in format that you specify.
for example:
<?php
$result = array('abc' => 'no');
echo json_encode($result);
?>
This is serverside script called data.php. In Javascript you can make so:
$(document).ready(function(){
$.getJSON('data.php', function (data) {
if(data.abc === 'no') {
your code...
}
});
}
You're comparing the wrong variable:
<?php
echo <<<JS
<script type="text/javascript">
var test = {json_encode($abc)};
$(document).ready(function(){
if(test == 'no' )
// here you go
}
});
</script>
JS;
If you really wanted to (though I don't think this is a very good practice), you could echo the PHP variable's value into a javascript variable like this:
<script type="text/javascript">
var phpValue = <?php echo $abc; ?>;
alert(phpValue);
</script>
I can see this being dangerous in many cases, but what this effectively does is echo the value of $abc onto the page (inside of your script tags of course). Then, when the javascript it run by the browser, the browser sees it like this:
<script type="text/javascript">
var phpValue = no;
alert(phpValue);
</script>
This is very basic, but you get an idea of what you could do by using that kind of code.