Results 1 to 8 of 8

Thread: getting javascript array to php

  1. #1
    Join Date
    Mar 2011

    getting javascript array to php

    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=\"uids\" 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 (n=0; n < list.length; n++) {
    if (list[n].checked) {
    total += list[n].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)) {

    Here's the php:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <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>

    <h2>Patrol Console Audit</h2>


    $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)) {

    list (
    ) = explode(',', $patrolUserData);

    list (
    ) = 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>";



    Thanks very much for any comments and/or ideas,

  2. #2
    Join Date
    Jul 2006

    So, it looks like you need help creating the ProcessConsoleAudit.php file.

    The first problem is, you're giving every checkbox the same name "uids".
    You should change this to uids[] (this will turn it into an array that is submitted to the next page)

    Since you are using post as the form method, this will grab the values in ProcessConsoleAudit.php
    $chkbx_array = $_POST['uids'];

    And this should help you debug the values to see what was checked:
    echo "<pre>\n";
    echo "</pre>\n";

    I haven't tested it, so let me know if you run into any problems

  3. #3
    Join Date
    Mar 2011
    Thanks for the reply irf2k.
    I had tried uids[], but then the javascript doesn't know what to do with it. That's the essence of the problem. By using uids (no brackets), it creates an array javascript can handle, but then how do I process it in ProcessConsoleAudit.php?

  4. #4
    Join Date
    Jul 2007
    why are you processing the array through javascript and not php?

  5. #5
    Join Date
    Mar 2011
    I really want both javascript and php.
    javascript for presenting a confirmation box and php for back-end processing.

    I've actually got it nailed now, though,
    I'm using toString to convert the javascript array to a (comma-separated) string and passing it back into the submitted form as a hidden input field.
    Works great.

    I really appreciate your input.

  6. #6
    Join Date
    Feb 2011
    You can use name="uids[]" for use it with php, and also add a class="uids" for use it with JavaScript.
    Then, in JS you need to get and parse the elements with class="uids"
    var get_tags = document.getElementsByTagName('input');
    var uidss = new Array();
    var u = 0;
      // traverse the get_tags array
      for (var i=0; i<get_tags.length; i++) {
        if (get_tags[i].className=='uids') {
          uidss[u] = get_tags[i];
    // ... then parse the uidss

  7. #7
    Join Date
    Mar 2011
    Makes sense.
    Thanks very much

  8. #8
    Join Date
    Mar 2007
    use json.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center