I have to post to a URL using PHP. right now it works but after the sending data/posting, it gives gives a json output (from the web service i'm posting to) and i need the output to be friendly. I can't use jquery with ajax because it's conflicting. i need to communicate directly to the web service
Here's my form:
<form action="http://somewebservice.com/<?php echo $PrizeID;?>" method="post" name="myform">
<input name="account" id="account" type="hidden" value="<?php echo $account;?>"/>
<input name="dob" id="dob" type="hidden" value="<?php echo $dob;?>"/>
<input name="site" id="site" type="hidden" value="<?php echo $site;?>"/>
<input name="submit" type="submit" value="submit"/>
</form>
after clicking submit it takes me to a page with the following JSON output:
{"Prize":"XXXXXX","ConfirmationCode":"######","Error":false,"ErrorMsg":null}
The posting works ok because i ckeck the web service logs and the submission is registered. all i need to do now is to show the ConfirmationCode
from the piece above nicely formatted instead of the whole JSON output.
Any help would be greatly appreciate it.
UPDATE i made some changes based on ur help... it refreshes the page but doesn't update the db. i checked the web service log and no entry is recorded. it gives me bool(false) as error and when ECHOing $context, i get "Resource id#4"
here's the updated form:
<form name="myform">
<input name="account" id="account" type="hidden" value="<?php echo $account;?>"/>
<input name="dob" id="dob" type="hidden" value="<?php echo $dob;?>"/>
<input name="site" id="site" type="hidden" value="<?php echo $site;?>"/>
<input name="submit" type="submit" value="submit"/>
</form>
here's the PHP
if(isset($_POST['submit'])){
$options = array(
'http'=>array(
'method'=>"POST",
'contentType'=> "application/x-www-form-urlencoded",
'content'=>http_build_query(array(
'account' => $account,
'dob' => $dob,
'site' => $site
))
));
$context = stream_context_create($options);
$result = file_get_contents("http://somewebservice.com/{$PrizeID}",NULL,$context);
var_dump($result);
}
Update (w/AJAX) I tried ajax and i while it writes to the web service, i get an error in the clietn site. the error says [object Object] here's the ajax
here's the form
<form name="myform" >
<input name="account" id="account" type="hidden" value="<?php echo $account;?>"/>
<input name="dob" id="dob" type="hidden" value="<?php echo $dob;?>"/>
<input name="site" id="site" type="hidden" value="<?php echo $site;?>"/>
<input id="submit" type="submit" value="submit"/>
<div id="result"></div>
here's the ajax script:
<script type="text/javascript">
$("#submit").click(function(e){
e.preventDefault();
var account = $('#account').val();
var dob = $('#dob').val();
var site = $('#site').val();
$.ajax({
type: "POST",
url: "http://somewebservice.com/<?php echo $PrizeID;?>",
data: "{ 'account': '" + account + "','dob': '" + dob + "', 'site': '" + site + "'}",
contentType: "application/json; charset=utf-8",
async: false,
success: function(data) {
//$('#result').text(data.d);
alert(data);
var obj = $.parseJSON(data);
$("#result").html("<p><b>Confirmation Code:</b> " + obj.ConfirmationCode + "</p>");
},
error: function(data) {
alert(data);
$("#result").html("<b>There was an error. Please try again</b><br/>");
}
});
});
</script>
UPDATE (w/Curl) - SOLVED - IT WORKS !!!! This works - it posts to the database via the web service and i'm able to get back the ConfirmationCode returned by the webservice.
Here's the form in offer.php
<form action="process.php" method="post" name="myform">
<input id="submit" type="submit" value="submit"/>
</form>
here's the process.php file
<?php
$data = array("account" => "{$account}", "dob" => "{$dob}", "site" => "{$site}");
$data_string = json_encode($data);
$ch = curl_init("http://somewebservice.com/{$PrizeID}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode($result, true);
?>
// here's the call for the confirmation number
<p>Your confirmation number is: <strong><?php echo $json_result['ConfirmationCode'];?></strong></p>