Everything works fine except email address. When i post email address to server and just echo it, recieve nothing. Only problem in # symbol, rest are ok.
angular:
$http({
method: 'POST',
url: 'http://______.org/_____.php',
data: {
signInSubmitBTN: '', email: 'joe#g.com'
}
}).success(function (data) {
alert(data); //alert empty when joe#g.com but joeg.com is ok
});
PHP
if (isset($_POST['signInSubmitBTN'])) {
$email = $_POST["email"];
echo $email;
}
NOTE - already configure app
app.config(function ($httpProvider, $httpParamSerializerJQLikeProvider) {
$httpProvider.defaults.transformRequest.unshift($httpParamSerializerJQLikeProvider.$get());
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
})
You need to encode using base64:
signInSubmitBTN: '', email: window.btoa('joe#g.com')
And don't forget to decode on the server side using atob()
Use this code in back end to get POST data back in PHP.
if(isset($_POST)){
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
echo $request->email;
}
Related
I have some code in PHP 5. The code is simple - get data from a form in the site using ajax, and send it to mail.
But the code doesn't get the parameters from the POST call.
I have tried it in Postman and the code still doesn't read the parameters.
Image of Postman:
The code in short:
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
What's the problem?
EDIT:
I've changed the code from $_POST to $_GET - now it working in Postman, but in ajax the error still here,
The ajax (with Jquery):
$.ajax({
type: "POST",
url: "http://doodlevideo.co.il/php/form-process.php",
data: {name: name_val, email: email_val, msg_subject: msg_subject_val, msg_subject_option: msg_subject_option_val, message: message_val},
success : function(text){
if (text == "send"){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
Why is it that if I am using Postman, I dont need to include if ($_POST) { '' } else { $_POST = json_decode(file_get_contents('php://input'), true);}
It works differently as If I were to send it from AJAX, but why?
Why doesn't Postman requres json_decode(file_get_contents('php://input'), true);
Ajax code
$.ajax({
url: "http://localhost/WEP/RESTAPI/php.php?api",
type: "POST",
data: JSON.stringify(data),
contentType: "application/json",
success: function(data) {
window.alert("Friend added! "+$name.val()+' '+$email.val());
},
error: function() {
alert("Error");
}
});
PHP
elseif ($srequest == 'POST'){
if ($_POST) {
'';
} else {
$_POST = json_decode(file_get_contents('php://input'), true);
}
$id = $_POST['id'];
$name = $_POST['name'];
$email = $_POST['email'];
//...mysqli connect,query
Your Ajax has been written to send a POST request with a JSON encoded body.
When you use Postman, you must have configured it to use a multipart or www-url-encoded body.
PHP will automatically decode request bodies using those formats.
Postman does something else then an AJAX post. One will post your classic html form with application/x-www-form-urlencoded, the other is posting straight json with a different contenttype.
bottomline: The php $_POST variable does not containt everyting you send with a POST http request!
also see here, this is an excellent explanation: PHP "php://input" vs $_POST
I am a new Angularjs user.I am facing a problem,when i submit a signup form,I have applied validation using AngularJs. At the same time if all the input fields are valid then i have send an $http Ajax call to check the email address,already exist or not.The issue is my php file did not receive email data.
$http({
method : 'POST',
async: false,
url: 'http://localhost/angular/signup/emailcheck.php',
data: { email: $scope.user.email }, // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
})
.success(function(data)
{
$scope.info = data;
if($scope.userForm.$valid && $scope.info === '0') {
alert('our form is amazing' + $scope.info);
}
else{
alert('Already exist');
}
}).error(function(response,status)
{
console.log('ERROR HERE'+ status);
});
My Php file code:
$email = $_POST['email'];
$sql = "SELECT * FROM user where username = '".$email."'";
$result = mysql_query($sql);
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
....
....
....
....
....
}
I have checked and found that php file did not receive email value at all.
$http({
method : 'POST',
async: false,
url: 'http://localhost/angular/signup/emailcheck.php',
data : $.param($scope.user), // this will definitely wor
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
})
.success(function(data)
{
$scope.info = data;
if($scope.userForm.$valid && $scope.info === '0') {
alert('our form is amazing' + $scope.info);
}
else{
alert('Already exist');
}
}).error(function(response,status)
{
console.log('ERROR HERE'+ status);
});
Try removing http://localhost from url and then see it may be CORS.
Just a guess: your url is pointing to localhost but has no port number, this is unusual, maybe you forgot it?
data: $.param({
email:$scope.user.email
})
Or this way: (modify the php)
Angular HTTP post to PHP and undefined
I have just found that in php file,
$_POST or $_GET will not work, to receive data.
Use the following:
$data = file_get_contents("php://input");
$objData = json_decode($data);
$email = $objData->email;
In my case it works.
I'm doing a code that send a data to a php file and send it back again and print it as a response however when i send the data it is sent as an empty data i did not know what is the problem this is the code that i tried:
var resource = $('#resource').val().replace(/[\n\r](?!\w)/gi, "").split("\n");
function doRequest(index) {
// alert(resource[0]); The data here is ok and it is not empty whenever the data is sent the response is empty !
$.ajax({
url:'curl_check.php?email='+resource[0],
async:true,
success: function(data){
alert(data);
if (resource.length != 1) {
removeResourceLine();
doRequest(index+1);
}
}
});
}
doRequest(0);
since you're not sending the data using the data property of the ajax call object like so:
$.ajax({
...
data : { email : resource[0] }
...
});
you are sending it as part of the URL, so it should be picked up as a GET variable. in php, this looks like:
$email = isset($_GET['email']) ? $_GET['email'] : false;
that said, i'd suggest using the ajax data property and specifying the type property and setting it to GET or POST. you could also use $.ajaxSetup
Ok I am not sure what is wrong with what you are doing but I am going to give you code I use to send data to php and get a response. I am going to do it with json because it is awesome. I hope it helps:
var resource = $('#resource').val().replace(/[\n\r](?!\w)/gi,"").split("\n");
function doRequest(index) {
$.post("curl_check.php", {
email: resource[0]
}, function(data, result, xhr){
if(result == 'success') {
console.log(data.success);
} else {
console.log('post failed');
}
}, "json");
}
The php code:
$json = array();
$email = $_POST['email']; //Please escape any input you receive
//Do what you have to do
$json['success'] = 'YAY IT WORKED';
die(json_encode($json));
I'm working with the Laravel framework and I'm making an AJAX request to send an email. The request works fine and the mail is sent, the problem is I can't get the server response if the mail has been sent successfully or not.
Here's the code (short version) wich is located under views/contact/mail.blade.php :
if( mail($to, $subject, $body,$headers) ) {
$data = array( 'text' => Lang::line('contact.mail-success')->get() );
return Response::json($data);
} else {
$data = array( 'text' => Lang::line('contact.mail-error')->get() );
return Response::json($data);
}
and here's the jquery :
$('#contact-form').submit(function() {
var request = $.ajax({
url: BASE+'/contact',
type: 'post',
data: { name: $('#name').val(), mail: $('#email').val(), message: $('#msg').val() },
dataType:"json",
success: function(data){
var message = $.parseJSON(data);
alert(message.text); // here I get the "cannot read property of null" in the console log
}
});
return false;
});
What am I doing wrong? Thanks for your help.
Since Laravel sends the correct headers with Response::json there's no need to parse the JSON in your Javascript, simply change the line
var message = $.parseJSON(data);
to
var message = data;
You shouldn't return Response::json() from a view file, the view are supposed to echo whatever output generated from the view but in this case you need to return the response from the route itself, as json would also include header information.
While sending a response in form of JSON must be encoded using json_encode(); in PHP. after successful reach of done method then parse the object as JSON.parse();
Example :
Modify the line in php file as
return response()->json(json_encode($data));
add the line in javascript files as
done(function (data){
console.log(JSON.parse(data));
console.log(data.text);
});