1

I am new to Ajax and PHP and hope someone can help me with this.

I need to generate an array with unique IDs in jQuery, pass it to a PHP page and return the corresponding values from the PHP page.

So far I have the following code which works on the jQuery side so the first console.log logs the input array correctly, e.g. ["1", "2", "3", "4", "5"].

However, I am not getting any results from the PHP page and the second console.log logs nothing for the output array so I guess I am missing one or more things there. The result should be an associative array where each item is listed with its tID and the corresponding value from the MySQL query, like "tID" => "1", "value" => "value1" ... .

Can someone help me with this ?

JS side:

function getValues(languageFrm){
    var values = [],
        tID;

    $('.valuesDynamic').find('.values').each(function(){
        tID = $(this).attr('name').replace('tID', '');
        if($.inArray(tID, values) === -1){
            values.push(tID);
        }
    });
    values.sort();
    console.log(values);

    $.ajax({        
        type: "post",   
        url: "ajax.php",
        cache: "false",
        data: {
            node: 'fetchValues',
            values: values,
            languageFrm: languageFrm
        },
        success: function(data){
            console.log(data);
        }
    });
}

PHP side:

case "fetchValues":
    $values = $_POST["values"];
    $languageFrm = $_POST["languageFrm"];

    $stmt = $conn->prepare("SELECT tID, " . $languageFrm . " FROM main WHERE tID IN(?) ORDER BY tID");
    $stmt->bind_param("s", implode(",", $values));
    $stmt->execute();
    $result = $stmt->get_result();
    while($arrValues = $result->fetch_assoc()){
        $result[] = array("tID" => $arrValues["tID"], "value" => $arrValues[$languageFrm]);
    }

    echo $result;
    break;

Many thanks for any help.

3 Answers 3

2

You could JSON encode you array and then JSON decode on PHP side.

For your JS side, you would replace your data section with:

 data: {
            node: 'fetchValues',
            values: JSON.stringify(values),
            languageFrm: languageFrm
        },

for PHP. instead of $values = $_POST["values"], you would do:

$values = json_decode($_POST["values"]);
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for this ! Will give it a try. I read about this in some other posts and it seemed to me that jQuery would do this automatically so that stringify wouldn't be needed here.
Not sure actually if this is the case, but you can easily check by var_dumping that variable. var_dump($_POST["values"]); This will show you precisely what is inside and if in fact it is already json encoded, you can simply leave it as is and do only json_decode on PHP side.
I tried your approach but it doesn't change anything for me so the result is still empty in this case.
What does var_dump return?
Are $_POST['languageFrm'] and $_POST['node'] there when you dump them?
|
1

I believe your PHP SIDE should be

case "fetchValues":
$values = implode(",", $_POST["values"]);
$languageFrm = $_POST["languageFrm"];

$stmt = $conn->prepare("SELECT tID, " . $languageFrm . " FROM main WHERE tID IN($values) ORDER BY tID");
$stmt->execute();
$result = $stmt->get_result();
while($arrValues = $result->fetch_assoc()){
    $result[] = array("tID" => $arrValues["tID"], "value" => $arrValues[$languageFrm]);
}

echo $result;
break;

In other words you must put $values directly in your query

2 Comments

Thanks for this - would that still prevent SQL injection ?
I tried this and it returns a fatal error "Call to a member function execute() on a non-object...".
0

JS Side

data: {
            node: 'fetchValues',
            values: $.extend({},values),
            languageFrm: languageFrm
        },

PHP Side

$result is an array. so replace echo $result; with print_r($result);

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.