Alex Alex - 1 year ago 79
MySQL Question

How to submit form to same page and get results displayed

I have this form

<form action="" method="GET" id="formSearch">
<div class="input-group">
<input type="text" class="form-control" id="inputSearch" name="inputSearch"/>
<span id="spanBtnSearch" class="input-group-btn">
<input type="submit" class="btn btn-primary" id="btnSearch" value="Search"/>

I want to send this form on the same page and my connection and query is below

$conn = mysqli_connect("localhost:3311","root","root","compared");
$shingpoint = "SELECT * FROM products WHERE store LIKE 'shingpoint' AND title LIKE '%".$_POST['inputSearch']."%'";
$result = mysqli_query($conn,$shingpoint);

And the results that should show the results is this

<div class="row" id="divShingpoint">
<h2 class="section-title style2 text-left">Shingpoint</h2>
<div id="divProductThumbnail" class="item col-lg-4 col-md-4 col-sm-4 col-xs-6 col-xxs-12 colums">
<div class="container" id="divInnerContainer">
<?php while ($shingpointrow = mysqli_fetch_assoc($result)) {?>
<div class="product">
<div class="image"><img class="img-responsive" src="<?php echo $shingpointrow['img'] ?>" alt="<?php echo $shingpointrow['title']?>"/></div>
<div class="description"><h4 class="productname"><?php echo substr(trim($shingpointrow['title']),0,20) ?></h4></div>
<div class="price">
<span><?php echo $shingpointrow['price'] ?></span>
<input type="button" class="btn btn-primary btn-sm" value="Details"/>
<?php }?>

It does send data as the page refreshes and url is updated but results don't get displayed on the div with the title I want to search.

And I am getting this error

Notice: Undefined index: inputSearch in C:\wamp64\www\compareit.php on line 3

What is missing what am i doing wrong? Or there is any other method to accomplish the task?

Answer Source

You set the method to be GET but you try to read from $_POST.

So either change your <form method="POST"> or $_POST['inputSearch'] replace with $_GET['inputSearch'].

Other than that I would add some validation, use PDO and change few more things...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download