How to read jquery cookie value in php cookie value? - javascript

I am not sure is it possible to read jquery cookie value in php cookie value?
var current = $.cookie("count");
show<?php echo $keys; ?><?php setcookie("bgx","document.write(current)"); echo $_COOKIE["bgx"]; ?>now.play();
$.cookie('count', <?php echo $count; ?>);

Here is a simple demo that shows how to create a cookie with JavaScript and read it with PHP when you reload the page (because JavaScript is executed after PHP, as it is client-side). Copy this code onto a new PHP document, upload it to your server, and try it.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<h1>Cookie demo</h1>
<p>This demo shows how you can create a cookie with Javascript (using jQuery), and read it with PHP.</p>
<?php
/* THIS PORTION IS EXECUTED ON THE SERVER */
// if the cookie "myCookie" is set
if(isset($_COOKIE['myCookie'])){
echo "<p><b>PHP found this value for <i>myCookie</i>: " . $_COOKIE['myCookie'] . "</b></p>";
}
else{
echo "<p><b>PHP did not find a value for <i>myCookie</i>. Give it a value below.<b></p>";
}
?>
<input type="text" id="myInput"/><button id="myButton">Change the cookie value using JS</button>
<!-- make sure you load jQuery and the jQuery cookie plugin -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script>
/* THIS PORTION OF CODE IS ONLY EXECUTED WHEN THE USER SEES THE PAGE (CLIENT-SIDE) */
$(function(){
$('#myButton').click(function(){
$.cookie('myCookie', $('#myInput').val());
alert(
'The value of myCookie is now "'
+ $.cookie('myCookie')
+ '". Now, reload the page, PHP should read it correctly.'
);
});
});
</script>
</body>
</html>

A cookie is a piece of data that is stored in a browser and associated with a website. Every time the browser requests something from a website, it includes the cookies in the request headers.
You can read and write cookies with client side JavaScript. (NB: It is possible to mark cookies as http_only, in which case they can't be read with client side JS).
You can read (by examining the request headers) and write (using response headers) cookies with server side code.
The usual rules of timing apply:
show<?php echo $keys; ?><?php setcookie(
Setting a cookie with server side code requires you to issue a response header. PHP doesn't let you issue response headers after you have started outputting the response body.
You are trying to call setcookie too late. You need to call it before you start outputting any content.
"document.write(current)"
I'm not sure what you are trying to achieve here.
It doesn't make much sense to store JavaScript code in a cookie.
You can't generate server side code using client side code, so if this is an attempt to write the value of current into a cookie, then it is too late.
setcookie("bgx","document.write(current)");
echo $_COOKIE["bgx"];
$_COOKIE is populated with data from the request.
It won't have data in it that your current response is about to ask the client to store. You won't get that new value until the next request.
In short: To read a cookie set with JavaScript, you have to make a new HTTP request after you have set it.

Related

GET in HTML and Not PHP

I want to process a GET extension in a HTML page and not a PHP page.
I have looked through the internet and not found anything.
URL = examplesite.com?id=1234
I assume this would go to the index page on the domain. As the index page is a HTML page, is there a way to get the details of the extension transferred to another link I have in the html script that emails me when someone looks at the site.
<script src="trigger.php">
</script>
This way I can customise the extension to know where the person found me. id=1234 is from twitter, id=2345 from FB etc.
Then i could place the extension onto the script to send me the email.
<script src="trigger.php?id=1234">
</script>
Is there a way to get the HTML page to process extension and pass it on in a variable of some sort.
Thanks in advance
Robert
You can do it in Javascript in the HTML. window.location.search contains the query string from the URL.
You can then use an AJAX request to send the query string to your server script.
<script>
$(document).ready(function() {
var script = 'trigger.php' + window.location.search;
$.get(script);
});
</script>
This is not possible with plain HTML. By definition, HTML is not dynamic. It can't process anything you want. However, there are three options.
Firstly, you can use JavaScript and AJAX calls to make another HTTP request to examplesite.com/processID.php (or another PHP page) which will process the request.
Another way to use JavaScript would be to use a client side API such as MailChimp to send the email directly from the users computer.
Or you could just redirect your root page for your domain examplesite.com to lead to index.php. I'm sure that's very easy to configure in mainstream servers such as Apache or Nginx. Otherwise please ask another question on Server Fault about how to set this up using your server.
If you are using a PHP hosting provider, they should also be able to help redirect the root page. If you don't have any access to PHP on your hosting provider, you're out of luck. You must only use the second option.
Do it with ajax
<form id="form1">
<input type="text" />
<button type="submit" id="sendforms">send</button>
</form>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#sendforms").click(function() {
var combinedFormData = $("#form1").serialize();
$.get(
"trigger.php",
combinedFormData
).done(function(data) {
//alert("Successfully submitted!");
$("#result").html(data);
}).fail(function () {
//alert("Error submitting forms!");
})
});
});
</script>

HTML in new Browser Tab or WIndow

In my PHP file, I have a string variable that contains complete HTML code.
$content = '<html>
<head>
<script>--Some javascript and libraries included--</script>
<title></title>
</head>
<body>
<style>--Some Styling--</style>
</body>
</html>';
I get this in a loop and now I want this HTML to open in new tab or window of browser. With every iteration, there would be a new Tab or Window. I don't think it is possible on server side so may be some JavaScript need to be concatenated to it.
In loop, I've created the URL to hit the controller and set the parameters with it and then simple use the following statement.
echo '<script type="text/javascript"> window.open("'. $url .'"); </script>';
and In the $url variable, I created the URL and post data in it. That's how, the new tab will open that hits the url and result will be my complete HTML.

