Skip to content
Advertisement

How to create html form to perform php query and retrieve results

I need to create a form with 5 fields in which a number will be entered in each, clicking a button a query is performed out based on the values entered in the fields and display the results within an html page.

Thanks.

Advertisement

Answer

I achieved what I was looking for with the following:

<?php


echo "<table style='border: solid 1px black;'>";
echo "<tr>
<th>F1</th>
<th>F2</th>
<th>F3</th>
<th>F4</th>
<th>F5</th>
</tr>";


class TableRows1 extends RecursiveIteratorIterator {
function __construct($it1) {
    parent::__construct($it1, self::LEAVES_ONLY);
}

function current() {
    return "<td style='width: 70px;'>" . parent::current(). "</td>";
}

function beginChildren() {
    echo "<tr>";
}

function endChildren() {
    echo "</tr>" . "n";
}
}

if( isset($_POST['submit']) )
{
    $feature = $_POST['F1'];
    $feature2 = $_POST['F2'];
    $feature3 = $_POST['F3'];
    $feature4 = $_POST['F4'];
    $feature5 = $_POST['F5'];
};



$feature = $_POST['F1'];
$feature2 = $_POST['F2'];
$feature3 = $_POST['F3'];
$feature4 = $_POST['F4'];
$feature5 = $_POST['F5'];

$values = [$feature, $feature2, $feature3, $feature4, $feature5];


$servername = "";
$username = "";
$password = "";
$dbname = "";

try {


$conn1 = new PDO("mysql:host=$servername;dbname=$dbname", $username, 
$password);
$conn1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt1 = $conn1->prepare(

"SELECT
F1, 
F2, 
F3, 
F4, 
F5
FROM table
WHERE
F1 = ?
AND
F2 = ?
AND
F3 = ?
AND
F4 = ?
AND
F5 = ?");

$stmt1->bindParam(1, $feature, PDO::PARAM_INT);
$stmt1->bindParam(2, $feature2, PDO::PARAM_INT);
$stmt1->bindParam(3, $feature3, PDO::PARAM_INT);
$stmt1->bindParam(4, $feature4, PDO::PARAM_INT);
$stmt1->bindParam(5, $feature5, PDO::PARAM_INT);

$stmt1->execute();


// set the resulting array to associative
$result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);

foreach(new TableRows1(new RecursiveArrayIterator($stmt1->fetchAll())) as 
$k1=>$v1) {
    echo $v1;
}
}
catch(PDOException $e1) {
echo "Error: " . $e1->getMessage();
}
$conn1 = null;
echo "</table>";

if (condition) {
      
      echo '';
    } 

elseif (condition ) {
    
    echo '';
       
}

?>

The form and javascript:

<form align = "center" action="" method="POST" id = "form">
    <input name="F1"  type = "number" min="1" max="34" step="1"/> 
    <input name="F2"  type = "number" min="2" max="35" step="1"/>
    <input name="F3"  type = "number" min="3" max="36" step="1"/> 
    <input name="F4"  type = "number" min="4" max="37" step="1"/>
    <input name="F5"  type = "number" min="5" max="38" step="1"/>
    <br>
    <br>
  </form>

  <script type="text/javascript">
  $(document).ready(function() {
  $(".mybutton").click(function() {

    $.ajax({
        type: "post",
        url: "checadorRetro.php",
        data: $("form").serialize(),
        success: function(result) {

            $(".myresult").html(result);
        }
    });

    });
    });
   </script>

Result and button:

 <div class="myresult"></div>
 <div class = "buscar">
 <button id="display" class="mybutton">Revisar</button>
 </div>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement