i am trying to make something related to paging, where i have a array of around 400-500 values..
i want to show 9 values at a time and then after clicking on NEXT button i want to show next 9 more..
Here is the part of the code i am using to call JS function onclick of button and that function's job is to get the count from the browser and set it to next value.
<?php
session_start();
$_SESSION['count']=9;
echo "<script type='text/javascript'>
function changelistnext()
{ ";
$c=$_SESSION['count'];
$c=$c+1;
$e=$c+9;
$_SESSION['count']=$e;
echo "
alert($c)
alert($e)
}
</script>";
?>
<button id='next' value='next' onclick='changelistnext()'>Next</button>
But everytime i click on the next it shows the same value....
You can't mix JavaScript and PHP as if they were one and the same.
JavaScript is a client-side language, which is executed in your browser.
On the other hand, PHP is a server script, and sessions are also kept on server.
If you open your page's source code in a browser, you will see that the session stuff is not there - it was executed by PHP when the page was generated. You can't use sessions directly in JS.
The page contains essentially this:
<script type='text/javascript'>
function changelistnext()
{
alert(some number) // value of C when the page was generated on server
alert(some number) // ditto for E
}
</script>
You could use Ajax call to ask some script on your server to give you data for the next "page" - PHP script can access the Session variables and send what you want back to the browser.
Related
I'm trying to build a website with only 2 pages.
Page 1
In this page theres a buttom and everytime the buttom is clicked it increases the value of the label on page 2.
Page 2
In this page there is only one label a value that is increased by page one
Could someone give me an example
consider the following link as a solution to your problem using AJAX and jQuery.
http://tutorialzine.com/2009/09/simple-ajax-website-jquery/
Go through sessions
In your page one
<?php
session_start();
$_SESSION['myValue']=10; // here your value.
?>
In page two
<?php
session_start();
echo $_SESSION['myValue'];
?>
Another way
<a href='pageone.php?myValue=12'>click</a>
then in any other page, you can access
$_GET['myValue'];
Depending on what resources you have available, you could use AJAX to save the value to the database/XML file on PAGE 1. Then write a script to retrieve the value on PAGE 2.
Alternatively, you could look into saving a SESSION variable on PAGE 1 and retrieve that on PAGE 2.
Session Links:
PHP SESSION Tutorial
PHP Manual - SESSIONS
Js for the button page
var counter=0;
function buttonclick(){
counter++;
window.postMessage(counter);
}
Js for display:
window.onload=function(){
window.addEventlistener("message", handle,false);
}
function handle(event){
alert(event.data);
}
This uses the browsers internal messaging system to transfer the value from one window to another. If you want to transfer it even if page2 is not opened use AJAX to send it to the server and use SESSIONS to store it.
I just found out that PHP can be used in Javascript like this:
<script>
function seeTime() {
alert("<?php echo time(); ?>");
}
</script>
<button onclick="seeTime();">Click Me</button>
The above code will popup an alert message with the Unix timestamp every time the button "Click Me" is clicked. But the problem is, the time remains the same everytime you click the button until the page is reloaded.
I know AJAX is the solution but is there any other way I can update the data inside the javascript code/file without using any external php file to fetch data from it with AJAX ?
I know AJAX is the solution but is there any other way I can update the data inside the javascript code/file without using any external php file to fetch data from it with AJAX ?
Yes, just don't use PHP at all:
function seeTime() {
alert(Math.floor(new Date().getTime()/1000));
}
You are getting confused between PHP and JavaScript. PHP is not being "used" in JavaScript. PHP runs on the server and generates the page, which is then sent to the browser.
<?php echo time(); ?> is replaced with the current timestamp, then sent to the browser which runs the JavaScript.
As far as I know it's impossible, PHP code is compiled server-side, so all you return from a php compilation are static value, you must use AJAX to refresh the time value.
You could set the time with JS if you wish.
I have a condition in php based on some session variables. If condition passes, I want to call a Javascript function which is updating my current webpage. Is it possible to place such a call to Javascript function independent of a button or a link or the window load function?
If I am understanding you correctly, just use php to output the javascript function. It will fire once, if you need.
<?php
if ($_SESSION['condition']=="foo") {
echo "<script>","/n";
echo "doThisNow();","\n";
echo "</script>","\n";
}
?>
Or, you could echo out the session variables as javascript variables in the same way, and then do the conditional check in javascript.
As proposed in another answer you can output a Javascript call whenever the page is loaded.
However, if your event cannot wait until the page is reloaded, then I suggest you setup some javascript to poll a page every X number of seconds (using AJAX) and do something when the values change.
Here is what I'm trying to do...
$("div").prepend("<div id='comment-number'><?php the_comment_number(); ?></div>");
Is there some way to get this to work?
<div class="gallery-item">
<div class="comment-number"><!--?php echo htmlspecialchars(the_comment_number()); ?--></div>
</span>
<span class="gallery-icon">
<img src="http://mysite.com/test/wp-content/uploads/2011/06/fff-150x150.gif">
</span>
</div>
PHP is executed on the server, but JavaScript code (jQuery) is executed later, in the web browser. For that reason, PHP can produce JavaScript code, but JavaScript can't produce PHP code.
The <!--? in your posted output shows that something is filtering our your PHP code. So the problem isn't your PHP code, it's that you're not actually executing PHP code. If it's a .js file, PHP almost certainly can't be included.
If PHP were being evaluated (ex. if this were in a <script> tag in a .php file), this should produce valid JavaScript code that will do what you want:
$("div").prepend("<div id='comment-number'><?php echo htmlspecialchars(the_comment_number()); ?></div>");
1) php is SERVER side scripting
2) javascript is CLIENT side scripting (generally)
so this is what happens:
1) User opens up your page http://example/
2) Your CLIENT sends GET request to http://example/ server
3) Apache (given you run on it) captures the request, based on the server config approaches index.php (index.html, etc). If php is installed, your index.php will be parsed by mod_php module
<<<< this is where SERVER side scripting is activated
4) outcome of the parsing of index.php will be then transferred back to CLIENT
5) CLIENT will digest the outcome received by SERVER
6) If there are javascript calls, those are executed either immediately OR when document is loaded (based on approach)
That's it. Here normal request life ends.
NOW if you want your page to dynamically update some parts of it, here is how you do that:
1) Usually to update your page dynamically, you would use AJAX approach. When AJAX request is created, 2-7 happens again, but this time the caller is your ajax process and information which is received is sent back to that process to decided what to do with it.
Okay, some coding:
1) index.php
<!-- include jquery, etc //-->
<div id="comments"></div>
<script>
function fetch_comments(){
$.get("ajax.php", null, function(data)){
// this is called, when response from SERVER is received
$("#comments").prepend(data);
setTimeout("fetch_comments", 5000); // fetch again in 5 seconds
}
}
$(document).ready({
fetch_comments();
});
</script>
2) ajax.php
<?php
//fetch comments, return them for CLIENT
echo "<p>Comment on " . date("Y-m-d H:i:s") . "<br />Lorem Ipsum</p>";
This should help you understand the whole process. Did not test the code, but should be quite ok.
do a .ajax() query to PHP script that will provide you value of the_comment_number(); and put result to comment-number by $("#comment-number").prepend(result); on success event in ajax query.
Remebmer that PHP script have to have connection to database and pass to it all variables you need (like entity id, article id, page etc.). You can do it by GET or POST.
Request is sended by browser so session/cookies will be the same unless you changed it in current request.
PHP is executed on the server side so you cannot call it from javascript
You can try something like this which will render once the page loads
$("div").prepend("<div id='comment-number'>"+ <?php the_comment_number(); ?> +"</div>");
Couldn't you just add the value directly to the template instead of using javascriot? eg:
<div class="gallery-item">
<div class="comment-number"><?php echo (the_comment_number());?></div>
...
</div>
Also you have a </span> tag with out matching <span> tag in your example.
As already told, you can't produce or call php code from javascript directly(you need to make an ajax call or form submit). You need to make ajax call using jquery to fetch the comment number and then update it into div.
However, you may want to look at this library - http://www.phplivex.com/ .It may help you in doing things your way. It allows you to call user defined php functions in javascript using AJAX internally.
Reading through this disccussion and from what i understand you want to acheive.. You gotta figure how your page is served. If it is an .php file that is serving the content, then you wont need Javascript at all and could get your function call to work by adding the function between the div as so..
<div class="comment-number"><?php echo htmlspecialchars(the_comment_number()); ?></div>
Assuming you don't have access to the .php or if its a .html/htm page that serves the content then your only bet would be to use ajax. That is make an ajax call to a php file(on the same domain) that makes your function call and echos the comment no. The Ajax will retrieve the echo'd comment no. which you can append/prepend to the desired
I'm trying to assign value of JavaScript variable to php session. Please see my code below -
<script type="text/javascript">
<?php $_SESSION['historyClass'] = "";?>
var myClass = $(this).attr("class");
if(myClass == 'trigger'){
<?php $_SESSION['historyClass'] = "trigger"; ?>
}
else{
<?php $_SESSION['historyClass'] = "trigger active"; ?>
}
alert('<?php echo $_SESSION['historyClass']; ?>')
</script>
In myClass variable, i'm getting 2 values
1) trigger
2) trigger active
Whatever the value I'll get, I want to store it in php session. But when I alert the session value it is always giving me "trigger active". It means it is always going to else part. I have checked the 'if' condition by alerting in it, the control is going properly in "If" and "else" part.
What is the problem? Am I doing something wrong?
Thanks in advance.
PHP is processed first, and then javascript is executed, so it's impossible to directly assign values to php variables.
instead you could send http requests from javascript (Ajax) to php scripts to save your data.
This won't work. PHP is executed on the server, and JS on the client. That means that the php is running before the JS is parsed. I suggest you look at Ajax if you want to run PHP from javascript (specifically the jQuery library and its ".get()" function).
What's happening is that the PHP is parsed, and doesn't see any JS, so it runs as normal, and sets the session to trigger, and then trigger active. Then javascript comes along on the client, and the client doesn't see any PHP code,so doesn't run it.
this wont work. the php code runs on serverside, js - on client. so php is executed before the page is shown to the user. it means that first is executed $_SESSION['historyClass'] = "trigger"; than $_SESSION['historyClass'] = "trigger active"; and obviously the value will be trigger active
yes you are doing something wrong.
javascript runs on client side and php on server side. so your code runs first on server side and then on the client.
thats why you can't do it like you did. a common way to transfer javascript data to a php script is, writing the value to a hidden field, so that it gets submitted to the server.
just create a hidden field and fill it with a value from javascript
<script type="text/javascript">
function valToPHP(name,value){
document.getElementById(name).value = value;
}
</script>
...
</head>
<body>
<input type="hidden" id="myField" name="myField" value="" />
...
you can then read it in your php script like that
$_GET["myField"] or $_POST["myField"]
this depends on your method of the form