Quantcast
Channel: Programming | FettesPS
Viewing all articles
Browse latest Browse all 31

Ajax – Post Multiple Select Values

$
0
0

So this form is pretty basic, it has a select box (list box) filled with possible values. You can select all or some of the values and then submit it to the server via Ajax. The problem was when I grabbed the value from the select box I only got the last value selected, not each. Regardless of if I was using POST or GET I still had to build the parameter list since that’s how XMLHttpRequest works. So I looped through the list and tried using the same parameter name for each, then on the PHP end I had the same result — each parameter overwrote the previous one and I saw only the last result. It wasn’t until I stumbled upon this post that someone gave a decent suggestion, which was to put a [] at the end of each parameter name when building the query string and PHP would automatically interpret it as an array. Since his code was somewhat over complicated I thought I’d share my version of it:

test-utility.php

<html>
<head>
    <title>Test Utility</title>
    <script type="text/javascript" src="test-utility.js">	</script>
</head>
<body>
    <h1 align='center'>Test Utility</h1>

<center>

    <p style="width:450px; text-align:justify;<?php if($error) echo "color:red;" ?>">
        <?php if($error) { ?>
            <strong>Error:</strong> <?php echo $error; ?>
        <?php } ?>
    </p>

    <form name='frmTestUtility' id='frmTestUtility' method='post' action=''>
    <table cellpadding="2" cellspacing="2" border="0">
    <tr>
        <td>
            Listbox(s):<br><br>
            <div style="font-size:smaller">(Hold CTRL to<br>select multiple)</div>
        </td>
        <td align="center">
            <select name='listbox[]' id='listbox' multiple="multiple">
                <option value='123'>123</option>
                <option value='abc'>abc</option>
                <option value='alpha'>alpha</option>
                <option value='beta'>beta</option>
                <option value='gamma'>gamma</option>
            </select>
            <br>
            <input type="button" name="all" value="All" onClick='javascript:select_all();'>
            <input type="button" name="none" value="None" onClick='javascript:select_none();'>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" name="submit" onclick="javascript:run_queries();" value="Run SQL" />
        </td>
    </tr>
    </table>
    </form>

    <div id="ajax_results">
        &nbsp;
    </div>
    </center>
</body>
</html>

test-utility.js

 
function select_all() {
    var d = document.getElementById("listbox");
        for(var i=0;i<d.length;i++) {
            d.options[i].selected = "1";
        }
}

function select_none() {
    var d = document.getElementById("listbox");

    for(var i=0;i<d.length;i++) {
        d.options[i].selected = "";
    }
}

// Build the query string and submit it to the next page for processing
function run_queries() {
    var xmlhttp = "";
    var url = "";

    // For modern browsers
    if(window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();

    // for IE 5/6
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    //  Rebuild the array of selected option boxes
    lb = document.getElementById("listbox");
    for(var i=0; i < lb.length; i++) {
        if(lb[i].selected) {
            // Note the [] after the name
            url += "&lb[]=" + lb[i].value;
        }
    }

    xmlhttp.open("GET","test-utility-ajax.php?" + url, false);
    xmlhttp.send();

    if (xmlhttp.status == 200) {
        document.getElementById("ajax_results").innerHTML = xmlhttp.responseText;
    } else {
        return false;
    }

    return false;
}

test-utility-ajax.php

<?php
  // As you can see PHP displays it as an array
  if($_GET} print_r($_GET);
?>

Viewing all articles
Browse latest Browse all 31

Trending Articles