When I click the submit button, there's an ajax post request. But then the returned json value has only "" in it. Even the console.log(data) is not working when the .done() ajax is fired up.
<form id="form" action="#">
<input type="email" placeholder="email" id="account_name" name="account_name" value="email@test.com"> <br>
<input type="text" placeholder="First Name" id="first_name" name="first_name" value="volvo"><br>
<input type="text" placeholder="Last Name" id="last_name" name="last_name" value="volva"><br>
<input type="submit" name="submit" id="btn"/>
</form>
<script type="text/javascript">
$(function(){
var btn = $("#btn");
var form = $("#form");
var accName =$("#account_name").val();
form.on("submit",function(evt){
evt.preventDefault();
var myReq = $.ajax({
method:"POST",
url:"create.php",
dataType: 'json',
data: "account_name="+accName
});
myReq.done(createAcct);
});
function createAcct(data){
console.log(data);
var a = JSON.parse(data);
console.log(typeof a);
}
});
</script>
So the returned json is this {"ac":""}Array in the response tab. Here's my php curl
$ca = createAcct($_REQUEST['account_name']);
$data = ["ca"=>"$ca"];//should be the email
print json_encode($data);
echo $data;
function createAcct($email_account){
$data = '{"account_name":"'.$email_account.'"}';
$ch = curl_init();
//Set cURL parameters to perform API Call
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, "https://api.website.com/api/accounts/create");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$statusCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$output = curl_exec($ch);
$err = curl_error($ch);
if ($statusCode == 204) {
return $email_account;
curl_close($ch);
} else {
curl_close($ch);
}
}