getting javascript array to php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rgren925
Forum Newbie
Posts: 4
Joined: Tue Nov 09, 2010 2:52 pm

getting javascript array to php

Post by rgren925 »

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:

Code: Select all

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:

Code: Select all

<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 '<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
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: getting javascript array to php

Post by cpetercarter »

Maybe I am missing something, but I don't see what your problem is. When the user clicks "OK" in the alert box, your javascript function will submit the form data to the script named in the "action" attribute in your form tag. The data from the form will be available to php in the $_POST array in the normal way.
rgren925
Forum Newbie
Posts: 4
Joined: Tue Nov 09, 2010 2:52 pm

Re: getting javascript array to php

Post by rgren925 »

The problem was that I didn't really understand the relationship between javascript variables and hidden variables on the php-generated form.
Makes sense now.
Post Reply