Is there any way to POST forms to the external site? - php

I must create a simple app to:
create input with my variable (NIP)
connect to site https://wyszukiwarkaregon.stat.gov.pl
somehowe show in my app Captcha code from there and able to fill in and POST to above site
search there "NIP" POSTed from my app and show results
I tried to get content by cURL and JS but as we know there's Same Origin Policy protection... Posting by JSON ends the same.
Now I'm stuck and don't even know how to do it. Anybody have any ideas?
Thanks for any help how to can create it.

How about AJAX-Request via JQuery?
Links:
http://www.w3schools.com/jquery/jquery_ajax_get_post.asp
http://api.jquery.com/jquery.ajax/
$.ajax({
method: "POST",
dataType: "json",
url: "http://www.stat.gov.pl/regon/",
data: { name: "John", password: "XXXXX" }
})
.done(function( msg ) {
alert( "Your Response:" + msg );
});

Related

Undefined index from AJAX POST to PHP?

I'm new to this community and a beginner with programming. I'm trying to post a JSON data with AJAX to another PHP file and my code is as follow:
--- AJAX code ---
<?php session_start(); ?>
//other codes in between
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
//alert('Transaction completed by ' + details.payer.name.given_name);
alert(data.orderID);
// Call your server to save the transaction
return fetch('/paypal-transaction-complete', {
method: 'post',
body: JSON.stringify({
orderID: data.orderID
})
});
});
var orderid_data = data.orderID;
$.ajax({
url: "test_parse.php",
type: "POST",
data: {'order_ID': orderid_data },
success: function(data){
alert(data);
});
--- PHP code ---
var_export($_POST);
echo $_POST["order_ID"];
I've realised that my $_POST return an empty array.
Any help would be greatly appreciated :)
Since you open [...]test_parse.php directly in your browser, there is no POST-data present anymore, therefor its empty.
The POST-Data is part of the request. If you open it manually in the browser you perform a GET request.
What can you do to see the var_export?
You can use Chrome or Firefox for example both have Developer-Tools which allow you to trace the exact POST-Request you performed via ajax.
For Chome:
Hit F12 to open the Dev Tools
Navigate to Network
Now perform your Ajax-Post-Request and find it in the Network list. By clicking on the requests you will see all details you will need. Most Imporant for you is Response

Authenticate GET request in PHP

I have a PHP File that get id and remove user.like this:
$user_id = $_GET["id"];
remove_user($user_id); //this function remove user by that id
i send data to this PHP file using JQuery.like this:
$.ajax({
url: "http://example.com/delete-user.php",
type: "GET",
dataType: "html",
data: "id=" + user_id,
success: function(data) {
location.reload();
},
error: function(error) {
console.log("Error:");
console.log(error);
}
});
But I know this has a security problem.
If user enter this link in address bar:
http://example.com/delete-user.php?id=10
delete-user.php File execute and this user(with id=4) will be deleted.
how i cant prevent this?
in other words how i can check User who sent the request, do it through the form?
You can use $_SESSION to check if the user submitting the id is logged in and if it is, check if that user has the permission to perform this action.
But without knowing how your system / application is working, there isn't much information we can provide to you.

no data from callback, jquery ajax json, php, amazon

