John Davidson

html - On clicking the search button , The selected option is not getting inserted to my PHP file and hence the query is not getting executed

0 comments
Message:


No response on clicking search button . Nor does the URL transfers to localhost/homeservices/searchproviders.phpI am working on my project ,I want to transfer the selected values of city and profession to searchprovider.php file . I think the control is not getting transferred to searchprovider.php because i tried an alert script to check if it works ! But it didn't . Can someone please help me out !! Also i have connected to mysql using PDO method .


Thanks in advance !


Here's my index.php [C:\xampp\htdocs\homeservices\index.php]


<?php

include_once "./include/header.php";



$cities = ["Ahmednagar", "Akola", "Akot", "Amalner", "Ambejogai", "Amravati", "Anjangaon", "Arvi", "Aurangabad", "Bhiwandi", "Dhule", "Kalyan-Dombivali", "Ichalkaranji", "Kalyan-Dombivali", "Karjat", "Latur", "Loha", "Lonar", "Lonavla", "Mahad", "Malegaon", "Malkapur", "Mangalvedhe", "Mangrulpir", "Manjlegaon", "Manmad", "Manwath", "Mehkar", "Mhaswad", "Mira-Bhayandar", "Morshi", "Mukhed", "Mul", "Greater Mumbai*", "Murtijapur", "Nagpur", "Nanded-Waghala", "Nandgaon", "Nandura", "Nandurbar", "Narkhed", "Nashik", "Navi Mumbai", "Nawapur", "Nilanga", "Osmanabad", "Ozar", "Pachora", "Paithan", "Palghar", "Pandharkaoda", "Pandharpur", "Panvel", "Parbhani", "Parli", "Partur", "Pathardi", "Pathri", "Patur", "Pauni", "Pen", "Phaltan", "Pulgaon", "Pune", "Purna", "Pusad", "Rahuri", "Rajura", "Ramtek", "Ratnagiri", "Raver", "Risod", "Sailu", "Sangamner", "Sangli", "Sangole", "Sasvad", "Satana", "Satara", "Savner", "Sawantwadi", "Shahade", "Shegaon", "Shendurjana", "Shirdi", "Shirpur-Warwade", "Shirur", "Shrigonda", "Shrirampur", "Sillod", "Sinnar", "Solapur", "Soyagaon", "Talegaon Dabhade", "Talode", "Tasgaon", "Thane", "Tirora", "Tuljapur", "Tumsar", "Uchgaon", "Udgir", "Umarga", "Umarkhed", "Umred", "Uran", "Uran Islampur", "Vadgaon Kasba", "Vaijapur", "Vasai-Virar", "Vita", "Wadgaon Road", "Wai", "Wani", "Wardha", "Warora", "Warud", "Washim", "Yavatmal", "Yawal", "Yevla"];
?>

<h2 class="text-center" style="margin-top: 20px">Home Services</h2>
<hr>
<div class="container" style="margin-top:20px; margin-bottom: 60px;">

<form method="post" action="scripts/searchproviders.php">
<div class="row">
<div class="form-group col-5">
<label for="">City</label>
<select class="form-control" name="city" id="city">
<option value="none">-- Select City --</option>
<?php foreach ($cities as $city) : ?>
<option value="<?= $city ?>"> <?= $city ?>
</option>
<?php endforeach; ?>
</select>
</div>

<div class="form-group col-5">
<label for="">Who's Required </label>
<select class="form-control" name="profession" id="profession">
<option value="none" name="profession">-- Select Profession --</option>
<option value="electrician" name="profession">Electrician</option>
<option value="plumber" name="profession">Plumber</option>
<option value="mobile" name="profession">Beautician</option>
<option value="mobile" name="profession">Maid</option>
<option value="mobile" name="profession">Mobile Repairer</option>
<option value="mobile" name="profession">Photographer</option>
<option value="mobile" name="profession">Carpenter</option>
</select>
</div>

<div class="form-group col-2">
<label for="">Action</label>
<input type="button" id="search" class="form-control btn btn-success" type="button" value="Search"></input>
</div>
</div>
</form>

<div class="table-responsive">
<table id="providers" class="table">
<thead>
<tr>

<th>Name</th>
<th>Address</th>
<th>Profession</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Select city and profession..</td>
</tr>
</tbody>
</table>
</div>
</div>



<?php include_once "./include/footer.php";?>

///Here's my searchproviders.php [C:\xampp\htdocs\homeservices\scripts\searchproviders.php]


<!doctype html>
<html>

<?php

require_once 'helpers.php';
echo'<script>alert("Hiii")</script>';
if (isset($_POST['city']) && isset($_POST['profession'])) {


$city = $_POST['city'];
$profession = $_POST['profession'];
$servername ="localhost";
$username ="root";
$password ="sarasaeed";
$dbname="services";

try {
$conn = new PDO('mysql:host=localhost;dbname=services; charset=utf8','root','sarasaeed');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM providers WHERE city='$city' AND profession='$profession'";
$statement = $conn->prepare($sql);
$statement->execute();
$providers = $statement->fetchAll();
if($providers){
foreach($providers as $key=>$value) {
echo "<html>";
echo "<div class='table-responsive'>";
echo "<tr>";
echo "<td>$providers->name</td>"."<td>$providers->adder1</td>"."<td>$providers->profession</td"."<td><a href='booking.php?provider=></td>";
}
}else{
echo"NO PROVIDERS FOUND";
}
}catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
$conn = null;
}
}
?>
</html>



My Answer:
It looks like the issue might be with the way you are handling the form submission in your index.php file.

Instead of using an input type of button for the search button, you should use type="submit" to submit the form data to the searchproviders.php file.

Here's the updated code for your search button in index.php:

html



By changing the input type to submit, when the user clicks the search button, the form will be submitted and the selected values of city and profession will be sent to the searchproviders.php file.

Additionally, make sure that your form tag in index.php has the correct action attribute pointing to the searchproviders.php file:

html



With these changes, when the user selects a city and profession and clicks the search button, the form data should be submitted to the searchproviders.php file and the query should be executed.

Let me know if you need further assistance!

Rate this post

5 of 5 based on 5600 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us