Home>

I'm new to this business. I don't really understand about ajax, and it was necessary to somehow return the values ​​from the database and fill in the client's fields with them. Please do not throw your slippers. ((

I am trying to organize a request. When you click on select in the list, the function of sending the request is triggered. The request comes to the server, where the response is generated as an array, which I am trying to convert to JSON ...

if (getcheck ($ _ POST ['getProduct'])== true)
{
        $ id= getProduct;
        db_connect ();
        $ result= mysql_query ("SELECT * FROM products WHERE id= '$ id'");
        $ productInfo= get_to_array ($ result);
        echo 'var arr=' .json_encode ($ productInfo);
}

and then return ...

var arr= {
        title: "",
        description: "",
        price: "",
        photo: "",
        category: ""
    };
function select ()
{
        arr= [];
        var objSel= document.getElementById ('spisok');
        //alert(objSel.options [objSel.selectedIndex ].value);
        var hR= ('v'== '\ v')? new ActiveXObject ('Microsoft.XMLHTTP')
                   : new XMLHttpRequest ();
                   var params= 'getProduct=' + encodeURIComponent (objSel.options [objSel.selectedIndex] .value);
        hR.open ("POST", 'function_db.php?', true);
        hR.setRequestHeader ('Content-Type', 'application /x-www-form-urlencoded');
        hR.onreadystatechange= function ()
            {if (hR.readyState== 4) getJSON (hR.responseText);}
        hR.send (params);
        //getJSON(hR.responseText)
}

Nothing is output in response. ((

  • Answer # 1

    To return JSON, must return JSON.
    JSON, not javascript. Therefore, we need to remove unnecessaryvar arrand make it simple

    echo json_encode ($ productInfo);
    

    And, of course, watch the answer in the developer's console.

    I would like to take this opportunity to advertise PDO to replace obsolete mysql functions. In addition to protecting against SQL injection, it also allows you to do without bicycles such asget_to_array ()as they are built into it out of the box.

    include 'pdo.php';
        $ stmt= $ pdo->
    prepare ("SELECT * FROM products WHERE id=?");
        $ stmt->
    execute ([$ id]);
        echo json_encode ($ stmt->
    fetchAll ());
    
    • less code, more security
  • Answer # 2

    To return JSON, must return JSON.
    JSON, not javascript. Therefore, we need to remove unnecessaryvar arrand make it simple

    echo json_encode ($ productInfo);
    

    And, of course, watch the answer in the developer's console.

    I would like to take this opportunity to advertise PDO to replace obsolete mysql functions. In addition to protecting against SQL injection, it also allows you to do without bicycles such asget_to_array ()as they are built into it out of the box.

    include 'pdo.php';
        $ stmt= $ pdo->
    prepare ("SELECT * FROM products WHERE id=?");
        $ stmt->
    execute ([$ id]);
        echo json_encode ($ stmt->
    fetchAll ());
    
    • less code, more security