我有一个搜索栏,我在显示数据库中不同表格的所有记录(只有标题),现在还有标题我想传递id,如何使用ajax将id从一个页面传递到另一个页面? 请帮我解决这个问题...
Here is my code:
ajax_search.php:
<?php
require_once('../config.php');
if($_GET['data']=='setLanguage')
{
$lang = $_POST['lang'];
$_SESSION['CHOOSEN_LANGUAGE'] = $lang;
}
// FUNCTION FOR INSERT CITY BASED ON LAT LONG //
if($_GET['data']=='getCityByLatLong')
{
$browser_used=$_POST['browser_used'];
$city_id ='';
if($browser_used!='chrome')
{
$deal_lat=$_POST['lat'];
$deal_long=$_POST['lang'];
$geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$deal_lat.','.$deal_long.'&sensor=false');
$output= json_decode($geocode);
for($j=0;$j<count($output->results[0]->address_components);$j++)
{
$cn=array($output->results[0]->address_components[$j]->types[0]);
if(in_array("locality", $cn))
{
$city_name= $output->results[0]->address_components[$j]->long_name;
}
}
$sql1 = "SELECT CITY_ID FROM CITY WHERE CITY_TITLE='$city_name'";
$result1 = mysql_query($sql1);
$num = mysql_num_rows($result1);
if($num!='')
{
$city_row = mysql_fetch_assoc($result1);
$city_id = $city_row['CITY_ID'];
}
}
$city_id=100;
if($city_id !='')
{
$_SESSION['CHOOSEN_CITY_ID'] = $city_id;
}
echo $city_id;
}
if($_GET['data']=='changeCity')
{
$city_id = $_POST['city_id'];
echo $_SESSION['CHOOSEN_CITY_ID'] = $city_id;
}
//set SpecialityID in session
if($_GET['data']=='setSpeciality')
{
unset($_SESSION['SPECIALITY']);
$speciality_id = $_POST['speciality_id'];
echo $_SESSION['SPECIALITY']['ID'] = $speciality_id;
}
if($_GET['data']=='getAreaByCity')
{
$city_id = $_POST['city_id'];
$sql1="SELECT CITY_AREA_ID,AREA_TITLE FROM CITY_AREA WHERE CITY_ID ='".$city_id."' AND AREA_STATUS ='1'";
$response=mysql_query($sql1);
echo"<option value=''>Locality</option>";
while($row = mysql_fetch_assoc($response))
{
$id = $row['CITY_AREA_ID'];
$title = $row['AREA_TITLE'];
echo"<option value='$id'>$title</option>";
}
}
//CREATE BY RAJAN *** Search Query***
//-------------------------------------
//CREATE QUERY TO DB AND PUT RECEIVED DATA INTO ASSOCIATIVE ARRAY
if (isset($_REQUEST['query']))
{
$query = $_REQUEST['query'];
/*$querystr = "SELECT e.EMPLOYEE_ID,TRIM(IF('$LANG' = 'en',e.FIRST_NAME,e.FIRST_NAME_AR)) AS TITLE,
TRIM(IF('$LANG' = 'en',s.SPECIALIZATION_TITLE,s.SPECIALIZATION_AR_TITLE)) AS SPECIALIZATION_TITLE
FROM EMPLOYEE AS e
LEFT JOIN EMP_SPECIALIZATION AS sp ON sp.EMPLOYEE_ID=e.EMPLOYEE_ID
LEFT JOIN SPECIALIZATION AS s ON s.SPECIALIZATION_ID=sp.SPECIALIZATION_ID
LEFT JOIN EMPLOYEE_UNIT AS eu ON eu.EMPLOYEE_ID=e.EMPLOYEE_ID
LEFT JOIN UNIT AS u ON u.UNIT_ID=eu.UNIT_ID
LEFT JOIN UNIT_SERVICES AS us ON us.UNIT_ID=u.UNIT_ID
LEFT JOIN SERVICES AS sr ON sr.SERVICES_ID=us.SERVICES_ID
WHERE e.FIRST_NAME LIKE '%$query%' OR e.FIRST_NAME_AR LIKE '%$query%' OR e.LAST_NAME LIKE '%$query%' OR e.LAST_NAME_AR LIKE '%$query%'
OR s.SPECIALIZATION_TITLE LIKE '%$query%' OR s.SPECIALIZATION_AR_TITLE LIKE '%$query%'
OR u.UNIT_TITLE LIKE '%$query%' OR u.UNIT_AR_TITLE LIKE '%$query%'
OR sr.SERVICES_TITLE LIKE '%$query%' OR sr.SERVICES_AR_TITLE LIKE '%$query%'
GROUP BY EMPLOYEE_ID
ORDER BY TITLE";
$sql = mysql_query ($querystr);
$array = array();
while ($row = mysql_fetch_array($sql))
{
$array[] = array ('label' => $row['EMPLOYEE_ID'],'value' => $row['TITLE'],);
}*/
$array = array();
$querystr = "SELECT e.EMPLOYEE_ID,TRIM(CONCAT(IF('$LANG' = 'en',e.FIRST_NAME,e.FIRST_NAME_AR),IF('$LANG' = 'en',e.LAST_NAME,e.LAST_NAME_AR))) AS TITLE,
TRIM(IF('$LANG' = 'en',s.SPECIALIZATION_TITLE,s.SPECIALIZATION_AR_TITLE)) AS DEPARTMENT
FROM EMPLOYEE AS e
LEFT JOIN EMP_SPECIALIZATION AS sp ON sp.EMPLOYEE_ID=e.EMPLOYEE_ID
LEFT JOIN SPECIALIZATION AS s ON s.SPECIALIZATION_ID=sp.SPECIALIZATION_ID
WHERE e.FIRST_NAME LIKE '%$query%' OR e.FIRST_NAME_AR LIKE '%$query%' OR e.LAST_NAME LIKE '%$query%' OR e.LAST_NAME_AR LIKE '%$query%'
OR s.SPECIALIZATION_TITLE LIKE '%$query%' OR s.SPECIALIZATION_AR_TITLE LIKE '%$query%'
GROUP BY EMPLOYEE_ID
ORDER BY TITLE";
$sql = mysql_query ($querystr);
$cnt = mysql_num_rows($sql);
while ($row = mysql_fetch_array($sql))
{
$array[] = array ('value' => $row['TITLE']." --From Specilization");
}
$querystr = "SELECT e.EMPLOYEE_ID,TRIM(CONCAT(IF('$LANG' = 'en',e.FIRST_NAME,e.FIRST_NAME_AR),IF('$LANG' = 'en',e.LAST_NAME,e.LAST_NAME_AR))) AS TITLE,
TRIM(IF('$LANG' = 'en',u.UNIT_TITLE,u.UNIT_AR_TITLE)) AS DEPARTMENT
FROM EMPLOYEE AS e
LEFT JOIN EMPLOYEE_UNIT AS eu ON eu.EMPLOYEE_ID=e.EMPLOYEE_ID
LEFT JOIN UNIT AS u ON u.UNIT_ID=eu.UNIT_ID
WHERE e.FIRST_NAME LIKE '%$query%' OR e.FIRST_NAME_AR LIKE '%$query%' OR e.LAST_NAME LIKE '%$query%' OR e.LAST_NAME_AR LIKE '%$query%'
OR u.UNIT_TITLE LIKE '%$query%' OR u.UNIT_AR_TITLE LIKE '%$query%'
GROUP BY EMPLOYEE_ID
ORDER BY TITLE";
$sql = mysql_query ($querystr);
$cnt = mysql_num_rows($sql);
while ($row = mysql_fetch_array($sql))
{
$array[] = array ('value' => $row['TITLE']." --From Unit");
}
$querystr = "SELECT e.EMPLOYEE_ID,TRIM(CONCAT(IF('$LANG' = 'en',e.FIRST_NAME,e.FIRST_NAME_AR),IF('$LANG' = 'en',e.LAST_NAME,e.LAST_NAME_AR))) AS TITLE,
TRIM(IF('$LANG' = 'en',sr.SERVICES_TITLE,sr.SERVICES_AR_TITLE)) AS DEPARTMENT
FROM EMPLOYEE AS e
LEFT JOIN EMPLOYEE_UNIT AS eu ON eu.EMPLOYEE_ID=e.EMPLOYEE_ID
LEFT JOIN UNIT AS u ON u.UNIT_ID=eu.UNIT_ID
LEFT JOIN UNIT_SERVICES AS us ON us.UNIT_ID=u.UNIT_ID
LEFT JOIN SERVICES AS sr ON sr.SERVICES_ID=us.SERVICES_ID
WHERE e.FIRST_NAME LIKE '%$query%' OR e.FIRST_NAME_AR LIKE '%$query%' OR e.LAST_NAME LIKE '%$query%' OR e.LAST_NAME_AR LIKE '%$query%'
OR sr.SERVICES_TITLE LIKE '%$query%' OR sr.SERVICES_AR_TITLE LIKE '%$query%'
GROUP BY EMPLOYEE_ID
ORDER BY TITLE";
$sql = mysql_query ($querystr);
$cnt = mysql_num_rows($sql);
while ($row = mysql_fetch_array($sql))
{
$array[] = array ('value' => $row['TITLE']." --From Services ".$row['EMPLOYEE_ID']);
}
$querystr = "SELECT u.UNIT_ID,TRIM(IF('$LANG' = 'en',u.UNIT_TITLE,u.UNIT_AR_TITLE)) AS TITLE
FROM UNIT AS u
WHERE u.UNIT_TITLE LIKE '%$query%' OR u.UNIT_AR_TITLE LIKE '%$query%'
GROUP BY UNIT_ID
ORDER BY TITLE";
$sql = mysql_query ($querystr);
$cnt = mysql_num_rows($sql);
while ($row = mysql_fetch_array($sql))
{
$array[] = array ('value' => $row['TITLE']." --From Clinic");
}
//RETURN JSON ARRAY
echo json_encode ($array);
}
?>
search_section.php:
<div class="form-group check1">
<label class="sr-only" for="location">Search here for Doctor's Name, Specialities or Hospitals</label>
<input type="hidden" name="test_id" id="test_id" value="">
<input type="text" name="search_specialist" size="30" onchange="return getDetails();" class="form-control newcheck" id="location" placeholder="Search here for Doctor's Name, Specialities or Hospitals">
<button type="submit" class="btn btn-default btn-primary prim" onclick="return getDetails();"><i class="fa fa-search" aria-hidden="true"></i></button>
</div>
<script>
$('input.newcheck').typeahead({
name: 'search_specialist',
remote:"<?php echo CUR_URL ;?>new_template/ajax_code.php?query=%QUERY",
limit: 100
</script>
答案 0 :(得分:0)
Okej,生病只是尝试回答你的问题如何使用ajax传递值。 首先你需要表格:
<form id="form">
<input type="text" id="input" value="test">
</form>
表单中的所有输入字段值都将被传递。
的Ajax:
jQuery(function(){
$('#form').on('submit', function (e) { //on submit function
e.preventDefault();
$.ajax({
type: 'post', //method POST
url: 'yoururl.php', //URL of page where to pass values
data: $('#form').serialize(), //seriallize is passing all inputs values of form
success: function(){ //on success function
$("#input").attr("disabled", true); //example
$("#input").removeClass('btn-primary').addClass('btn-success');//example
},
});
}
});
注意(如果您想传递一些您不希望用户在页面上看到的值),您仍然可以使用显示块添加输入,例如:
<input type="text" name="akcija" value="<?php echo $row['akcija']; ?>" style="display:none;" />
在ajax页面上,您可以通过
获取值$akcija = $_POST['akcija']