开发者

How to submit multiple parameters in an HTML form on subsequent requests

I have a form that submits parameters using standard HTML controls to a PHP file. The PHP file then iterates through a csv file and returns the resuts via AJAX. If I select a dropdown menu from the form I get the data back no problem but when I then make another selection it doesn't remember what I have previously selected so only the new parameter gets submitted. How do I ensure the previous selected control(s) get submitted? Any ideas or suggestions greatly appreciated.

search.php:

<?php
error_reporting(E_ALL & ~E_NOTICE);
require_once('includes/MagicParser.php');
$key = $_GET['key'];
$search = $_GET['search'];
$counter = 0;
function recordHandler($record)
{
global $key;
global $search;
global $counter;

if ($record[$key] == $search) {
    if ($counter % 2) {
        print "<tr class=\"alt_row\">";
    } else {
        print "<tr>";
    }       
    print 开发者_运维百科"<td>".$record['Subject']."</td>";
    print "<td>".$record['Tutor']."</td>";      
    print "<td>".$record['Level']."</td>";
    print "<td>".$record['Course Type']."</td>";
    print "<td>".$record['Course Code']."</td>";
    print "<td>".$record['Primary Center']."</td>";
    print "<td>".$record['Lesson 1 Date']."</td>";
    print "<td><a href=\"#\"></a></td>";
    print "<td><a href=\"#\"></a></td></td>";
    print "</tr>";
} else {
    return;
}
$counter ++;
}

print "
<table id=\"results\">
<tr>
       <th>Subject</th>
        <th>Tutor</th>
        <th>Level</th>
        <th>Type</th>
        <th>Code</th>
        <th>Center</th>
        <th>Date</th>
        <th>Timetable</th>
        <th>Outline</th>
</tr>
";

MagicParser_parse("includes/course-data.csv", "recordHandler");

print "
</table>
<div id=\"pager_display\"></div>
";
?>

scripts.js:

function showCourse(search, key)
{
if (search == "") {
    document.getElementById("dynamic_display").innerHTML = "";
    return;
}

if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
} else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function()
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("dynamic_display").innerHTML = xmlhttp.responseText;
        pager = new Pager('results', 15);
        pager.init(); 
        pager.showPageNav('pager', 'pager_display'); 
        pager.showPage(1);
    }
}

xmlhttp.open("GET", "search.php?key="+ key +"&search=" + search, true);
xmlhttp.send();
}

/*
function disableEnableForm(form, boolean)
{
var formElements = form.elements;

for (i = 0; i < form.length; i ++) {
    formElements[i].disabled = boolean;
}
}
*/


I would store them in $_SESSION. However, even once you get that squared away your basic search logic doesnt allow for multiple parameters so youll need to reconfigure that as well.. It might look something like the following:

if(isset($_SESSION['search']))
{
   $search = $_SESSION['search'];
}
else
{
  $search = array();
}

$key = $_GET['key'];
$search[$key] = $_GET['search'];

// store updated search in array
$_SESSION['search'] = $search;

$counter = 0;
function recordHandler($record)
{
  global $key;
  global $search;
  global $counter;



  foreach($search as $key => $value)
  {
    if($record[$key] != $value)
    {
      // if the record doesnt have all values that match for $key then its not a match
      return;
    }
  }

    if ($counter % 2) {
        print "<tr class=\"alt_row\">";
    } else {
        print "<tr>";
    }       
    print "<td>".$record['Subject']."</td>";
    print "<td>".$record['Tutor']."</td>";      
    print "<td>".$record['Level']."</td>";
    print "<td>".$record['Course Type']."</td>";
    print "<td>".$record['Course Code']."</td>";
    print "<td>".$record['Primary Center']."</td>";
    print "<td>".$record['Lesson 1 Date']."</td>";
    print "<td><a href=\"#\"></a></td>";
    print "<td><a href=\"#\"></a></td></td>";
    print "</tr>";

   $counter ++;
}

This isnt by any means complete Im not sure exactly how your parser works and all that fun stuff - and this only supports an AND search - it will only return rows which match all supplied key-value pairs in the search. You would also need to implement something to clear the search parameters so the user can start over too. But you should be bale to get the basic idea...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