I've been trying the code at: https://gist.github.com/Exeu/4674423 this is a code that connects to amazon wsdl and retrieve product information. so far I have not been able to make it work. The code consist of a index.html, ama_funtion.js, search.php, AmazonECS.class.php. The index file loads and you insert the item name you want choose the category and other params, then when you click in the search link it goes to the ama_function.js and it make and ajax request to the search.php which have the passwords and keys to connect to the wsld, then it request the info from Amazon and return it as JSON to ama_function.js.
I have test ama_funtions.js to see if the index.html file connects to it, and it does, I did put an alert to show the input in the search box at index.html. Also I have test the search.php, I have manualy put the values in the search.php file and it connects to amazon and retrieve the product information as JSON object. the problem is that there is not a success callback as no data return to ama_funtions.js from search.php to be rendered. I know this as I tested it by adding an error function
$.ajax({
url: "search.php",
dataType: 'json',
type: 'GET',
error: function () {
alert("NO DATA");
},
and it did show me the alert, also I have tried to change the ->
data: "q="+value+"&category="+cat+"&country="+country+"&page="+page,
for ->
data: {
q : value,
category : cat,
country : country,
page : page }
nothing works I am crazy looking to solve this, I will appreciate someone that help us with this. Please see a working example which belong the person sharing the code -> http://amazonecs.pixel-web.org. This is the way is suppose to work but it doesn't.
Regards
$.ajax({
url: "search.php",
dataType: 'json',
type: 'GET',
error: function () {
alert("NO DATA");
},
data: "q="+value+"&category="+cat+"&country="+country+"&page="+page,
data: {
q : value,
category : cat,
country : country,
page : page
}
})

jQuery $.ajax POST function into WPSC

I am using Wordpress Shopping cart. I want to append or modify the product name based on 3 variations the same thing goes for the price to.
I have tried to implement a simple Jquery on click event to an image using the AJAX $.post method within the single product page. It's not sending the data value even if I encode it with JSON. I must say that I'm new with JSON and PHP. Thanks in advance !
In the single product page
$(function() {
$('#div_img_link').click(function() {
$.ajax({
type: 'POST',
data: "test value",
// dataType : "json",
url: '../wpsc-cart_widget.php',
cache:false
});
});
});
In wpsc-cart_widget.php
$name=wpsc_cart_item_name();
if (isset($_POST['data'])){
$value1 = $_POST['data'];
}else{
$value1 = "";
}
echo "$name $value1";
I know that are some similar posts out there I've read them all can't figure it out. I want to know if the WPSC can perform the jQuery POST function without refreshing the page. Any help will be greatly appreciated!!
schematics
EDIT
SCHEMATICS 2 - to be more concise !
What I'm trying to accomplish
SEA, you're missing a function to handle the response that comes back from the server.
I would expect to see something like this :
$(function() {
$('#div_img_link').click(function() {
$.ajax({
url: '../wpsc-cart_widget.php',
type: 'POST',
data: "test value",
dataType : "JSON",
cache:false,
success: function(data) {
//do something here with the data.
//eg.
alert(data);
},
error: function() {
//do something here if an error occurs.
//eg.
alert("an error occurred");
},
});
});
});
The best place to learn more about ajax is the ajax() page in the jQuery API documentation.
You also need to ensure that the server response and the client expectation are compatible. With a client expectation of dataType: "JSON", you must ensure that the server returns a json-encoded response. Json-encoding is only necessary for multiple data in a single response. A single data item can be sent unencoded.
EDIT:
After seeing your Schematic 2, I think your ajax request data should look something like this :
data: {
'pid': '12345678',
'quantity': '1'
),
where pid is the "product id" and quantity is how many of the product the user wants to add to his/her basket.
But surely the WPSC documentation includes examples of this stuff? You shouldn't have to work it out for yourself.

Difference between a form post and jquery post

I need to know the exact difference between:
<form method="POST" action="https://mywebsite/signon.php">
<input name="harv_acc" value="940322903" type="hidden" />
<input name="harv_eml" value="a#b.com" type="hidden" />
<input type="submit" value="SignOn" />
and
var url = "https://mywebsite/signon.php";
$.ajax({
url: url,
type: 'POST',
//dataType: 'html', -- this was something I tried later
//data: "harv_acc=" + accountnumber + "&harv_eml=" + email , this is also what I tried last but below is what I tried first
data: { harv_acc: account, harv_eml: email },
success: function (data) {
closePopup("div_PleaseWait");
alert(data);
//window.location = encodeURI('<%= Url.Action("DownloadDocument", "Documents") %>?DocumentID=' + documentID + '&DownloadType=' + downloadType + '&DownloadPath=' + data);
}
});
When I post the latter I get a 200 but no response. If I submit the first one I get the correct response.
From the comments:
I'm posting to another site
Aha! There's your issue. Browsers block AJAX to external websites for security reasons. Sorry, but you're not going to issue that request via an XHR request.
If the other website wants you to communicate with them, they could expose this part of the site via JSON-P, which works something like this:
My site adds <script src="http://othersite.com/signon.js?username=foo&password=bar&callback=myCallback"> to the source code (yeah, it's messy to use GET for this, but JSON-P can't work any other way), and creates a function named myCallback to handle the response data.
The other site signs in, then returns something like myCallback({success: false, errorMessage: "Incorrect password, try again!"})
That script is run on my site, calls myCallback, and everything is happy.
JSON-P is a powerful protocol, but only works if the remote site agrees to it. Still, if they do, jQuery has a nice shortcut for it: just set dataType: "jsonp" and it will handle the whole callback thing for you.
But if you're not closely involved with this website, that's unlikely to happen, and you'll probably just be stuck with having to give up on this kind of cross-site interaction. Sorry, but this kind of cross-domain policy is critical to online security. (I don't want other sites issuing requests to bankofamerica.com on my behalf, kthx.)
The first parameter passed to your complete function will be a jqXHR object, which is a wrapper around the browser's XMLHttpRequest object. A more convenient way to handle the response is to use the done method:
var url = "https://mywebsite/signon.php";
$.ajax({
url: url,
type: 'POST',
dataType: 'html',
data: "harv_acc=" + accountnumber + "&harv_eml=" + email
}).done(function(data) {
closePopup("div_PleaseWait");
alert(data);
});
Cross domain ajax requests are not supported by browser. But there is another way to get around this.
You can use JSONP for cross-domain requests. It is easy to use and allows you to request anything (as long as it is in JSON format) from any server/script that supports the callback. The good thing about JSONP is that it works in older browsers too.
The only serious limitation seems to be that it always uses the HTTP GET method
Can you please check with this too.
Try sending ther data as a key:value object. This is an example from the jQuery docs
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
Update: as user Matchu pointed out, this is not the problem, as the data will be converted into a query string anyway, as stated in the jQuery docs:
"The data option can contain either a query string of the form key1=value1&key2=value2, or a map of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string using jQuery.param() before it is sent. "
So yeah, some rash answering on my part there. At least I learned something! ;)
when using POST method you should ,in your case, Post your data as JSON
var url = "https://mywebsite/signon.php";
$.ajax({
url: url,
type: 'POST',
dataType: 'html',
data: {
harv_acc : accountnumber,
harv_eml : email
},
success: function (data) {
closePopup("div_PleaseWait");
alert(data);
//window.location = encodeURI('<%= Url.Action("DownloadDocument", "Documents") %>?DocumentID=' + documentID + '&DownloadType=' + downloadType + '&DownloadPath=' + data);
}
});
NOTE : i used dataType : JSON

Categories