0

I have made the array $latent_weights_array and I would like when I puss the button 'save' to run a php script via ajax passing the are as $_GET variable.

in PHP

<?php
    echo "<input type='button' class='btn'
             onclick='ajaxWeight(".json_encode($latent_weights_array).")' value='Save'/>";
?>  

in javascript

function ajaxWeight(latentweights){
    // trim code here

    var queryString = "?latentweights=" + latentweights;

    ajaxRequest.open("GET", "031instsql.php" + 
                              queryString, true);
    ajaxRequest.send(null);
}

in 031instsql.php

<?php
     if (isset($_GET['latentweights'])){
         echo $_GET['latentweights'];
         $kati=array();
         $kati=json_decode($_GET['latentweights'],true);
     }
?>

1.Why doesn't seem to work? 2.What does need to be done here?

Shaddow
  • 3,175
  • 3
  • 22
  • 42
Stam
  • 2,410
  • 6
  • 32
  • 58

3 Answers3

0

json_encode produces valid JavaScript code for array definition, so in you are passing an array to ajaxWeight. Inside it you are trying to concatenate it with a string, but JavaScript does not do any jsonification for you. See how to make JSON string in JS or if you don't need an actual JS object to perform any operation on it, you can double encode it on php side:

json_encode(json_encode($latent_weights_array))

This way, you will be passing string to ajaxWeight that can be concatenated into your URL.

Community
  • 1
  • 1
dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
  • I think that I have explained it in my answer... In short first encoding produces string that used in JS will be treated as array, second encoding of this string will produce string in JS, and only string can be concatenated. – dev-null-dweller Jun 23 '13 at 19:44
0

Looks like you're JavaScript ajax call should resemble this:

function ajaxWeight(latentweights){
    // trim code here

   xmlhttp.onreadystatechange=function()
   {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      // Deal with response
    }
  }

    var queryString = "?latentweights=" + latentweights;

    xmlhttp.open("GET", "031instsql.php" + queryString, true);
    xmlhttp.send();
}

Or better still, use jQuery

$.getJSON({
      url: "031instsql.php",
      {latentweights: latentweights})
.done(function(result){
 // Deal with result
 })
.fail(function( jqxhr, textStatus, errorResponse) {
    var error = textStatus + ', ' + errorResponse;
    console.log( "Request Failed: " + errorResponse);
 });

I think you also need to render $kati for the response from PHP.

Sparko
  • 735
  • 6
  • 15
0

You can achieve this using jQuery. Try this

<?php
    $latent_weights_array = array(1,2,3);
    echo '<input type="button" class="btn" onclick="ajaxWeight('.json_encode($latent_weights_array).')" value="Save"/>';
?> 


<script type="text/javascript">
    function ajaxWeight(latentweights){
        $.ajax({
            type: "GET",
            url: "031instsql.php",
            data: 'latentweights='+latentweights,
            success: function(html){
                alert(html);
            }
       });
    }
</script>

For more on jQuery AJAX READ THIS

Khawer Zeshan
  • 9,470
  • 6
  • 40
  • 63