Can't get advanced search dropdown values to fill via PHP - php

I have an advanced form that looks like this:
I am using a template from Bootsnipp which contains my code, and am trying to adjust it - available here.
My method of doing search is to append values to specific URL parameters based on the search, and then further down in the PHP code, check if those parameters are blank or contain values, and then execute code which fetches auctions from a database.
Since I started playing around to do this, the "Filter By State" buttons are not filling in the states filters from the database (the only thing that appears is the blank option), and also my search buttons (with the eyeglass icons) have disappeared. No idea why as this was working before. Note I am using PDO. Here is HTML and PHP code for the filter section:
<!-- Page Content -->
<div class="container">
<!--This is the search bar-->
<div class="container">
<div class="row">
<div class="col-sm-12">
<!-- http://stackoverflow.com/questions/8476602/appending-get-parameters-to-url-from-form-action-->
<!--If this is a form it messes up the styling-->
<div method="get" class="input-group" id="adv-search" action="listings.php">
<input type="text" name="q" class="form-control"
placeholder="Search for auctions by name or description" value=""/>
<div class="input-group-btn">
<div class="btn-group" role="group">
<div class="dropdown dropdown-lg">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
aria-expanded="false"><span class="caret"></span></button>
<div class="dropdown-menu dropdown-menu-right" role="menu">
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="filter">Sort By</label>
<select class="form-control" name="sort" value="by_lowest_time">
<!-- By default by lowest time remaining-->
<option value="by_lowest_time" selected>Lowest Time Remaining</option>
<option value="by_highest_time">Highest Time Remaining</option>
<option value="by_lowest_price">Lowest Price</option>
<option value="by_highest_price">Highest Price</option>
</select>
</div>
<div class="form-group">
<label for="filter">Filter by Category</label>
<?php
try {
$catsql = 'SELECT * FROM ebay_clone.Category';
$catq = $db->query($catsql);
$catq->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
<select class="form-control" name="cat">
<option value=""></option>
<!-- http://stackoverflow.com/questions/3078192/how-to-set-html-value-attribute-with-spaces-using-php-->
<?php while ($r = $catq->fetch()): ?>
<option
value="<?php echo str_replace(' ', '_', strtolower(htmlspecialchars($r['item_category']))); ?>"> <?php echo htmlspecialchars($r['item_category']) ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="form-group">
<label for="filter">Filter by State</label>
<?php
try {
// error_reporting(E_ERROR | E_PARSE);
$statsql = 'SELECT * FROM ebay_clone.State';
$statq = $db->query($statsql);
$statq->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
<select class="form-control" name="state">
<!-- Blank option-->
<option value=""></option>
<?php while ($r = $statq->fetch()): ?>
<option value="<?php echo trimstr_replace(' ', '_', strtolower(htmlspecialchars($r['state']))); ?>"> <?php echo htmlspecialchars($r['state']) ?> </option>
<?php endwhile; ?>
</select>
</div>
<button type="button" value="search" class="btn btn-primary"><span
class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
</form>
</div>
</div>
<button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-search"
aria-hidden="true"></span></button>
</div>
</div>
</div>
</div>
</div>
</div>

Your while loop fills a $row variable
<?php while ($row = $statq->fetch()): ?>
But you are using $r to read it:
$r['state']

I have figured this out. It was to do with me changing my SQL statement and reassigning a new SQL statement to a new query. Also, I didn't need to refer to my database name, only the table within the database.
This works now:
<!--This is the search bar-->
<div class="container">
<div class="row">
<div class="col-sm-12">
<!-- http://stackoverflow.com/questions/8476602/appending-get-parameters-to-url-from-form-action-->
<!--If this is a form it messes up the styling-->
<div class="input-group" id="adv-search">
<form method='get' action='listings.php'>
<input type="text" name="q" class="form-control"
placeholder="Search for auctions by name or description" value=""/>
</form>
<div class="input-group-btn">
<div class="btn-group" role="group">
<div class="dropdown dropdown-lg">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
aria-expanded="false"><span class="caret"></span></button>
<div class="dropdown-menu dropdown-menu-right" role="menu">
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="filter">Sort By</label>
<select class="form-control" name="sort" value="by_lowest_time">
<!-- By default by lowest time remaining-->
<option value="by_lowest_time" selected>Lowest Time Remaining
</option>
<option value="by_highest_time">Highest Time Remaining</option>
<option value="by_lowest_price">Lowest Price</option>
<option value="by_highest_price">Highest Price</option>
</select>
</div>
<!-- Item Category -->
<div class="form-group">
<label for="filter">Filter by Category</label>
<select class="form-control" id="item-category" name="cat"
class="form-control">
<option value="" selected disabled hidden>Please Select a Category
</option>
<?php $sql = 'SELECT * FROM Category';
foreach ($db->query($sql) as $row) { ?>
<option
value="<?php echo $row['item_category']; ?>"><?php echo htmlspecialchars($row['item_category']); ?></option>
<?php } ?>
</select>
</div>
<!-- Item State -->
<div class="form-group">
<label for="filter">Filter by State</label>
<select class="form-control" id="item-state" name="state"
class="form-control">
<option value="" selected disabled hidden>Please Select a
Condition
</option>
<?php $sql = 'SELECT * FROM State';
foreach ($db->query($sql) as $row) { ?>
<option
value="<?php echo $row['state']; ?>"><?php echo htmlspecialchars($row['state']); ?></option>
<?php } ?>
</select>
</div>
<button type="submit" action="listings.php" method="get" value="search"
class="btn btn-primary"><span
class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>
</form>
</div>
</div>
<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"
aria-hidden="true"></span></button>
</div>
</div>
</div>
</div>
</div>
</div>

Related

Show data based on modal click

I made a while loop for the data to be displayed in a table format, I prepared <a> when clicked it opens a modal.
I want to have each modal have more info on that specific data when clicked.
Here is how I prepared things:
<?php
$mysqli = new mysqli('localhost', 'mushref', 'Almadina1!', 'security_db')
or die('Dramatic Error: ' . mysqli_error($mysqli));
$selectquery = "SELECT * FROM cases_reports";
$query = mysqli_query($mysqli, $selectquery);
$nums = mysqli_num_rows($query);
while($res = mysqli_fetch_array($query)) {
?>
<tr>
<td class="name mb-0 text-sm"> <?php echo $res['cccEmployee']?> </td>
<td> <?php echo $res['irNumber']?> </td>
<td> <?php echo $res['caseType']?> </td>
<td> <?php echo $res['startDateTime']?> </td>
<td> <?php echo $res['endDateTime']?> </td>
<td>
<div class="dropdown">
<a class="btn btn-sm btn-icon-only text-light" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-ellipsis-v"></i>
</a>
<div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">
<a class="dropdown-item" href="#">Print PDF</a>
<a class="dropdown-item" href="#">Export Excel</a>
<a class="dropdown-item" href="#">Export Access</a>
</div>
</div>
</td>
</tr>
<div class="modal fade" id="modal-default" tabindex="-1" role="dialog" aria-labelledby="modal-default" aria-hidden="true">
<div class="modal-dialog modal- modal-dialog-centered modal-" role="document">
<div class="modal-content">
<div class="modal-header">
<h6 class="modal-title" id="modal-title-default"> <?php echo $res['caseType']?> </h6>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php
echo $res['caseType'];
?>
</div>
</div>
<?php }?> <!-- End php While -->
Of course, the modal shows the first data available, which in this case "accident". How can I show each data's info when the modal URL is clicked?
just call your modal programmatically from js and load/replace its corresponing data before showing modal.
<?php echo $res['caseType']?>
and in your js file :
$(document).ready(function(){
$("#modalLuncher").click(function(){
$("#modal-title-default").html('read from #modalLuncher data-* tag');
// $("#other-replacements") ;
$("#modal-default").modal();
});
});
I wanted to show the modal so I can Ajax edit the table.
Here what I did:
Script:
$(document).ready(function() {
// Append values in input fields
$(document).on('click', 'a[data-role=update]', function(){
var id = $(this).data('id');
var cccEmployee = $("#"+id).children('td[data-target=cccEmployee]').text();
var irNumber = $("#"+id).children('td[data-target=irNumber]').text();
var caseType = $("#"+id).children('td[data-target=caseType]').text();
var startDateTime = $("#"+id).children('td[data-target=startDateTime]').text();
var endDateTime = $("#"+id).children('td[data-target=endDateTime]').text();
//Append the variables
$('#cccEmployee').val(cccEmployee);
$('#IR_number').val(irNumber);
$('#case_type').val(caseType);
$('#startDate').val(startDateTime);
$('#endDate').val(endDateTime);
$('#caseId').val(id);
$('#reportsModal').modal('toggle');
});
//Create event to get data from fields and update.
$('#save_report_changes').click(function() {
var id = $('#caseId').val();
var cccEmployee = $('#cccEmployee').val();
var irNumber= $('#IR_number').val();
var caseType = $('#case_type').val();
var startDateTime = $('#startDate').val();
var endDateTime = $('#endDate').val();
$.ajax({
url: '/edit-report',
method: 'post',
data: {
id: id,
cccEmployee: cccEmployee,
irNumber: irNumber,
caseType: caseType,
startDateTime: startDateTime,
endDateTime: endDateTime
},
success: function(response) {
$("#"+id).children('td[data-target=cccEmployee]').text(cccEmployee);
$("#"+id).children('td[data-target=irNumber]').text(irNumber);
$("#"+id).children('td[data-target=caseType]').text(caseType);
$("#"+id).children('td[data-target=startDateTime]').text(startDateTime);
$("#"+id).children('td[data-target=endDateTime]').text(endDateTime);
$('#reportsModal').modal('toggle');
},
});
});
});
Then created a while loop in php to display the data + linking the edit button to the ajax:
<!-- Start PHP While -->
<?php
$mysqli = new mysqli('localhost', 'mushref', 'Almadina1!', 'security_db')
or die('Dramatic Error: ' . mysqli_error($mysqli));
if( isset($_POST['id']) ) {
$id = $_POST['id'];
$ccc_employee = $_POST['cccEmployee'];
$IR_number = $_POST['irNumber'];
$case_type = $_POST['caseType'];
$caseLocation = $_POST['caseLocation'];
$startDate = $_POST['startDateTime'];
$endDate = $_POST['endDateTime'];
$case_description = $_POST['caseDesc'];
$action_taken = $_POST['actionsTaken'];
$details = $_POST['caseDetails'];
$notes = $_POST['caseNotes'];
$recommendation = $_POST['caseRecommendation'];
// Query to update data
$result = mysqli_query($mysqli, "UPDATE cases_reports SET
cccEmployee='$ccc_employee',
irNumber='$IR_number',
caseType='$case_type',
caseLocation='$caseLocation',
startDateTime='$startDate',
endDateTime='$endDate',
caseDesc='$case_description',
actionsTaken='$action_taken',
caseDetails='$details',
caseNotes='$notes',
caseRecommendation='$recommendation'
WHERE id='$id'");
if( $result ) {
return "Data updated successfully";
}
}
$selectquery = "SELECT * FROM cases_reports";
$table = mysqli_query($mysqli, $selectquery);
$nums = mysqli_num_rows($table);
while($res = mysqli_fetch_array($table)) {
?>
<tr id="<?php echo $res['id']?>">
<td data-target="cccEmployee"> <?php echo $res['cccEmployee']?> </td>
<td data-target="irNumber"> <?php echo $res['irNumber']?> </td>
<td data-target="caseType"> <?php echo $res['caseType']?> </td>
<td data-target="startDateTime"> <?php echo $res['startDateTime']?> </td>
<td data-target="endtDateTime"> <?php echo $res['endDateTime']?> </td>
<td class="table-actions">
<a href="#" data-role="update" data-id="<?php echo $res['id']; ?>">
<i class="fas fa-edit"></i>
</a>
<!-- <a href="#!" class="table-action table-action-delete" data-toggle="tooltip" data-original-title="Delete product">
<i class="fas fa-trash"></i>
</a> -->
</td>
<td>
<div class="dropdown">
<a class="btn btn-sm btn-icon-only text-light" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-ellipsis-v"></i>
</a>
<div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">
<a class="dropdown-item" href="#">Print PDF</a>
<a class="dropdown-item" href="#">Export Excel</a>
<a class="dropdown-item" href="#">Export Access</a>
</div>
</div>
</td>
</tr>
<div class="modal fade" id="reportsModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal- modal-dialog-centered modal-" role="document">
<div class="modal-content">
<div class="modal-header">
<h6 class="modal-title" id="modal-title-default"> Case Details </h6>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="case-data">
<div class="form-row">
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="cccEmployee">CCC Employee</label>
<input type="text" class="form-control" id="cccEmployee" name="cccEmployee">
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="IR_number">IR Number</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="IR_number">IR#</span>
</div>
<input type="number" class="form-control" name="IR_number" id="IR_number">
</div>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="case_type">Case Type</label>
<select class="form-control" name="case_type" id="case_type" data-toggle="select">
<option value="" disabled selected>Select Case Type</option>
<option value="Accident">Accident</option>
<option value="Theft">Theft</option>
<option value="Death">Death</option>
<option value="Harrasment">Harrasment</option>
<option value="badBehaviour">Bad Behaviour</option>
<option value="Drugs">Drugs</option>
<option value="Drunk">Drunk</option>
<option value="Fight">Fight</option>
<option value="Medical">Medical</option>
<option value="illegalEntry">Illegal Entry</option>
<option value="illegalWorker">Illegal Worker</option>
<option value="Fire">Fire</option>
<option value="Electric">Electric</option>
<option value="illegalHunting">Illegal Hunting</option>
<option value="oilLeak">Oil Leak</option>
<option value="petrolLeak">Petrol Leak</option>
<option value="Kidnapping">Kidnapping</option>
<option value="Blackmail">Blackmail</option>
<option value="Misunderstanding">Misunderstanding</option>
<option value="propertyDamage">Property Damage</option>
<option value="smoke_NoFire">Smoke, No Fire</option>
<option value="stuck_InElevator">Stuck in Elevator</option>
<option value="Scooter">Scooter</option>
<option value="Drifting">Drifting</option>
<option value="LostPerson">Lost Person</option>
<option value="Fraud_Forgery">Fraud/Forgery</option>
<option value="Other">Other</option>
</select>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="caseLocation">Location</label>
<select class="form-control" name="caseLocation" id="caseLocation" data-toggle="select">
<option value="" disabled selected>Select Location</option>
<option value="Baylasun">Baylasun</option>
<option value="Baylasun Hotel">Baylasun Hotel</option>
<option value="AL-Waha">AL-Waha</option>
<option value="Al-Morooj">Al-Morooj</option>
<option value="Royal Green">Royal Green</option>
<option value="Beach Towers">Beach Towers</option>
<option value="Emmar Building">Emmar Building</option>
<option value="Industrial Area (East)">Industrial Area (East)</option>
<option value="Industrial Area (West)">Industrial Area (West)</option>
<option value="Hejaz Gate">Hejaz Gate</option>
<option value="Gate 3">Gate 3</option>
<option value="Marina 1">Marina 1</option>
<option value="Marina 2">Marina 2</option>
<option value="Marina 3">Marina 3</option>
<option value="Tala Garden">Tala Garden</option>
<option value="AL-Shorooq">AL-Shorooq</option>
<option value="Yam Beach">Yam Beach</option>
<option value="Sales Center">Sales Center</option>
</select>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="startDate">Start Date & Time</label>
<input class="form-control" type="datetime-local" name="startDate" id="startDate">
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="endDate">End Date & Time</label>
<input class="form-control" type="datetime-local" name="endDate" id="endDate">
</div>
</div>
<div class="col-md-12 mb-3">
<div class="form-group">
<label class="form-control-label" for="case_description">Case Description</label>
<textarea class="form-control" name="case_description" id="case_description" rows="3"></textarea>
</div>
</div>
<div class="col-md-12 mb-3">
<div class="form-group">
<label class="form-control-label" for="action_taken">Actions Taken</label>
<textarea class="form-control" name="action_taken" id="action_taken"></textarea>
</div>
</div>
<div class="col-md-12 mb-3">
<div class="form-group">
<label class="form-control-label" for="details">Details</label>
<textarea class="form-control" name="details" id="details"></textarea>
</div>
</div>
<div class="col-md-12 mb-3">
<div class="form-group">
<label class="form-control-label" for="notes">Notes</label>
<textarea name="notes" id="notes" class="form-control"></textarea>
</div>
</div>
<div class="col-md-12 mb-3">
<div class="form-group">
<label class="form-control-label" for="recommendation">Recommendations</label>
<textarea name="recommendation" id="recommendation" class="form-control"></textarea>
</div>
</div>
<input type="hidden" id="caseId" class="form-control"/>
</div>
</div>
<div class="modal-footer">
<button type="button" id="save_report_changes" class="btn btn-primary">Save changes</button>
</div>
<?php } //End php While

Submit same forms with two buttons and send thank you mail only on the second submit button

I am having the issue on submitting the same form with two submit buttons. I am submitting the form for the first time with ajax submission and on the second time I want to submit the whole form but when I send the form the thank you mail hits two times . I want to send the thank you mail only on the second submit button and not on the first button. I have the button type ="button" ,
Here is my form code:-
<form name="basicform" id="basicform" method="post" action="<?php bloginfo("template_directory"); ?>/forms/callback/callback_process.php">
<input type="hidden" value="<?php echo $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" name="source" id="source" />
<input type="hidden" value="submited" name="basicform"/>
<input type="hidden" name="form_name" id="form_name" value="Header Contact Form" class="basicformname" >
<div id="sf1" class="frm">
<fieldset>
<!--<legend>Take a step for a Good health.</legend>-->
<div class="wor-alert"><p class="text-center fill-fields">Please Fill All This Fields*</p></div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="travcure-icon travcure-user"></i></span>
<input type="text" placeholder="Name" id="uname" name="uname" class="form-control" autocomplete="off">
<span class="nameerr" id="unameerr"><?php echo #$_SESSION['unameerr'] ? $_SESSION['unameerr']:''; unset($_SESSION['unameerr']); ?></span>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="travcure-icon travcure-envelope"></i></span>
<input type="text" placeholder="Email" id="uemail" name="uemail" class="form-control" autocomplete="on">
<span class="emailerr" id="uemailerr"><?php echo #$_SESSION['uemailerr'] ? $_SESSION['uemailerr']:''; unset($_SESSION['uemailer']); ?></span>
</div>
</div>
<div class="form-group">
<input type="text" placeholder="Phone Number" id="uphone" name="uphone" class="uphone form-control" autocomplete="off">
<span class="phoneerr" id="uphoneerr"><?php echo #$_SESSION['uphoneerr'] ? $_SESSION['uphoneerr']:''; unset($_SESSION['uphoneerr']); ?></span>
<input type="hidden" class="uphonefull" name="uphonefull">
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="travcure-icon travcure-user-md"></i></span>
<span class="plain-select">
<select name="utreatments" data-style="btn-primary" id="utreatments" class="form-control selectpicker">
<option value="">Select Treatment</option>
<option value="Alternative Treatment">Alternative Treatment</option>
<option value="2">Treatment Two</option>
<option value="3">Treatment Three</option>
<option value="4">Treatment Four</option>
<option value="5">Treatment Five</option>
</select>
</span>
</div>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn open1" type="button" id="head-frm-btn">Next <span class="fa fa-arrow-right"></span></button>
</div>
</fieldset>
</div>
<div id="sf2" class="frm" style="display: none;">
<fieldset>
<!--<legend>What treatment are you looking for?</legend>-->
<div class="form-group">
<span class="plain-select">
<select name="ucity" id="ucity" class="form-control">
<option value="">Select City</option>
<option value="Mumbai">Mumbai</option>
<option value="Pune">Pune</option>
<option value="Banglore">Banglore</option>
<option value="Nagpur">Nagpur</option>
<option value="Goa">Goa</option>
</select>
</span>
</div>
<div class="form-group">
<span class="plain-select">
<select name="uhospital" id="uhospital" class="form-control">
<option value="">Select Hospital</option>
<option value="Hospital One">Hospital One</option>
<option value="Hospital Two">Hospital Two</option>
<option value="Hospital Three">Hospital Three</option>
<option value="Hospital Four">Hospital Four</option>
<option value="Hospital Five">Hospital Five</option>
</select>
</span>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn back2" type="button"><span class="fa fa-arrow-left"></span> Back</button>
<button class="btn open2" type="button" id="head-frm-btn-two">Next <span class="fa fa-arrow-right"></span></button>
</div>
</fieldset>
</div>
<div id="sf3" class="frm" style="display: none;">
<fieldset>
<!--<legend>Almost done</legend>-->
<div class="waittext">
<p>Our customer care executive will get in contact with you soon. Please bear with us.</p>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn back3" type="button"><span class="fa fa-arrow-left"></span> Back</button>
<input class="btn open3" value="Submit" type="submit" name="head_form_submit">
</div>
</fieldset>
</div>
</form>
I submit this form using ajax for the first submit button and then on the second button I send the whole information but I want to send the thank you mail on the second button and not on the ajax submission of the button. I want to get th email of both the forms but i want to send the thank you mail only on the second submit button.
you can use two submit buttons
<button type="submit" name="submit">Submit form</button>
<button type="submit" name="submit_and_send_email">Submit and Send Email</button>
in php you can access the post like this
<?php
if(isset($_POST['submit']))
{
//submit only
}
if(isset($_POST['submit_and_send_email']))
{
//submit and send email
}
?>

getting only last id in php

I have total 7 Id in MySQL table. and I am fetching those Id's from database
code is :
<form class="form-inline" method="POST" action="book.php">
<div class="row-fluid home">
<div class="span3">
<?php
$query=mysql_query("select * from category");
$cat_count = 0;
while($result=mysql_fetch_array($query))
{
$category_id=$result['cat_id'];
$category_type=$result['cat_type'];
$categroy_desc=$result['cat_desc'];
$category_image=$result['cat_images'];
$category_price=$result['cat_price'];
$cat_count += 1;
//$_SESSION["session_category_id"] = $category_id;
?>
<div class="room_selector" data-price="<?php echo $category_price; ?>" data-adults="4" data-kids="2"
<?php if ($cat_count!=1) {echo 'style="display: none;"'; } ?> >
<h5>
<a href="index.php?id=<?php echo $category_id; ?>" class="pull-left">
<i class="icon-chevron-left"></i>
</a>
<?php echo $category_type; ?>
<a href="index.php?id=<?php echo $category_id; ?>" class="pull-right ">
<i class="icon-chevron-right"></i>
</a>
</h5>
<a class="iframe" href="rooms.php">
<img src="data:image/png;base64,<?php echo base64_encode($category_image); ?>" alt="No Photo" />
</a>
<p><?php echo $categroy_desc;?></p><br><Br><bR>
<!--<a class="btn btn-primary btn-large book-now" name="submit" type="submit" href="book.php?id=<?php echo $category_id; ?>">
Check Availability!</a>-->
<?php echo $x=$category_id;
?>
<input type="hidden" name="cat_id" value="<?php echo $category_id; ?>">
<?php $users[] = $result['cat_id'];?>
</div>
<?php
}
?>
<?php echo $category_id;
?>
</div>
<div class="span3 home_calendar">
<div class="form-horizontal">
<div class="control-group">
<label class="control-label pull-left" for="inputEmail">Arrive</label>
<div class="controls">
<input type="text" value="" name="from" class="span2 check-in-date" value="2015/05/22" />
</div>
</div>
</div>
<div class="datepicker_from"></div>
</div>
<div class="span3 home_calendar">
<div class="form-horizontal">
<div class="control-group">
<label class="control-label pull-left" for="inputEmail">Depart</label>
<div class="controls">
<input type="text" value="" name="to" class="span2 check-out-date" value="2015/05/27" />
</div>
</div>
</div>
<div class="datepicker_to"></div>
</div>
<div class="span3">
<div class="form-horizontal">
<div class="control-group">
<label class="control-label" for="inputEmail">Rooms</label>
<div class="controls">
<select class="span1 select_rooms" name="no_of_selected_room">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Adults per room</label>
<div class="controls">
<select class="span1 select_adults" name="no_of_adults">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Kids per room</label>
<div class="controls">
<select class="span1 select_kids" name="no_of_kids">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">extra person</label>
<div class="controls">
<select class="span1 select_kids">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</div>
</div>
</div>
<div id="total_price" class="price" name="price"></div>
<input type="submit" class="btn btn-primary btn-large book-now" name="submit" value="Check Availability!">
<!--<a class="btn btn-primary btn-large book-now" type="submit" href="book.php?id=">Check Availability!</a>-->
</div>
The $category_id is the main key to my project. once user will select the room then the $category_id will select based on that.
I am trying to send this id to other page using one form
//book.php
if(isset($_POST['submit']))
{
$n_cat_from = $_POST['from'];
$n_cat_to = $_POST['to'];
$n_no_of_selected_rooms = $_POST['no_of_selected_room'];
$n_no_of_adults = $_POST['no_of_adults'];
$n_no_of_kids = $_POST['no_of_kids'];
$current_Category_id = $_POST['cat_id'];
echo "id;;;;;;;;;;;;" . $current_Category_id;
}
?>
When I try to echo that id I am getting only last id in the database (after user selecting different id also )
where I am wrong ? why it is taking last Id ? help me
if you want to store multiple values in a hidden input with the same name, you have to add [] after the name, like this:
<input type="hidden" name="cat_id[]" value="<?php echo $category_id; ?>">
<?php $users[] = $result['cat_id'];?>
this way, you should have an array in book.php:
$_POST[cat_id] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 )

Bootstrap 3 tabs - calling MySQL Database in PHP for type ahead function - not working

I am using bootstrap tabs for displaying a search box in tabbed format. I am trying to call mySQL backend to get the list of items for the searc box. For some reason it is not working. Can someone help by looking at the code ? Look at the JS function in bottom. Why is it not calling ?
<div class="row ">
<div class="col-sm-6 col-sm-offset-3" >
<form class="form-inline" name="searchForm" onsubmit="return validateForm()" action="" method="get">
<div class="form-group">
<ul class="nav nav-tabs" role="tablist" id="myTab">
<li role="presentation" class="active">Company</li>
<li role="presentation">City or Zipcode</li>
<li role="presentation">Job Title</li>
</ul>
</div>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="company">
<div class="form-group">
<div class="input-group">
<input class="form-control " type="text" name="searchText" id="searchText" value="Enter Company Name" onfocus="if(this.value=='Enter Company Name')this.value='';" onblur="if(this.value=='')this.value='Enter Company Name';">
<div class="input-group-btn">
<select id="searchYear" name="searchYear" class="form-control">
<option value="14" selected="selected">2014</option>
<option value="13">2013</option>
<option value="12">2012</option>
<option value="11">2011</option>
<option value="10">2010</option>
<option value="09">2009</option>
</select>
</div>
</div>
<BR>
<button type="submit" value="Search" class="btn btn-block btn-danger">Search</button>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="city">
<div class="form-group">
<div class="input-group">
<input class="form-control" type="text" name="searchCity" id="searchCity" value="Enter US City or Zipcode" onfocus="if(this.value=='Enter US City or Zipcode')this.value='';" onblur="if(this.value=='')this.value='Enter US City or Zipcode';" >
<div class="input-group-btn">
<select id="searchYear" name="searchYear" class="form-control">
<option value="14" selected="selected">2014</option>
<option value="13">2013</option>
<option value="12">2012</option>
<option value="11">2011</option>
<option value="10">2010</option>
<option value="09">2009</option>
</select>
</div>
</div>
<BR>
<input type="hidden" name="action" value="search">
<button type="submit" value="Search" class="btn btn-block btn-danger">Search</button>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="jobtitle">
<div class="form-group">
<div class="input-group">
<input class="form-control" type="text" name="searchJobTitle" id="searchJobTitle" value="Enter Job Title" onfocus="if(this.value=='Enter Job Title')this.value='';" onblur="if(this.value=='')this.value='Enter Job Title';" >
<div class="input-group-btn">
<select id="searchYear" name="searchYear" class="form-control">
<option value="14" selected="selected">2014</option>
<option value="13">2013</option>
<option value="12">2012</option>
<option value="11">2011</option>
<option value="10">2010</option>
<option value="09">2009</option>
</select>
</div>
</div>
<BR>
<input type="hidden" name="action" value="search">
<button type="submit" value="Search" class="btn btn-block btn-danger">Search</button>
</div>
</div>
</div>
</form>
</div>
<div> <!-- search box row div offset -->
<!-- row 3: ads top most box, only shown in desktops and not shown in mobile-->
<!--div class="row hidden-md hidden-sm hidden-xs">
<img src='ad.jpg'/>
</div-->
<!-- New row set: Data display -->
<script type="text/javascript">
$(function() {
//autocomplete
$(".searchText").autocomplete({
source: "/h1b-visa-sponsors/search.php",
minLength: 3
});
});
$(function() {
//autocomplete
$(".searchJobTitle").autocomplete({
source: "/h1b-visa-sponsors/searchJobTitle.php",
minLength: 3
});
});
$(function () {
$('#myTab a:first').tab('show')
})
</script>

Making proper foreach HTML in PHP?

I have this php code
<div class="form-group box">
<div class="col-lg-7">
<?php
foreach ($results as $row) {
echo '<select class="form-control" required name="article[]">
<option value="'.$row->articles_id.'">'.$row->article_name.'</option>';
$sql="SELECT * FROM articles WHERE article_active = 1";
$query = $this->db->query($sql);
$articles = $query->result();
foreach ($articles as $row){
echo'
<option value="'.$row->articles_id.'">'.$row->article_name.'</option>';
}
echo'</select>';
}
?>
</div>
<div class="col-lg-3">
<?php foreach ($results as $row) {
echo '<input class="form-control kolicina" type="text" value="'.$row->order_qty.'" name="qty[]" '.$disabled.' required/>'; }
?>
</div>
<div class="col-lg-2">
<?php foreach ($results as $row) {
echo'<button type="button" class="btn btn-danger button-remove">Remove</button>';
}
?>
</div>
</div>
When display this i got this HTML code
<div class="col-lg-7">
<select class="form-control" required="" name="article[]">
<option value="10">Cipiripi</option>
<option value="8">Koka Kola</option>
<option value="10">Cipiripi</option>
</select>
<select class="form-control" required="" name="article[]">
<option value="8">Koka Kola</option>
<option value="8">Koka Kola</option>
<option value="10">Cipiripi</option>
</select>
</div>
<div class="col-lg-3">
<input class="form-control kolicina" value="44" name="qty[]" required="" type="text">
<input class="form-control kolicina" value="44" name="qty[]" required="" type="text">
</div>
<div class="col-lg-2">
<button type="button" class="btn btn-danger button-remove">Remove</button>
<button type="button" class="btn btn-danger button-remove">Remove</button>
</div>
I have tried a lot ways with loop, to get proper code, this is example hos html should look?
<div class="form-group box">
<div class="col-lg-7">
<select class="form-control" required="" name="article[]">
<option value="10">Cipiripi</option>
<option value="8">Koka Kola</option>
<option value="10">Cipiripi</option>
</select>
</div>
<div class="col-lg-3">
<input class="form-control kolicina" value="44" name="qty[]" required="" type="text">
</div>
<div class="col-lg-2">
<button type="button" class="btn btn-danger button-remove">Remove</button>
</div>
</div>
<div class="form-group box">
<div class="col-lg-7">
<select class="form-control" required="" name="article[]">
<option value="8">Koka Kola</option>
<option value="8">Koka Kola</option>
<option value="10">Cipiripi</option>
</select>
</div>
<div class="col-lg-3">
<input class="form-control kolicina" value="44" name="qty[]" required="" type="text">
</div>
<div class="col-lg-2">
<button type="button" class="btn btn-danger button-remove">Remove</button>
</div>
</div>
What i need that loops goes in different wrapper, not in in same col?
This is working fiddle how it has to look
http://jsfiddle.net/ckqth4a7/
And this is what i have now
http://jsfiddle.net/q0njhgfk/
No, just ditch that inner foreach loops inside just build it by the whole row.
Something like:
<?php foreach($results as $row): ?> <!-- loop this as a whole row -->
<div class="form-group box">
<div class="col-lg-7">
<select class="form-control" required="" name="article[]">
<?php
// this is the only loop for the options inside the select
$sql = "SELECT * FROM articles WHERE article_active = 1";
$query = $this->db->query($sql);
$articles = $query->result();
?>
<?php foreach($articles as $article): ?>
<option value="<?php echo $article->articles_id; ?>"><?php echo $article->article_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-lg-3">
<input class="form-control kolicina" value="<?php echo $row->order_qty; ?>" name="qty[]" required="" type="text">
</div>
<div class="col-lg-2">
<button type="button" class="btn btn-danger button-remove">Remove</button>
</div>
</div>
<?php endforeach; ?>
Or maybe, if the query is just standing there and will not be dynamic take it off the parent loop.
<?php
// take this outside, its always the same anyway, so that it queries only once
// this is the only loop for the options inside the select
$sql = "SELECT * FROM articles WHERE article_active = 1";
$query = $this->db->query($sql);
$articles = $query->result();
?>
<?php foreach($results as $row): ?> <!-- loop this as a whole row -->
<div class="form-group box">
<div class="col-lg-7">
<select class="form-control" required="" name="article[]">
<?php foreach($articles as $article): ?>
<option value="<?php echo $article->articles_id; ?>"><?php echo $article->article_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-lg-3">
<input class="form-control kolicina" value="<?php echo $row->order_qty; ?>" name="qty[]" required="" type="text">
</div>
<div class="col-lg-2">
<button type="button" class="btn btn-danger button-remove">Remove</button>
</div>
</div>
<?php endforeach; ?>

Categories