Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Have you checked your console for errors? –  Matt Harrison Jun 23 '13 at 10:40
    
You can check your ajax request using Firebug or Chrome Developer tools. –  brpaz Jun 23 '13 at 10:44
    
no errors in my console –  Stam Jun 23 '13 at 10:52

3 Answers 3

up vote 0 down vote accepted

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.

share|improve this answer
    
I don't know why... but it worked! –  Stam Jun 23 '13 at 19:20
    
Could you explain me why it worked with double encode? –  Stam Jun 23 '13 at 19:26
    
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

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.