2016-11-04 1 views
0


J'ai un fichier PHP qui me prend dans une table mes résultats.
En utilisant PHP, j'ai ajouté quelques filtres. J'aimerais pouvoir entrer dans le filtre avec le formulaire de recherche et non via le code.
Et, comment puis-je faire cela dans une page «statique» (pas d'actualisation ou de changement d'adresse)?Créer un formulaire HTML pour mon PHP ADODB

index.html (code <form>). Pour l'information, j'ai utilisé le style JQuery.

Screen

<h1>Ricerca Ordini</h1> 
<form action="index.php" method="post"> 
<input type="text" name="input" placeholder="Ricerca Ordini"> <br><br> 
<label for="selectmenu">Tipo:</label> 
<select id="selectmenu"> 
    <option>C1</option> 
    <option>CR</option> 
    <option>F1</option> 
    <option>FP</option> 
    <option>FPE</option> 
    <option selected="selected">All Type</option> 
</select> 
<label for="spinner">ID:</label> 
<input id="spinner"> <br><br> 
<label for="from">From</label> 
<input type="text" id="from" name="from"> 
<label for="to">to</label> 
<input type="text" id="to" name="to"> <br><br> 
<input type="submit" name="search"> 
</form> 

index.php

<?php 

define ('DBNAME',"./DinamicoWeb.mdb"); // Definisce il nome del database 
define ('DBTBL',"Ordini"); // Definisce il nome della tabella 
define ('PKNAME',"Id Ord"); // Definisce il nome della chiave primaria 
define ('PKCOL',0); // Definisce la posizione della chiave primaria 
define ('LINKPK',true); // Abilita i link alla PK per modifica-cancella 

$con = new COM("ADODB.Connection"); 
$conStr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". 
      realpath(DBNAME).";"; 
$con->open($conStr); 

$id="1"; 
$tipo=""; 
$numero="2"; 
$data1=""; 
$data2=""; 
$input="TT"; 


$sql="SELECT [Id Ord] AS [ID], [Tipo Ord] AS [Tipo], [N Ord] AS [Numero], [Data Ord] AS [Data], [Ragione Sociale], [Indirizzo], [TotImp] AS [IMPORTO TOTALE], [TotIva] AS [IMPORTO IVA] FROM [Ordini] WHERE [Indirizzo] LIKE '%$input%' OR [Ragione Sociale] LIKE '%$input%' OR [Id Ord] LIKE '$id' OR [Tipo Ord] LIKE '$tipo' OR [N Ord] LIKE '$numero'"; 

$rs = $con->execute($sql); 


if($rs === false) { 
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $con->ErrorMsg(), E_USER_ERROR); 
} else { 
    $rows_returned = $rs->RecordCount(); 
} 

$numFields = $rs->Fields->count; 

// Print HTML 
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; 
echo '<html xmlns="http://www.w3.org/1999/xhtml">'; 
echo '<head>'; 
echo '<meta http-equiv="Content-Type" 
    content="text/html; charset=utf-8" />'; 
echo '<title>Gestione degli '.DBTBL.'</title>'; 
echo '<link href="styles.css" rel="stylesheet" type="text/css" />'; 
echo '<link rel="stylesheet" href="css/bootstrap.css">'; 
echo '<link rel="stylesheet" href="css/footable.bootstrap.css">'; 
echo '<link rel="stylesheet" href="css/footable.bootstrap.min.css">'; 
echo '<link rel="stylesheet" href="css/footable.core.bootstrap.min.css">'; 
echo '</head><body>'; 
echo '<h1>GESTIONE '.DBTBL.'</h1>'; 
// Elenca records ----- 
//echo ("<div class='table-responsive'>"); 
echo ("<table class='datatable table tabella_reponsive ui-responsive' summary='Prova dati con MS Access'>"); 
echo("<caption>Tabella ".DBTBL."</caption>\n"); 
echo("<thead><tr>\n"); 
for ($i=0;$i<$numFields;$i++){ 
    echo("<th scope='col'>"); 
    echo $rs->Fields($i)->name; 
    echo("</th>\n"); 
} 
echo("</tr></thead>\n"); 
echo("<tbody>"); 

