javascript-php var post get - php

That code work:
startSlideshow(<?php echo json_encode(glob("photos-animaux/*.jpg"))?>);
that code dont :
$.post("",{'folder':'animaux'});
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg"))?>);
WHY ?, what i am doing wrong ?, help !
why the stupid php fonction just dont make the string right !! ahhhh!
---new infos----
that line work :
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg")) ?>);
because if i MANUALLY enter in the address bar ?folder=animaux...bam! work
so the problem shoul be there : $.get("photo-portfolio.php",{folder:"animaux"});
still dont know where !

If you're using $.post() from JQuery, you should use $_POST['folder'] to access your variable. If you use $.get(), then you use $_GET['folder'] in PHP. Try changing that $_GET to $_POST.

Change $_GET["folder"] to $_POST["folder"] ?
You can dump the $_POST to be sure you're getting the right info..
echo '<pre>', print_r( $_POST, 1), '</pre>';

I hope you're not literally writing these two lines together and hope they are interacting, are you?
$.post("",{'folder':'animaux'});
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg"))?>);
PHP runs on the server, Javascript in the browser. In the above two lines, if written like this, the PHP is already long done by the time $.post() is called.
PHP processes the code on the server and sends this to the browser:
$.post("",{'folder':'animaux'});
startSlideshow(['something.jpg', 'something2.jpg']);
The browser executes this code:
Post {'folder':animaux'} to "" (no effect whatsoever).
Start a slideshow with ['something.jpg', 'something2.jpg'] (which was already decided by the time the page loaded).
I hope you're aware of this two stage process.

Related

setInterval doesn't seem to re-execute php script

what i'm trying to do is get a variable to update every 5 seconds doing this:
setInterval(document.getElementById("listeners").innerHTML =
"<?php include('../includes/shoutcaststuff.php');
echo $dnas_data['CURRENTLISTENERS']; ?>",5000);
but what happens is the inner html is set but doesn't update every 5 seconds like it should.
my guess is that the php only executes once, but i have no idea if that's the case or not.
and i'm aware i should make a function to do the stuff inside setInterval... i'll clean up the code once i figure out how to make it work.
thanks in advance.
ok... ajax was 'the best' answer since no more than 2 people would be logged in at a time here so server requests isn't such a big deal.
here's how i got it to work:
function lCount(){
$.get("../includes/shoutcaststuff.php",{Count: "TRUE"}, function(data){
document.getElementById('listeners').innerHTML = data;
});
}
setInterval(lCount,5000);
and added this to the end of the php:
if(isset($_GET["Count"])){
echo $dnas_data['CURRENTLISTENERS'];
}
now it works fine.
thanks for the suggestions guys :)
<?php include('../includes/shoutcaststuff.php');
echo $dnas_data['CURRENTLISTENERS']; ?>
This code only executes once when the page is built. For the rest of the times this javascript is called whatever is first echoed will be the value.
Instead of using a static value here, you are going to need to use an ajax request (or a websocket if you want to use html5). The request will then hit your server once every 5 seconds. Keep in mind that this can cause undue load on your server.
Ratchet is a commonly used PHP WebSocket implementation that allows for data to be sent to the client using push technology. This is probably more preferable than using your polling approach.
PHP code is run on the server generating the HTML/JS. Use ajax if you need to run php code once the page has loaded.
Take a look at this for example;
Using this:
setInterval(document.getElementById("listeners").innerHTML =
"<?php echo "1";?>",5000);
Will output this to the browser:
setInterval(document.getElementById("listeners").innerHTML =
"1",5000);

PHP session: two values are ALWAYS sent

I'm pretty new to PHP but trying to find my way which has worked quite well up till now.
Problem is the following: I have a website with 2 links. Both should redirect to the same second website but depending on the link clicked, some values should change. I was trying to use a PHP session for this.
Here is the code up to now:
Link 1:
<a href="<? echo $link ?>" class="helsinki" onclick="<? $_SESSION['clicked']= "helsinki"; ?>"^^Helsinki^^</a>
Link 2:
^^Seattle^^
Now if I try to read which link was clicked on the next side like this:
<? if(isset($_SESSION['clicked']))
echo "clicked ". $_SESSION['clicked'];
?>
I always see "Seattle", Helsinki never appears although (I thought) I input Seattle if the Seattle link is clicked. Apparently it's not like that... Can anyone help me here?
This is because PHP code is server side, and thus executed at the time of the page request. So $_SESSION['clicked'] is set to helsinki then reset to seattle at the time your first page loads. You may want to use $_GET variables instead of $_SESSION variables.
Wait wait wait a second: PHP is "server-side", javascript is "client-side". That means you will ALWAYS execute PHP before javascript.
What you are trying to achieve can be simply done with a GET variable:
^^Seattle^^
^^Helsinki^^
And than in the second site you can get the value of our parameter:
$city = $_GET['city'];
Not sure if this is useful, but you could create a "redirector". It takes the user to a server side page that will redirect them to the final page and at the same time change the session.
The page:
...
...
The code for redirector.php:
<?php
session_start();
// determine where to redirect user (could be done with database or array of options)
switch ($_GET['link']) {
case 'helsinki':
header('Location: /link/to/helsinki');
break;
case 'seattle':
header('Location: /link/to/seattle');
break;
default:
return;
}
$_SESSION['clicked'] = $_GET['link'];
You're mixing up Javascript and PHP.
PHP is server side, that means that all PHP code is parsed and executed on the server side, and then is sent to the client.
So if you write
line #1: ^^Helsinki^^
and then
line #2: ^^Seattle^^
the value of $_SESSION['clicked'] is always first "Helsinki" (line #1), but then immediately overwritten by "Seattle" (on line #2).
In order to achieve what you want the script to achieve, is, for example, as follows:
^^Helsinki^^
^^Seattle^^
and then catch the sent variables from the PHP variable $_GET:
if (isset($_GET['location'])) {
if ($_GET['location'] == "helsinki") {
// Whatever
}
elseif ($_GET['location'] == "seattle") {
// You want
}
}
"^^Helsinki^^
"^^Seattle^^
and use $_GET['location'] at the other page to check weather it is helsinki or seattle.
$link should be the same for both links.
Also you can't mix PHP (server-side) with Javascript (client-side). Look it up for why that is.
You cannot write PHP code in the onclick event. If you want to do it like that, you should use Javascript/AJAX. Even if you do it like that, you won't still get the result as you desire, since AJAX request will delay some seconds.
I think the right way to do this kind of thing is like below:
Helsinki"
and this is the change_city.php:
<?php
$_SESSION["clicked"] = $_GET["city"];
?>
if you want to still use the $link, then you just put this above code top of your PHP file.

Executing a script

I need to execute a script within php, i tried almost everything..
http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?username=user&password=******&sendername=Vfup&mobileno= '.$f.' &message='.$message1.''
I tried java script, but i $f and $message1 are variables, so i cant use echo twice
Is there any function in php, which automatically opens the above link in a new window and i need the remaining part of the php code to execute normally without being affected. Will provide more details if required..
Regards,
PHP executes on the server, so is not capable of opening windows. If you want this to happen on the client side you will have to echo it out as JavaScript that the browser can execute.
If you're trying to get this alternate process to fire, then you might want to just call file_get_contents from your current script:
file_get_contents('http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?'.
'username=user&password=******&sendername=Vfup&mobileno= '.$f.
'&message='.$message1.'');
When file_get_contents is given a URL, it will load all of the data from that URL into a local string. This means, in this case, that the server will do what you are trying to get the browser to do for you.
If that is not sufficient, then you will need to use the js function window.open and pass it that URL:
<script>window.open("<?php echo $url; ?>");</script>

PHP echo in Javascript

I have this code:
document.getElementById('root').style.left = '<?php echo $page_position[$info["os"]][$info["browser"]][0]; ?>';
document.getElementById('root').style.top = '<?php echo $page_position[$info["os"]][$info["browser"]][1]; ?>';
Why won't this work like this?
Can anybody point me in the right direction?
EDIT:
<?php echo $page_position[$info["os"]][$info["browser"]][1]; ?>
echoed "top:300px;"
Sorry guys, very stupid error of mine :/
If you put it in a .js file, it won't work because it must be in a page that is parsed for PHP (.php).
Things to check:
Check to make sure those variables are available. print_r($info) will help you out.
Make sure that the code is actually being filled with those variables (symptom of #1). View source and check that they are there.
Ensure you don't have any JavaScript errors preceding your code. Open your console (like Firebug) and check to make sure you don't.
Ensure that a root element exists. Again, use your console.
My money goes on #3, you probably have a JavaScript error somewhere that is stopping execution of the script.

Problem in assigning js value to php variable

How can i assign a javascript value to a php variable,
This is what i want to do:
<?php
$pag = echo "<script language ='javascript'>var pn = document.getElementById('t').value;
document.write(pn); </script>";
?>
But getting error as: Parse error: syntax error, unexpected T_ECHO
or is there any other way to do, but i want to assign that valur to php variable.
can somebody help here?
First, you can't give an "echo" to a variable. echo send a string or an information to the browser.
Then, you need to understand that Javascript is interpreted on the browser and PHP on the server.
It means, PHP can send variable to javascript (in an ugly and static way or with Ajax) but Javascript can't, unless you use it to change page sending the variable via GET or via AJAX.
Finally you should tell us what you need that for...
Javascript is client side, all PHP code is loaded by the server, before sending to the client. Thus, the only way to access JS variables in PHP is by setting a cookie in js, or by using AJAX
If you want to assign the value to a variable and then echo it out, use this code instead:
<?php
$pag="<script language ='javascript'>var pn = document.getElementById('t').value; document.write(pn); </script>";
echo($pag);
?>
Edit
Looking back over your question it would be good if you could explain exactly what you're trying to achieve.
Remove the echo.. You're assigning to a variable, not outputting anything.

Categories