How to store the value returned by javascript function into a php variable?

The output generated by the program below is Inner textjain. Fine, but I want $m php variable to be assigned the value returned from JavaScript function a().
<html>
<head></head>
<body>
<?php
$name='abhi';
?>
<div id="a">
</div>
<script type="text/javascript">
function a(){
var a="jain";
<?php echo $GLOBALS['name'] ; ?>=a;
document.getElementById('a').innerHTML="Inner text"+<?php echo $name; ?>;
return <?php echo $GLOBALS['name'];?>;
}
</script>
<?php echo $n='<script type="text/javascript">' , 'a();' , '</script>';
echo $n;?>
</body>
</html>
You can't. Once the page is delivered to the browser, which is where Javascript runs, your PHP server has already shut the request down. If you somehow really want to send something calculated in Javascript back to PHP, you'll have to make an additional request to the browser from Javascript (using AJAX) but even then the variable will be available in a new request, not the old one.
PHP runs on the server, Javascript runs on the browser, and they don't run concurrently, so you'll have to send your Javascript variable to the PHP script (and re-run the PHP script), which involves reloading the page.
You can e.g. a GET request (by loading script.php?yourvariable=value).
you can't. PHP runs at the server first and then javascript will run at client side.

PHP file_get_contents() in setInterval() not working

I am trying to use get_file_contents() in a setInterval() to repeatedly update some text which is displaying the contents of a file. Here's the code:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<body>
<div id="theDiv"></div>
</body>
<script>
$("document").ready(function(){
setInterval(function(){
$("#theDiv").text("<?php echo file_get_contents('file.txt'); ?>");
console.log("<?php echo file_get_contents('file.txt'); ?>")
},500);
});
</script>
The problem is: it seems that the text is not updating until I reload the page.
the file_get_contents() is NOT set to reload by setInerval. You mixed up client-side JavaScript & server-side PHP.
You probably need the following:
$("document").ready(function(){
setInterval(function(){
$("#theDiv").load('file.txt');
},500);
});
You have to create another php file to output the content and use ajax to get it.
Example:
$("document").ready(function(){
setInterval(function(){
$("#theDiv").load('/echo_the_content.php');
},500);
});
Create echo_the_content.php with:
<?php
echo file_get_contents('file.txt');
If your file.txt is in public dir (web accessible), then you could load it directly without another php file.
the setInterval get executed in the client side where as the php is evaluated in the server side so the command echo file_get_contents('file.txt'); is evalulated only once.
You need to use an ajax request where it returns the contents of the file
$("#theDiv").load("file.txt");
where the server side resource file.txt returns the desired response that has to be displayed in the div

Sending silently email when page loaded

I need to send an email(silently) each time page was loaded.
I tried below code :
<html>
<head>
<script language="javascript" type="text/javascript">
var email = "h.kamrava#yahoo.com"
var subject = "test"
var body = "Hello world"
document.write("<form name="form" action=\"mailto:"+ email +"\?subject="+ subject +"\&body="+ body +"\" method=\"post\" enctype=\"text/plain\"></form>")
</script>
</head>
<body onload="document.form.submit()">
</body>
</html>
It's does't work to me! What am i doing wrong? tnx
I suggest you let the server do this, like PHP mail, rather than have JS do it. You can send a request to the server on page load to tell the server to send an email to your target. That way:
No messy, creepy looking scripts on your page.
They'll never know an email was actually sent.
"Fire and forget". Send the request to the server, and done! The server does the rest.
Your email address, written only on the server, stays hidden in the server side and avoid email harvesters and subsequently, spam.
The request that triggers it could be anything, like loading a 1x1 gif, a script, AJAX or whatever.
I don't think you can do the same through forms, especially a post form. In either case what you do won't be silent as it requires the user to send that email. mailto: simply presents the user with a way to send the email using their own client.
If you want to send an email quickly from your HTML document, use mailto: from within an anchor, then either wait for the user to click it or force it to click.
My link
If you can use PHP or some other server-side language, it'd be much easier and "silent". Check out the mail() function of PHP, which allows you to send an email to whoever from whoever.
you need to use ajax for this purpose .
for example you can use JQuery ajax , something like this :
$(document).load('ajax/mail.php?email=exp#mail.com&subject=test&body=hi')
and then in php page use mail() function to send it
EDIT: Looking at the form code, I can see that this won't actually send an email...but this would be how you would submit a form using javascript. If you want to send an email silently, you will need to post to some backend service.
Try putting everything into a load handler:
<html>
<head>
<script language="javascript" type="text/javascript">
function sendEmail(){
var email = "h.kamrava#yahoo.com"
var subject = "test"
var body = "Hello world"
document.write("<form name="form" action=\"mailto:"+ email +"\?subject="+ subject +"\&body="+ body +"\" method=\"post\" enctype=\"text/plain\"></form>");
document.form.submit();
}
</script>
</head>
<body onload="sendEmail()">
</body>
</html>
checkout this :
http://jsfiddle.net/4gGFR/
and instead of calling form post, call that <body onload="sendemail();">

Categories

Resources