Jump to content

getting javascript array to php


rgren925

Recommended Posts

Hi.

I'm pretty new to php and javascript.

Perhaps I'm going about this all wrong, so any ideas will be appreciated...

 

I've got a php-generated form that calls an external javascript function (through onclick of button input) to validate an array of checkboxes.

If the user presses ok on the javascript confirm box, it does a submit of the form.

The checkboxes are a javascript, rather than php array:

 

echo "<input type=\"checkbox\" name=\"attuids\" value=\"$dirReportUid\" class=\"CheckField\">$dirReportUid<br>\n";

 

What I need to do is get the array of checked boxes from my javascript array into a php array in the php script called by the submit action.

 

Here's the javascript:

 

function CheckboxTest(list) {
    total = "";
    outArray = new Array();
    outCount = 0;

    for (i=0; i < list.length; i++) {
        if (list[i].checked) {
            total += list[i].value + "\n";
        }
    }

    if (total == "") {
        alert("No direct reports have been selected");
    }
    else {
        alertMessage = "You have selected the following users\n";
        alertMessage = alertMessage + "for Patrol Console deletion:\n\n";
        alertMessage = alertMessage + total + "\n";
        if (confirm(alertMessage)) {
            document.myForm.submit();
        }
    }
}

 

Here's the php:

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Patrol Console Audit</title>
<link href="css/ConsoleAudit.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="javascripts/CheckboxProcess.js"></script>
</head>

<body>
<h2>Patrol Console Audit</h2>

<?php

$patrolConsoleFile = "/home/rfg/ATT/console.userlist.csv";
$directReports = array();

$PATROLFILE = fopen("$patrolConsoleFile", "r");
$checkUid = "cr6228";

while (!feof($PATROLFILE)) {
    $fileLine = fgets($PATROLFILE);
    $patrolUserData = trim($fileLine);

    if (empty($patrolUserData)) {
        continue;
    }

    list (
        $mgrData,
        $mgrDirReportUid,
        $mgrDirReportName,
    ) = explode(',', $patrolUserData);

    list (
        $mgrName,
        $mgrUid,
        $mgrEmail,
    ) = explode(':', $mgrData);

    if ($mgrUid == $checkUid) {
        array_push($directReports, "$mgrDirReportUid $mgrDirReportName");
    }
}

$firstName = "Jerry";
$lastName = "Garcia";

if (count($directReports) > 0) {
    echo "<h3> Direct Reports for $firstName $lastName</h3>\n";
    echo '<form action="ProcessConsoleAudit.php" method="post" name="myForm">'."\n";
    echo '<input name="checkedBoxes" type="hidden", id="checkedBoxes" value=""/>';
    echo '<h4>Select users to retain Patrol Console access</h4>'."\n";
    echo "<fieldset class=\"InputFields\">\n";

    echo "<br><input type=\"checkbox\" name=\"CheckAll\" class=\"CheckField\" onclick=\"CheckUncheckAll(document.myForm.CheckAll,document.myForm.uids)\">Check All</br>\n";
    echo "<br></br>";

    foreach ($directReports as $dirReportUid) {
        echo "<input type=\"checkbox\" name=\"uids\" value=\"$dirReportUid\" class=\"CheckField\">$dirReportUid<br>\n";
    }

    echo "</fieldset>\n";
    echo "<div class=\"SubmitDiv\">\n";
    echo '<input type="button" value="submit" class="SubmitButton" onclick="CheckboxTest(document.myForm.uids)">'."\n";
    echo "</div>\n";
    echo "</form>\n";

}
else {
    echo "<br> No direct reports were selected<br>";
}

?>

</body>
</html>

 

Thanks very much for any comments and/or ideas,

Rick

 

Link to comment
https://forums.phpfreaks.com/topic/232354-getting-javascript-array-to-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.