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.
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'm using WordPress to get_categories into a variable and then trying to get the category name from the get_categories array to then pass that into a jQuery variable to have it check against another conditional in jQuery.
So far, I'm able to get the data from get_categories correctly and then use json_encode to parse that array into a json format.
But what I'm getting hung up on is I just want the name that is coming from the get_categories array from WordPress and then pass that into jQuery to run the conditional.
I'm not sure of the proper way to get that formatted correctly so that jQuery can interpret that without getting all cranky about it.
Here's what I have so far:
Code:
<?php $test = get_categories('taxonomy=stuff&type=item'); ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var $things = <?php echo json_encode($test); ?>
$(".this-div").each(function() {
var $item = $(this).attr("title");
if ($things.indexOf($item) > -1) {
$(this).addClass('current');
}
});
});
</script>
I've tried $test[0]->name but that's only going to return one of the values and not all of them.
So, I'm not sure if I'm going about this correctly or if there is a better way possibly.
Can't say I use word press, but if get_categories() just returns a JSON string you could do something like this:
<p class="categories"><?php echo get_categories('taxonomy=stuff&type=item')></p>
<script>
let categories = JSON.parse($('.categories').text());
$(".this-div").each(function() {
let $div = $(this);
if (categories.includes($div.attr("title")) {
$div.addClass('current');
}
});
</script>
The hp part in php means Hypertext Preprocessor, as in it preprocesses the hypertext. It doesn't work within a script. However, you can use it to write data to an element in the hypertext and then access that from a script.
As long as you put the hypertext before the script in the document, you shouldn't have to wait for the entire document to load. $(document).ready()
Also, if you're going to use $(this) multiple times, it's recommended that you save it as a variable to save redundant jquery function calls. I find future self appreciates if you name it something more descriptive than $this.
Oh yeah, and .includes(). It's a cleaner way of saying .index() > -1.
Figured it out with this:
<?php
$test = get_categories('taxonomy=item&type=things');
$names = array_column($test, 'name'); ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var $things = <?php echo json_encode($names); ?>;
console.log($things);
$(".element").each(function() {
var $state = $(this).attr("title");
if ($things.indexOf($state) > -1) {
$(this).addClass('current');
}
});
});
</script>
From here: Using jQuery script with WordPress foreach loop
I am currently trying to output a number of records through pages. However I am already kinda failing at the beginning...
The <table> with all the records gets updated through ajax. However the problem I have is that whenever I click on the link which should update the part.php?page=1 it doesn't do anything.
file part.php
function activateEMode(aa){
//update all val etc
loadDoc("partB.php?page=1"+val+"&person="+person+"&status="+status+"&s_priortiy="+val1+"&s_uDate="+val2+"&s_fDate="+val3+"&search="+search+"&aa="+aa+"&q="+val, partiB);
}
file partB.php
$page = $_GET['page'];
for ($i=1;$i<5;$i++){
echo '<a href=part.php?page='.$i.'>'.$i.'</a>';
}
echo $page;
So what I am trying to achieve is that, when ajax function loadDoc is called it should set the ?page to the <a> pressed. Would be nice if you could help me :).
You can use window.location.replace to jump to a new link.
It will be like this:
var currnet_Location = windows.location;
var current_Location = current_Location.substring(0, current_Location.indexOf("page="));
var location_to_jump = current_Location+"New Location";
window.location.replace(location_to_jump);
Good Luck :)
I am storing the result of some calculations in a HTML div element using the innerHTML attribute:
var result = document.getElementById('duration').innerHTML+
obj1+" "+obj+" "+
km.toFixed(1)+" Km "+Date
document.getElementById('duration').innerHTML = result
Now I want to store the div's content/data in a PHP variable.
After set dic content, use jQuery to send data to PHP:
var url = 'myPHPFile.php';
$.post(url, {data: document.getElementById('duration').innerHTML});
In your PHP , get your variables from $_POST['data']:
<?php
$data = $_POST['data'];
?>
JavaScript is on client side, so you can try to send JS variable (div content) back via URL and then obtain it via PHP.
JS like this:
window.location.href=ā€¯index.php?divContent=" + document.getElementById('duration').innerHTML;
And then in PHP obtain it from GET, like this:
$somevar = $_GET["divContent"];
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;