0
votes

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>
2
var myString = JSON.stringify(myObject); var obj = jQuery.parseJSON(myString); Um, what exactly are you trying to do?Izkata
could be trying to clone the object? why? I don't knowb.kelley
Thanks for your time to look in to my problem. I just want to pass json object to my php file for other process. I am not getting any data in var_dump(_POST['obj'] in my updateprocess.php file. it displays var_dump(0). any suggestionAzam

2 Answers

0
votes

You could use post to handle the json and return you with what you need

0
votes

You can use $.post() to POST the json to your PHP file and have it parse the $_POST data:

http://api.jquery.com/jQuery.post/

You don't look to be POSTing correctly. Try this, it'll log to console to tell you you've POSTed correctly. I assume that your obj variable is the one you want to be posting with the name "obj"?

var dataString = 'obj=' +obj

$.ajax({  
  type: 'POST',  
  url: 'updateprocess.php',  
  data: dataString,  
  success: function() {  
    console.log('POST successful.');
  }  
});

Now updateprocess.php should be able to get the $_POST['obj'] variable.