我遇到了问题,我不知道如何解决这个问题,或者问题如何解决。在我的HTML / PHP页面中,有一个搜索表单。当用户搜索某些内容时,我不是将控制权传递给另一个页面,而是使用jquery将另一个php页面加载到div容器中以执行查询并显示结果。我遇到的问题是对结果进行排序。我通常处理排序的方法只是构建包含新排序参数的URL字符串,并执行/加载该URL,即同一页面,只是不同的排序参数。但是,通过将查询嵌入到另一个页面的页面,我需要在包含页面的div中重新加载该页面。但由于我在嵌入式页面中,我无法访问该页面的元素。
<!-- ======================== Container Page =============================== -->
<div class="row search-form-container" id="search-scripts-container">
<div class="col-xs-3">
<div class="input-group input-append date" data-provide="datepicker">
<span class="input-group-addon add-on">FROM</span>
<input type="text" class="form-control" id="script_from_date"/>
<span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
</div>
<div class="col-xs-3">
<div class="input-group input-append date" data-provide="datepicker">
<span class="input-group-addon add-on">TO</span>
<input type="text" class="form-control" id="script_to_date"/>
<span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
</div>
<!-- search all input box -->
<div class="col-xs-4">
<div class="input-group input-append">
<span class="input-group-addon addon">SEARCH</span>
<input type="text" class="form-control" id="script_search_all" />
</div>
</div>
<div class="col-xs-1">
<button type="button" class="btn btn-primary" id="sbmt_scripts_search">Query</button>
</div>
</div>
//===================== JQuery Function ===============================
$('#sbmt_scripts_search').click(function() {
var from_date = $('#script_from_date').val();
var to_date = $('#script_to_date').val();
var search_str = $('#script_search_all').val().replace(/\ /g, '_'); // convert spaces to underscores to pass in url
$('#results-container').load('/search/results.php?from_date='+from_date+'&to_date='+to_date+'&search_str='+search_str+'&col=0&order=DESC');
});
//============================ PHP Results Page ============================
Create and execute query....
// selection list to select field to sort by
<div class="row" style="margin:40px 0 5px 0">
<div class="col-sm-2"></div>
<div class="col-sm-8">
<div class="col-sm-9" align="right">
<label for="col_sort">Sort By:</label>
</div>
<div class="col-sm-3" style="padding:0;">
<select class="sort_select" id="col_sort" style="width:100%; padding:0; margin:0;">
<option value="/search/prescription_results.php?from_date=<?=$from_date?>&to_date=<?=$to_date?>&search_str=<?=$search_string?>&col=0&order=<?=$sort_args[0]['order']?>">
Data
</option>
<option <?=($col == 1) ? "selected='selected'" : ""?> value="/search/results.php?from_date=<?=$from_date?>&to_date=<?=$to_date?>&search_str=<?=$search_string?>&col=1&order=<?=$sort_args[1]['order']?>">
Data
</option>
<option <?=($col == 2) ? "selected='selected'" : ""?> value="/search/results.php?from_date=<?=$from_date?>&to_date=<?=$to_date?>&search_str=<?=$search_string?>&col=2&order=<?=$sort_args[2]['order']?>">
Data
</option>
<option <?=($col == 3) ? "selected='selected'" : ""?> value="/search/results.php?from_date=<?=$from_date?>&to_date=<?=$to_date?>&search_str=<?=$search_string?>&col=3&order=<?=$sort_args[3]['order']?>">
Data
</option>
<option <?=($col == 4) ? "selected='selected'" : ""?> value="/search/results.php?from_date=<?=$from_date?>&to_date=<?=$to_date?>&search_str=<?=$search_string?>&col=4&order=<?=$sort_args[4]['order']?>">
Data
</option>
<option <?=($col == 5) ? "selected='selected'" : ""?> value="/search/results.php?from_date=<?=$from_date?>&to_date=<?=$to_date?>&search_str=<?=$search_string?>&col=5&order=<?=$sort_args[5]['order']?>">
Data
</option>
<option <?=($col == 6) ? "selected='selected'" : ""?> value="/search/results.php?from_date=<?=$from_date?>&to_date=<?=$to_date?>&search_str=<?=$search_string?>&col=6&order=<?=$sort_args[6]['order']?>">
Data
</option>
</select>
</div>
</div>
<div class="col-sm-2"></div>
</div>
// display query results
<div class="row" style="margin-top:10px">
<div class="col-sm-2"></div>
<div class="col-sm-8">
<div class="panel panel-default">
<div class="panel-heading">
<a href="/view.php?rx=<?=base64_encode($data['number'])?>">data#<?=$data?></a>
</div>
<div class="panel-body">
<div class="col-sm-6">
<p>
<strong>Data:</strong> <?=$result1?><br/>
<strong>Data:</strong> <?=$result2?><br/>
<strong>Data:</strong> <?=$result3?><br/>
</p>
</div>
<div class="col-sm-6">
<p>
<strong>Data:</strong> <?=$result4?><br/>
<strong>Data:</strong> <?=$result5?><br/>
<strong>Data:</strong> <?=$result6?>
</p>
</div>
</div>
</div>
</div>
<div class="col-sm-2"></div>
</div>
以下是进行操作的基础知识。当有人选择要排序的标题时,我想使用新的排序参数重新加载php页面,而无需重新加载外部或容器页面。
//========================= PHP Query ================================
SELECT presc.*, doc.first_name as doctor_fname, doc.last_name as doctor_lname, prov.name as provider_name
FROM `PRESCRIPTIONS` presc
JOIN `DOCTORS` doc
ON presc.doctor_id = doc.id
LEFT JOIN `PROVIDERS` prov
ON presc.provider_id = prov.id
WHERE presc.patient_fname LIKE '%$search_string%'
AND".$date_concat."
OR presc.patient_lname LIKE '%$search_string%'
AND".$date_concat."
OR CONCAT_WS(' ', presc.patient_fname, presc.patient_lname) LIKE '%$search_string%'
AND".$date_concat."
OR presc.date_of_birth LIKE '%$search_string%'
AND".$date_concat."
OR presc.script_date LIKE '%$search_string%'
AND".$date_concat."
OR presc.drug LIKE '%$search_string%'
AND".$date_concat."
OR presc.rx_number LIKE '%$search_string%'
AND".$date_concat."
OR presc.status LIKE '%$search_string%'
AND".$date_concat."
OR presc.ticket_date LIKE '%$search_string%'
AND".$date_concat."
OR presc.adherence_pct LIKE '%$search_string%'
AND".$date_concat."
OR presc.nursing_home LIKE '%$search_string%'
AND".$date_concat."
OR doc.first_name LIKE '%$search_string%'
AND".$date_concat."
OR doc.last_name LIKE '%$search_string%'
AND".$date_concat."
OR prov.name LIKE '%$search_string%'
AND".$date_concat;"
对此问题的任何帮助将不胜感激。
答案 0 :(得分:1)
您应该使用AJAX来执行此操作。以下是非常粗略的用法。你会注意到我希望PHP中的函数getDataFromDataBaseOrWhatever
能够正确地返回HTML格式,以便在客户端上显示。这是一种方法,但是你可以考虑简单地返回一个具有你想要的值的对象,然后使用JS中的success
函数来使用该对象然后构建HTML本身。这通常是更好的方法,因为PHP应该只是处理数据,而客户端应该控制视图(HTML)。
但是我已经在下面的第一个方向完成了它,所以它对你来说并不是那么多。如果你想要另一种方式的例子,请告诉我。
// JS
$.ajax({
url: 'script.php',
type:'POST',
dataType: "json",
data: {
action: 'search',
startDate: startDate,
endDate: endDate
// other filters
},
success: function(res){
if(res.success) {
$('#results-container').html(res.html);
} else {
// error with script or server
}
},
error: function(err){
error with request/server
},
});
// PHP
$data = $_POST['data'];
if($data['action'] != 'search') {
die('wrong action');
}
// make sure params are what you might expect for security
// setup our query function
// using HTML output by server
function getDataFromDataBaseOrWhatever() {
return $html;
}
// create return object
$return = new stdClass();
// use data from POST
$startDate = $data['startDate'];
// get return values/HTML
$return->html = getDataFromDataBaseOrWhatever();
// if everything is okay, set the res property to true
$return->success = true;
die(json_encode($return));