$alt = false; 
while (!$rs->EOF) 
{ 
    echo("<tr>"); 
    for ($i=0;$i<$numFields;$i++){ 
     $altClass = $alt ? " class='alt'" : ""; 
     if (LINKPK && $i==PKCOL){ 
     echo "<td".$altClass."><a href='?id=".$rs->Fields($i)->value 
       ."'>".$rs->Fields($i)->value."</a></td>\n"; 
     } 
     else{ 
     echo "<td".$altClass.">".$rs->Fields($i)->value."</td>\n"; 
     } 
    } 
    echo("</tr>\n");  
    $rs->MoveNext(); 
    $alt = !$alt; 
} 
echo("</tbody>"); 
echo("</table>\n"); 
echo("</div>"); 
echo '</body></html>'; 
$rs->Close(); 
$con->Close(); 
?> 

J'utilise ces variables pour faire le test, et il fonctionne correctement.

$ id = "1";
$ tipo = "";
$ numero = "2";
$ data1 = "";
$ data2 = "";
$ input = "TT";

Result

Insérer un extrait: (index.html)

<!doctype html> 
 
<html lang="it"> 
 
<head> 
 
<meta charset="utf-8"> 
 
<title>Ricerca Ordini</title> 
 
<link href="jquery-ui.css" rel="stylesheet"> 
 
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
 
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
 
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 
    <script> 
 
    $(function() { 
 
    var dateFormat = "mm/dd/yy", 
 
     from = $("#from") 
 
     .datepicker({ 
 
      defaultDate: "+1w", 
 
      changeMonth: true, 
 
      changeYear: true, 
 
      numberOfMonths: 1 
 
     }) 
 
     .on("change", function() { 
 
      to.datepicker("option", "minDate", getDate(this)); 
 
     }), 
 
     to = $("#to").datepicker({ 
 
     defaultDate: "+1w", 
 
     changeMonth: true, 
 
     changeYear: true, 
 
     numberOfMonths: 1 
 
     }) 
 
     .on("change", function() { 
 
     from.datepicker("option", "maxDate", getDate(this)); 
 
     }); 
 
    
 
    function getDate(element) { 
 
     var date; 
 
     try { 
 
     date = $.datepicker.parseDate(dateFormat, element.value); 
 
     } catch(error) { 
 
     date = null; 
 
     } 
 
    
 
     return date; 
 
    } 
 
    }); 
 
    </script> 
 
\t <style> 
 
\t body{ 
 
\t \t font-family: "Trebuchet MS", sans-serif; 
 
\t \t margin: 50px; 
 
\t } 
 
\t .demoHeaders { 
 
\t \t margin-top: 2em; 
 
\t } 
 
\t #dialog-link { 
 
\t \t padding: .4em 1em .4em 20px; 
 
\t \t text-decoration: none; 
 
\t \t position: relative; 
 
\t } 
 
\t #dialog-link span.ui-icon { 
 
\t \t margin: 0 5px 0 0; 
 
\t \t position: absolute; 
 
\t \t left: .2em; 
 
\t \t top: 50%; 
 
\t \t margin-top: -8px; 
 
\t } 
 
\t #icons { 
 
\t \t margin: 0; 
 
\t \t padding: 0; 
 
\t } 
 
\t #icons li { 
 
\t \t margin: 2px; 
 
\t \t position: relative; 
 
\t \t padding: 4px 0; 
 
\t \t cursor: pointer; 
 
\t \t float: left; 
 
\t \t list-style: none; 
 
\t } 
 
\t #icons span.ui-icon { 
 
\t \t float: left; 
 
\t \t margin: 0 4px; 
 
\t } 
 
\t .fakewindowcontain .ui-widget-overlay { 
 
\t \t position: absolute; 
 
\t } 
 
\t select { 
 
\t \t width: 200px; 
 
\t } 
 
\t </style> 
 
\t 
 
</head> 
 

 
<body> 
 
<h1>Ricerca Ordini</h1> 
 
<form action="index.php" method="post"> 
 
<input type="text" name="input" placeholder="Ricerca Ordini"> <br><br> 
 
<label for="selectmenu">Tipo:</label> 
 
<select id="selectmenu"> 
 
\t <option>C1</option> 
 
\t <option>CR</option> 
 
\t <option>F1</option> 
 
\t <option>FP</option> 
 
\t <option>FPE</option> 
 
\t <option selected="selected">All Type</option> 
 
</select> 
 
<label for="spinner">ID:</label> 
 
<input id="spinner"> <br><br> 
 
<label for="from">From</label> 
 
<input type="text" id="from" name="from"> 
 
<label for="to">to</label> 
 
<input type="text" id="to" name="to"> <br><br> 
 
