1

I'm pretty lost, all I want to do is spend a array to my javascript code. I've seen a lot of examples, but none ends me working. I need some help. I'm pretty stuck.

functions.js

function cargaTemperatura(idsem){
    $.ajax({
        type: "POST",
        url: "load_temp.php",
        data: { idsem :  idsem },
        dataType: "json",
        success: function(data){
            alert(JSON.parse(data));
        },error: function(data){
            alert('error');
        }
    });
};

load_temp.php

<?php
include_once 'Includes/db.php';
$select = "SELECT * FROM CalTmp WHERE idsem='".$_POST['id_sem']."'";
$result = mysqli_query($link, $select) or die($select);
$array = mysqli_fetch_all($result,MYSQLI_ASSOC);
debug_to_console($result->num_rows);
debug_to_console($_POST['id_sem']);
echo json_encode($array);

2
  • 1
    id_sem != idsem Commented Aug 14, 2016 at 20:19
  • Warning: Your code is wide open to SQL Injection attacks. Commented Aug 14, 2016 at 20:30

2 Answers 2

1

My suggestion:
- Step 1: In load_temp.php. Hardcode idsem value. Check on browser. Make sure there has a json string
- Step 2: Add this line

header("Content-type: application/json");

after

<?php
Sign up to request clarification or add additional context in comments.

Comments

0

Solved!!

I've resolved through library MooTools. My final code is:

Functions.js

function cargaTemperatura(idsem){
    var idsemJSON = JSON.encode(idsem);
    alert(idsemJSON);
    var miAjax = new Request({
        url: "carga_temp.php",
        data: "idsem=" + idsemJSON,
        onSuccess: function(data){
            alert(data);
        },
        onFailure: function(){
            $('resultado').set("html", "fallo en la conexion Ajax");
        }
    });
    miAjax.send();
};

load_temp.php

<?php
$misDatosJSON = json_decode($_POST["idsem"]);

include_once 'Includes/db.php';
$select = "SELECT * FROM CalTmp WHERE idsem='".$misDatosJSON."'";
$result = mysqli_query($link, $select) or die($select);
$array = mysqli_fetch_all($result);
debug_to_console($result->num_rows);
debug_to_console($_POST['id_sem']);
echo json_encode($array);

I hope someone will serve useful. Certainly the mistake was in sending the variable that was not in JSON format.

Thanks,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.