I have the following javascript that creates json object and works fine but I want to pass this object to php file called from a modal window for the next process.
<script type="text/javascript">
$(document).ready(function(){
$('a.edit').click(function(){
var myrowID = $(this).data('rowid');
var sfid = $(this).data('sfid');
var company = $(this).data('company');
var dealerclass = $(this).data('dealerclass');
$(".modal-body #mysfid").val(sfid);
$(".modal-body #mycompany").val(company);
$(".modal-body #mydealerclass").val(dealerclass);
var myObject = {};
myObject.sfid = sfid;
myObject.company=company;
var myString = JSON.stringify(myObject);
var obj = jQuery.parseJSON(myString);
$(".modal-body #myjobject").val(obj.sfid);
});
});
From here I don't know how to pass it to php file. where I can use the json_decode function to get my required data to process. If this way is not correct then please suggest any other alternate way to achieve this. Please help me out. Thanks for help.
Here is the call from a modal in bootstrap HTML
<a class="btn btn-success" href="../BootLogin/Sales/updateprocess.php">Approved</a>
Latest update in updateprocess.php file but still no display.
$_POST['obj'];
echo 'var dump'; var_dump($_POST);
$jsondata=$_POST['obj']; //sets all data curiously to null, bug ?
$data = json_decode($_POST['obj']);
echo 'decoded data';
print_r($data);
echo 'END';
exit();
here is the jquery script for posting JSON
<script type="text/javascript">
$(document).ready(function(){
$('a.edit').click(function(){
var myrowID = $(this).data('rowid');
var sfid = $(this).data('sfid');
var company = $(this).data('company');
var dealerclass = $(this).data('dealerclass');
$(".modal-body #mysfid").val(sfid);
$(".modal-body #mycompany").val(company);
$(".modal-body #mydealerclass").val(dealerclass);
var myObject = {};
myObject.sfid = sfid;
myObject.company=company;
var myString = JSON.stringify(myObject);
var obj = jQuery.parseJSON(myString);
$(".modal-body #myjobject").val(obj.sfid);
$.post("updateprocess.php");
});
});
I have tested with the path of file as well but still (0)
my updateprocess.php has the following code now
$_POST['obj'];
echo $_POST['obj'];
echo 'var dump';
var_dump($_POST);
//$jsondata=$_POST['obj']; //sets all data curiously to null, bug ?
//$data = json_decode($_POST['obj']);
//var_dump($data);
echo 'decoded data';
// print_r($data);
echo 'END';
exit();
Here is the output.
var dumparray(0) { } decoded dataEND
I have updated as suggested but still the display is (0)
<script type="text/javascript">
$(document).ready(function(){
$('a.edit').click(function(){
var myrowID = $(this).data('rowid');
var sfid = $(this).data('sfid');
var company = $(this).data('company');
var dealerclass = $(this).data('dealerclass');
$(".modal-body #mysfid").val(sfid);
$(".modal-body #mycompany").val(company);
$(".modal-body #mydealerclass").val(dealerclass);
var myObject = {};
myObject.sfid = sfid;
myObject.company=company;
var myString = JSON.stringify(myObject);
var obj = jQuery.parseJSON(myString);
$(".modal-body #myjobject").val(obj.sfid);
});
});
var dataString = 'obj=' +obj
$.ajax({
type: 'POST',
url: 'updateprocess.php',
data: dataString,
success: function() {
console.log('POST successful.');
}
});
</script>
Updated Javascript Ajax code.
<script type="text/javascript">
var passSFID;
$(document).ready(function(){
$('a.edit').click(function(){
var myrowID = $(this).data('rowid');
var sfid = $(this).data('sfid');
var company = $(this).data('company');
var dealerclass = $(this).data('dealerclass');
$(".modal-body #mysfid").val(sfid);
$(".modal-body #mycompany").val(company);
$(".modal-body #mydealerclass").val(dealerclass);
passSFID = sfid;
$('a.approve').click(function(){
var postData = passSFID;
alert(postData); // Data prints at this point as required
$.ajax({
type: "POST",
datatype: "json",
data: {mydata:postData},
url: "backend.php",
success: function(data){
console.log('Data Passed');
},
error: function(e){
console.log("Data passed failed");
}
});
});
});
});
</script>
var myString = JSON.stringify(myObject); var obj = jQuery.parseJSON(myString);
Um, what exactly are you trying to do? – Izkata