<input type="submit" name="search"> 
 
</form> 
 

 
<script src="external/jquery/jquery.js"></script> 
 
<script src="jquery-ui.js"></script> 
 
<script> 
 

 
$("#accordion").accordion(); 
 

 

 

 
var availableTags = [ 
 
\t "ActionScript", 
 
\t "AppleScript", 
 
\t "Asp", 
 
\t "BASIC", 
 
\t "C", 
 
\t "C++", 
 
\t "Clojure", 
 
\t "COBOL", 
 
\t "ColdFusion", 
 
\t "Erlang", 
 
\t "Fortran", 
 
\t "Groovy", 
 
\t "Haskell", 
 
\t "Java", 
 
\t "JavaScript", 
 
\t "Lisp", 
 
\t "Perl", 
 
\t "PHP", 
 
\t "Python", 
 
\t "Ruby", 
 
\t "Scala", 
 
\t "Scheme" 
 
]; 
 
$("#autocomplete").autocomplete({ 
 
\t source: availableTags 
 
}); 
 

 

 

 
$("#button").button(); 
 
$("#button-icon").button({ 
 
\t icon: "ui-icon-gear", 
 
\t showLabel: false 
 
}); 
 

 

 

 
$("#radioset").buttonset(); 
 

 

 

 
$("#controlgroup").controlgroup(); 
 

 

 

 
$("#tabs").tabs(); 
 

 

 

 
$("#dialog").dialog({ 
 
\t autoOpen: false, 
 
\t width: 400, 
 
\t buttons: [ 
 
\t \t { 
 
\t \t \t text: "Ok", 
 
\t \t \t click: function() { 
 
\t \t \t \t $(this).dialog("close"); 
 
\t \t \t } 
 
\t \t }, 
 
\t \t { 
 
\t \t \t text: "Cancel", 
 
\t \t \t click: function() { 
 
\t \t \t \t $(this).dialog("close"); 
 
\t \t \t } 
 
\t \t } 
 
\t ] 
 
}); 
 

 
// Link to open the dialog 
 
$("#dialog-link").click(function(event) { 
 
\t $("#dialog").dialog("open"); 
 
\t event.preventDefault(); 
 
}); 
 

 

 

 
$("#datepicker").datepicker({ 
 
\t inline: true 
 
}); 
 

 

 

 
$("#slider").slider({ 
 
\t range: true, 
 
\t values: [ 17, 67 ] 
 
}); 
 

 

 

 
$("#progressbar").progressbar({ 
 
\t value: 20 
 
}); 
 

 

 

 
$("#spinner").spinner(); 
 

 

 

 
$("#menu").menu(); 
 

 

 

 
$("#tooltip").tooltip(); 
 

 

 

 
$("#selectmenu").selectmenu(); 
 

 

 
// Hover states on the static widgets 
 
$("#dialog-link, #icons li").hover(
 
\t function() { 
 
\t \t $(this).addClass("ui-state-hover"); 
 
\t }, 
 
\t function() { 
 
\t \t $(this).removeClass("ui-state-hover"); 
 
\t } 
 
); 
 
</script> 
 
</body> 
 
</html>

Répondre

0

Vous devez ajouter sur votre index.php ceci:

$input=$_POST['input']; 
$id=$_POST['id']; 
$tipo=$_POST['tipo']; 
$numero=$_POST['numero']; 

Et changer votre index.html:

<form action="index.php" method="post"> 
<input type="text" name="input" placeholder="Ricerca Ordini"> <br><br> 
<label for="selectmenu">Tipo:</label> 
<select id="selectmenu" name="tipo"> 
    <option>C1</option> 
    <option>CR</option> 
    <option>F1</option> 
    <option>FP</option> 
    <option>FPE</option> 
    <option selected="selected">All Type</option> 
</select> 
<label for="spinner">ID:</label> 
<input id="spinner" name="id"> <br><br> 
<label for="from">From</label> 
<input type="text" id="from" name="from"> 
<label for="to">to</label> 
<input type="text" id="to" name="to"> <br><br> 
<input type="submit" name="search"> 
</form> 

Le déposé "nom" sur HTML doit être le même de [ 'nom'] sur php.

+1

Merci! Ce code fonctionne parfaitement. Mais si je laisse vide le texte "input" et sélectionne "tipo" sur la combobox, sur php montrez-moi TOUS les résultats et pas seulement avec mon "Tipo" sélectionné. –