0

I'm making a page where a user can select all of their services that are affected by an outage and set their status.

The way it works is, I have them check a checkbox, after the checkbox is checked the user is presented with a selection of statuses. Now I can get all of the affected services but when I try to get their status things don't work at all. Basically what I'm trying to achieve is that the system sets the new status for every affected service. I have also tried different approaches and my most recent one works if only one service is selected and it is just a mess.

HTML Code:

<label style="width: 100%;">
    <input type="hidden" name="id[]" value="Service ID">
    <input type="checkbox" class="affectedChecker" name="affected[]" value="Service ID">
    <span>Service Title</span>
    <select name="status[]" class="form-control animated--grow-in ml-2" style="width: 30%; display: inline;" hidden="hidden">
        <option value="Operational">Operational</option>
        <option value="Degraded Performance">Degraded Performance</option>
        <option value="Minor Outage">Minor Outage</option>
        <option value="Partial Outage">Partial Outage</option>
        <option value="Major Outage>Major Outage</option>
    </select>
</label>

Current PHP code (Works with only one service and is a complete mess):

if(!empty($_POST['id'])) {
    foreach($_POST['id'] as $id) {
        if(!empty($_POST['affected'])) {
            if(in_array($id,$_POST['affected'])){
                if(!empty($_POST['status'])) {
                    foreach($_POST['status'] as $status) {
                        $cstatus->updateStatus($status, $page_id, $id);
                    }
                }
            }
        }
    }
}

Old PHP code (Doesn't work but I think there is just something simple I'm doing wrong here):

if(!empty($_POST['affected'])) { //Kui on valitud affected serviceid, siis
    $affected = implode(",",$_POST['affected']); //Paneb komadega järjestusse kõik servicid, mis valiti, need ongi need servicid, mis on affected
    $cstatus->createIncident($title, $incidentstatus, $message, $affected, $page_id, $cid); //Loob intsidendi andmebaasis
    if(isset($_POST['affected'])) {
        print_r($_POST); //It works until this point and gets the correct amount of affected services in the array
        foreach($_POST['affected'] as $value){ //Doesn't work anymore from here
            $id = $_POST['id'];
            $status = $_POST['status'];
            echo $status;
            $cstatus->updateStatus($status, $page_id, $id);
        }
    }
}

Thanks, Nimetu.

1
  • You use foreach($_POST['affected'] as $value), but don't use $value anywhere Commented Mar 1, 2022 at 19:26

1 Answer 1

2

[Opinionated] I think it's better to have default value of status as "Not Affected", so you should not create or confusing about 2 data at the same time. And also put id inside each status.

HTML :

<label style="width: 100%;">
    <span>Service Title</span>
    <select name="status[<?php echo $value->id; ?>]" class="form-control animated--grow-in ml-2" style="width: 30%; display: inline;">
        <option value="">Not Affected</option>
        <option value="Operational">Operational</option>
        <option value="Degraded Performance">Degraded Performance</option>
        <option value="Minor Outage">Minor Outage</option>
        <option value="Partial Outage">Partial Outage</option>
        <option value="Major Outage">Major Outage</option>
    </select>
</label>

PHP :

foreach($_POST['status'] as $key => $value) {
    if (empty($value)) {
        continue;
    }
    $cstatus->updateStatus($value, $page_id, $key);
}
Sign up to request clarification or add additional context in comments.

2 Comments

The status changing now works however I would like to keep the affected checkbox functionality as later I need to get a list of the services that are affected by the incident and getting that information using regular statuses is not possible
I would suggest to give your affected checkbox name like affected[<?php echo $value->id; ?>], so you can access that inside my foreach php answer like $_POST['affected'][$key]

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.