Trying to figure out how to make this rating system work - php

I am using the script found on this page.
I downloaded the whole package found on the page and modified the url to the php file like this:
$('#rate').rating('output.php', {maxvalue:5});
And in the output.php I have this. The author of the page says that the post will be calling rating. So that is why I'm using post rating.
<?php
if(isset($_POST['rating'])) {
header("Location: http://www.google.com");
}
?>
But when I run the page and select a few stars nothing happens. The reason why I did header location because I wanted to see if the thing works. But it's not working for me. Do you guys know what I could be doing wrong?

What do you expect it to do? By doing a header Location of google.com, you are essentially redirecting the AJAX request, not the browser of the user that is making the call. You could just print "test" and check with Firebug that the request was made and the text was returned.

Your're sending an ajax request. So you're browser won't redirect.
You could use firebug a firefox plugin. You can check you're ajax requests there, and what they return on the console tab.
Plus you're using jquery make sure you're javascript is correct!

As mentioned above, use Firebug's excellent debugging console to check what response you get. The proper way to validate an ajax request is to check if the HTTP_X_REQUESTED_WITH key in the superglobal server array contains XMLHttpRequest. I. e, you could do this:
<?php
function isAjaxRequest(){
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
}
if(isAjaxRequest(){
echo 'Hello, Ajax!';
}
else exit;
?>
Best,
Christian

How do you know nothing is working? Download Firebug, http://getfirebug.net, go to the "NET" tab and do your ajax request. Then check what kind of response you get. You also have to process the data you get from the rating script! You only get the javascript bit, the server side coding is up to you! You could i.e. do a var_dump of the 'RATING' post variable, and debug it with Firebug. Best of luck!

Related

AJAX or Direct Link

What is the best way to handle Ajax vs. Full page requests with PHP ?
I mean if I click a button that calls Ajax function and loads necessary info into a div and changes the URL.
How do I make that link work if the user copy it and use later to reach the same page.
In some of the apps I separate it with a hash. Something like a router and my code goes something like this:
var preRoute = document.URL.split('#');
if(preRoute[1] != undefined) {
//ajax call
} else {
//default page
}
you can try Jquery Adress http://www.asual.com/jquery/address/,
demo here
http://www.asual.com/jquery/address/samples/state/
or
http://vietcode.vn
You could check the headers. jQuery for example will add the following key/value to the header: X-Requested-With: XMLHttpRequest for ajax calls. See also this question
Using: jquery pjax - https://github.com/defunkt/jquery-pjax
you can detect on server side if you get X-PJAX request header
And a great feature of this plugin is the history manipulation and ease of use.

Any tool or browser add on to check POST/ GET response?

Suppose I have test.php file which echo the value of user and data .Now I can easily check my test.php whether it is working fine or not from a HTML form with user and data
input. But I want to check my test.php file without html form.
Is there any tool or browser plugin to POST or to GET data from test.php.
//test.php.
<?php
echo $_POST['user'];
echo $_POST['data'];
?>
Thanks in advance
Update I have found RESTclient and its really fine for me.
Your best bet is to simply watch webpages interact with your service using Fiddler; you'll then understand what format the service expects.
To send requests in Fiddler, click the Composer tab and fill in the headers and body.
Its fairly straightforward to execute a POST request from the CLI using cURL
curl --data "user=sharif&data=HelloWorld" http://site.com
See also curl usage explained

PHP How to respond to HTTP Request and continue processing?

I am working with an API for a payment gateway that does a callback request.
When the callback request is made, the gateway expects me to respond with "OK".
Nothing more or less. And that doesn't mean html rendered response. Just a callback file with those 2 letters. Note that doesn't mean it wants HTTP Status Code 200/OK... it wants actual data (not headers) for the word "OK".
So this won't work:
<html><body>OK</body></html>
This will work:
<?php echo "OK"; ?>
however, after I send back OK, I need to do some stuff on the server side and then redirect the browser page to another page. But when I try to do:
<?php
echo "OK";
header('Location: http://www.store.com/success.php');
exit;
?>
The gateway ignores the echo "OK" and instead reads the html off of the success.php page that I redirect to.
So how can I send back just the OK but continue doing things on my side?
Thanks
You can't send content then redirect. The redirect header setting must be done alone.
You could try using flush(); to force php to write out the OK.
Would it be acceptable to move the redirect into client side either with javascript or meta refresh tag?
Before you send the OK you could call PHP via commandline to make PHP acting like multi threated.

redirect to current page after header sent

My code structure is like this:
some php code here...
html
head
script
some ajax code here
/script
after running the ajax code, I want to redirect/refresh the page. How can I do it?
Thanks,
Try window.location.reload() in the ajax request success callback.
you can do it with JavaScript; place in AJax callback function:
window.location.reload(); //for refresh
window.location = "http://www.google.com/"; //redirect
Use JavaScript:http://www.tizag.com/javascriptT/javascriptredirect.php
Using window.location="URL" you can send the client to "URL".
But keep in mind that this will not work for people with JavaScript disabled (but so will you AJAX-code).
The best thing to do is to follow the ajax with a javascript redirect using document.location.href='page.html' as you're already relying on running some code in the browser.
Well it depends on what JavaScript version the browser has, but the latest versions should support:
window.location.reload(false);
Have that run on the callback from your AJAX routine.

Sending a request to another site that has a callback that targets my original page

In my test.php file, I sent a request to a Flickr app I have using
header("Location: " . $request);
where $request is the URL that I am trying to reach on Flickr.
For my Flickr app, I have to set a callback URL. When Flickr is done with processing my request, it will call the callback URL.
I would like the callback URL to be my original page, test.php. When I try this, I get stuck in an infinite loop, because test.php is re-sending the request back to Flickr, and Flickr calls my test.php again (repeat ad infinitum until the browser quits).
Is there a way to put some kind of conditional in test.php to check if the request came from Flickr, or at least some way to let the script know that the request has been sent, so don't send it again.
I've already tried it where I changed the callback URL to another page of mine, and that works fine. I'm just seeing if I could re-use the same page.
Its ugly.
The two posted solutions won't work because:
The referer isnt changed on redirect (well it is cleared if its a http meta redirect, but not if its a header redirect. but it doesnt become something else so easy).
Putting exiting after a sent header is generally a good idea if there is something else normaly executed afterwards, but its not related to the problem.
Simply put, if it should be the SAME page, you need to to store in a file or database or something the redirect counts per ip adress/user and break or something but NONE of this is really reliable. You can make it more secure by having a secured token that cannot be reverse engeneered etc but all this doesn't make sense. You could also use cookies. Which is just as unreliable as well.
Regarding your problem, flickr does NOT redirect back to the samep age.
Regarding to their specifications they append ?frob=[frob].
http://www.flickr.com/services/api/auth.spec.html
Check for that:
<?php
if(!isset($_GET["frob"])) {
header("Location: " . $request);
exit();
}
?>
try checking the referer with the $_server['HTTP_REFERER']
[Edited]
I just wanted to say that, you should try adding if condition
// just and example, use some regular expression to check the refere
if($_SERVER['HTTP_REFERER'] != http://flicker.com){
header("Location: " . $request);
}else{
// another code
}
Thanks
As an alternative to checking for the (non-)existence of $_GET["frob"], couldn't you set the callback url in Flickr to be www.mysite.com/test.php?from_flickr=1 and then do
if (!$_GET['from_flickr']) {
header('Location: '.$request);
exit;
}

Categories