0

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.

Pakin
  • 168
  • 1
  • 3
  • 16
  • `each field is a column in a mysql database` this kind of statement is synonymous with problems of poor design – Strawberry Nov 06 '20 at 17:48
  • Whenever you are at the early stages of programming I strongly recommend to **not** use `try/catch`, and to instead [make sure error reporting](https://stackoverflow.com/a/5438125/231316) is set to alert you about everything, otherwise small errors can take a while to diagnose. Exceptions are meant for developers to see and handle. This has a code-smell of trying too much at one. I'm seeing AJAX, jQuery and a class for creating HTML. I would recommend simplifying this greatly, get that working, and then add more complex features later. – Chris Haas Nov 06 '20 at 18:28
  • So you want one thing, but you posted code that does something else. Needs More Focus. – mickmackusa Nov 06 '20 at 20:46

1 Answers1

0

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>
Pakin
  • 168
  • 1
  • 3
  • 16