I create to file ajax.php and jajaxget.php.
script in ajax.php is-
<script>
var data={name:"nikhil"};
$.ajax({type:"post",url:"jajaxget.php",data: {name:"nikhil"},success:function(r,s){alert(s);alert(r);}});
</script>
and jajaxget.php is
<!doctype html>
<html>
<body>
<?php
echo $_POST["name"];
?>
ajax.php works fine and in alert i can see the data which i send. I get a success alert and this ALERT
But when i open the get.php file in browser there is nothing . "nikhil" should show on the page. but the page is blank . why data is not recived by jajaxget.php file. It may be very foolish question .but please help
That should not show on the page when you open it on the browser because you are not passing any POST data when you do so. Your code is working as expected. I think your problem is you do not really get to understand how GET/POST works.
In the post environment variable the post data sent on request is stored. It is not stored forever nor as kind of a cookie. When you make another request, the variable will have different data according with the headers of the request.
So when you are using AJAX and send post data to your file, the return shows the "name" value you've passed because you've sent it on the request headers. However, when you request the page on a browser and you are not passing any POST data, that won't be shown, because nothing you've sent.
If you want your server to store that data, you may use databases or cookies for that.
For more information read some webpages online like http://www.w3schools.com/tags/ref_httpmethods.asp. And please try to understand a bit how the web works before filing it of low quality questions.
If you still have doubts, please ask me.
Related
Having some trouble. New to web development. I'm trying to make a simple application that receives data from a Particle Photon board - using a webhook, everytime a particular event occurs on the board's end, a JSON is sent via POST. I'm running an Apache webserver. I want to process the POST request with PHP, and then have that PHP update what the user sees somehow. Right now I have only index.php, with the following:
<html>
<head>
</head>
<body>
<div>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$data = json_decode(file_get_contents("php://input"));
// do something with $data
}
?>
</div>
</body>
</html>
I'm not worried about input validation, I just want the rudimentary functionality. My thought process is that the webhook sends this POST request (it sends it to index.php, and appears to be working), and then the index.php will be triggered and then display the POST request information. I can't seem to get the index.php to receive the POST request. I've tried var_dump, echo, but it all comes back either blank or NULL. Is there something inherently flawed with my understanding of this setup?
This is the JSON that is being sent:
The only file I have on the server is index.php.
From comments:
First of all, prepare a process file, eg.: process.php, in this file you need only php code that check for a post, process it and store relevant data in your database.
<?php
if(isset($_POST)){
// Process post, store relevant data on database
}
Then, with data properly stored on database, you can show html pages to your users, retrieving data from database and showing to them accordingly.
when use GET Method for receive JSON data , we can acsses the result directly from web browser , for example i send a mydata value from ajax to a main.php file and it process and get answer show a result some thing like below :
<?php
if (isset($_GET["mydata"])) {
if ($_GET["mydata"]=="hello"){
echo "hello world";
}
}
?>
but when a user call it in browser directly like http:mysite.com/mydata.php?mydata=hello recive answer . i want dont allow users to get answer of http request directly , and just can show it from ajax result of main page is it possible ?
You're asking how to prevent an ajax-only request from being accessed directly by copy-pasting the URL into the web browser; that is, only allowing the URL to be accessible via ajax on the main web page.
Well, there are a few things you can try:
Check the Referrer for the URL of the main page with $_SERVER['HTTP_REFERER']
Set a header in Javascript using xhr.setRequestHeader() and then ensure it's value by checking for $_SERVER['HTTP_X_....'] in PHP
Like Jay Bhatt recommended, check for the X_REQUESTED_WITH header, but be aware this might not always be set (see: X-Requested-With header not set in jquery ajaxForm plugin)
However, in any of these situations you should be aware that anyone who knows what they are doing can easily set any HTTP header, variable, or even modify the referrer which is sent to the server. As such, there is no 100% guarantee that your resouce can be accessed only via AJAX on the main web page. There is no control built in the internet to verify where a request is coming from, so anyone can easily spoof or fake it.
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
I am calling a php file using $.post. From the server I am returning json response. When I open the server url directly in browser it returns successfully. But from js it is not working.
The link I am calling is this(http://54.249.240.120/qcorner/login).
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<input type="button" id="testID">
<script type="text/javascript">
$(document).ready(function() {
$("#testID").click(function() {
$.post('http://54.249.240.120/qcorner/login',function(result){
alert(result);
});
});
});
</script>
</body>
</html>
I also tried in Firefox. In that I get 200 OK, but no response.
Why this is happening
The problem is that it is a cross domain request the error being returned can be viewed by turning on the javascript console:
XMLHttpRequest cannot load http://54.249.240.120/qcorner/login. Origin null is not allowed by Access-Control-Allow-Origin.
You will need to make sure that the Access-Control-Allow-Origin headers are set to allow this to happen or Chrome will cancel the request.
I suspect you're running into a Cross-origin resource sharing problem. I'm guessing you're not accessing this page from http://54.249.240.120/, given that Chrome is showing it explicitly in the network tab (usually it doesn't do that if it's the same domain).
Long story short, you can't post via Javascript to another domain name. If you're accessing this at www.example.com, the browser won't recognize the IP address as the same domain name (even if the domain name resolves there). Easiest way, if you're in control of the whole situation, is just put that login code on the same domain as the code you're testing. In a local environment you can do this with your [hosts file, something like 54.249.240.120 www.example.com to redirect example.com (replace with your own domain that you are accessing the test page from) to the IP address. This won't work for the public internet, however.
If you must POST to another domain via javascript, you'll need to look into implementing the CORS standard. Here's an article I found explaining how to implement it in PHP.
An HTML 200 OK is not the same as a valid response. It means the server got your request, but it doesn't mean that your PHP file actually returned usable data. It could be returning an empty response altogether. It's time to debug the PHP file. I'd suggest logging to a file temporarily or use FirePHP. If error display is disable (as it should be on a production server), this can happen if your script is failing before any output is generated.
What exactly is happening with the request? You can't make an ajax request cross-domain with what you have listed in the question.
However, you're sending an empty POST request to the URL, and when I replicate an empty post request, it responds with an HTTP 206 error, which is what you need to sort out.
curl -X POST http://54.249.240.120/qcorner/login
{"head":{"status":206,"message":"Only 0 fields received, required 2"},"body":""}
I had the same issue as you and what I did is very simple.
In you PHP file receiving the ajax request, just add this line before sending the response :
<?php
header("Access-Control-Allow-Origin: *");
... // your code here
?>
I am relatively new to PHP, so my apologies if the answer is trivial. Lol
I wrote a simple Contact Us email form (actually a WordPress page-template file). All the code is in one file.
After the user submits the form and the email is sent, the file generates a Thank You message.
If the user reloads the Thank You page, they are prompted to "Resend the Form Data," which is why I am asking this question.
My question: How do I avoid the prompt to resend the form data and still keep all of my code (including the Thank You data) in one file?
EDIT: I've seen folks use headers( Location: ), but I don't think that will work for if I want to keep all my code in one file.
You could redirect to a different query.
header("Location: ?page=thankyou");
Then, you don't even need to check if the POST data was sent. Just display the thank you page if page is equal to thank you.
This worked for me, it can be put anywhere in html file not just beginning like header() function:
<?php
if (!empty($_POST)){
?>
<script type="text/javascript">
window.location = window.location.href;
</script>
<?php } ?>
I placed it into prevent_resend.php and then included it after the postdata processing was done.
// ... save data from $_POST to DB
include('prevent_resend.php');
// ... do some other stuff
You can use javascript to post the form and show the thank you message. This way the browser never leaves the page.
Even with a header('Location: xxx'); you can still redirect it to the same page, and either set a url parameter or a session variable to distinguish what should be shown.
Although I question your requirement to have all the code in one file (why couldn't you separate it, and use require_once to include shared library code?), the header('Location:') technique is still completely valid. Simply do:
header('Location: http://www.example.com/path/to/my-one-file-of-code.php?thankyou=1');
Then, in your file, you can have:
if (isset($_GET['thankyou']) && $_GET['thankyou']) {
// Do whatever it is you do to thank the visitor.
}
This worked for me:
header("Location: #");