0

I have a php loop that generates several buttons.Each button changes the content of a specific div and update some stuff in the database by using an ajax request.When the button is click it calls a function that executes the ajax request.The problem is that I am not able to pass the Div id as parameter in the function if I concatenate it with a string. Only when I write $TickCrossDiv = $i it is working (only when using number as Div id it works).

Here is my code :

for($i=0;$i<count($PlanningArray);$i++){

        $TickCrossDiv = 'tickCrossDiv'.$i;

     echo "<button onclick=\"SetActDone(
        ".$PlanningArray[$i]'PlanID'].",
        ".$PlanningArray[$i]['ActID'].",
        ".$TickCrossDiv.")\" >
        Mark as done</button>"
}

Here is the function :

function SetActDone(PlanID,ActID,DivID)
    {
        $.ajax({

            type: "POST",
            url: 'testAjax.php',
            data: {PlanID:PlanID, ActID:ActID},
            success: function(data) {

                $("#" + DivID ).html('<p>Status: Done</p> <i style="color:greenyellow; " class="fa fa-check-circle fa-2x"></i>');
            }
        });
    }

I am getting the error :

Uncaught Error: Syntax error, unrecognized expression: #object HTMLDivElement

1 Answer 1

1

Without knowing what the values of $PlanningArray[$i][...] are I can't say for sure. But most likely you need to wrap your echoed variable in quotes. That would explain why a number would work, it will be treated as an integer rather than a string. Try this:

for($i=0;$i<count($PlanningArray);$i++){

    $TickCrossDiv = 'tickCrossDiv'.$i;

     echo "<button onclick=\"SetActDone(
        ".$PlanningArray[$i]['PlanID'].",
        ".$PlanningArray[$i]['ActID'].",
        '".$TickCrossDiv."')\" >
        Mark as done</button>"

}

I'm guessing that $PlanningArray[$i]['PlanID'] and $PlanningArray[$i]['ActID'] are also integers so they don't need to be wrapped in quotes.

I also fixed a typo on this line:

$PlanningArray[$i]'PlanID']

If your code works, that typo probably isn't in your real script.

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

1 Comment

Thanks a lot !!! it's working now, I wrap the $TickCrossDiv in quotes and yeah the $PlanningArray[$i]['PlanID'] and $PlanningArray[$i]['ActID'] are integers

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.