Bonjour à tous, j'ai besoin, je cherche depuis plus d'une semaine.
Pour une rechercher asynchrone, j'ai trouvé script que j'ai adapté.
Pour l'affichage des données au chargement de la page, le script marche bien. Par contre lorsque j'inscris une valeur dans le champs input (recherche) une erreur survient.
erreur renvoyée
Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at XMLHttpRequest.ajax_request.onreadystatechange (admindashboard.js:872:24)
ajax_request.onreadystatechange @ admindashboard.js:872
XMLHttpRequest.send (async)
load_data @ admindashboard.js:866
(anonymous) @ admindashboard.js:856
html
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<div class="cadre-tableau">
            <h4 class="titre">Liste des centres crées</h4>
            <table id="table">
                <thead>
                    <tr id="first-tr">
                        <th class="center"></th>
                        <th class="center"></th>
                        <th hidden>ID</th>
                        <th>Code</th>
                        <th>Nommée</th> 
                        <th>Adresse</th>
                        <th>Telephone</th>
                        <th class="center">Compte acitve</th>
                        <th colspan="3" ><button id="openAdd" class ="btn green btn_centre">Nouveau</button></th>
                    </tr>
                </thead>
                <tbody id="post_data">
 
                </tbody>
            </table>
            <div id="pagination"></div>
            <!-- <span>Pages | </span><div id="pagination"></div> -->
 
        </div>
js/jquery
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
$(document).ready(function(){
	load_data();
});
const input = document.querySelector('#search');
input.addEventListener('keyup', function(){
	var query = input.value;
	//alert(query);
	load_data(query,1);
})
function load_data(query='' , page=1)
{
	var form_data = new FormData();
	form_data.append('query', query);
	form_data.append('page', page);
	var ajax_request = new XMLHttpRequest();
	ajax_request.open('POST', 'process_data.php',true);
	ajax_request.send(form_data);
	ajax_request.onreadystatechange = function()
	{
		if(ajax_request.readyState == 4 && ajax_request.status == 200)
		{   
 
			var response = JSON.parse(ajax_request.responseText);
			//console.log(ajax_request.responseText);
			var html = '';
			if(response.data.length > 0)
			{
				for(var count = 0; count < response.data.length; count++)
				{
 
					var compte ='';
					if(response.data[count].Active == 1){
						compte='oui';
					}else{
						compte='non';
					}
					html += '<tr>';
					html += '<td><input type="radio"  name="selectligne" ></td>';
					html += '<td>'+(count+1)+'</td>';
					html += '<td hidden>'+response.data[count].Id+'</td>';
					html += '<td>'+response.data[count].Code+'</td>';
					html += '<td>'+response.data[count].Nom+'</td>';
					html += '<td>'+response.data[count].Adresse+'</td>';
					html += '<td>'+response.data[count].Telephone+'</td>';
					html += '</tr>';
 
				}
			}
			else
			{
				html += '<tr><td colspan="3" class="text-center">No Data Found</td></tr>';
			}
			document.getElementById('post_data').innerHTML = html;
			document.getElementById('total_data').innerHTML = response.total_data;
			document.getElementById('pagination').innerHTML = response.pagination;
		}
	}
}
// fin load_data()
code php (process_data.php)
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
//echo '<script>alert('.$_POST["query"].')</script>';
// if($_POST['query'] != '')
// {
	//echo '<script>alert('.$_POST["query"].')</script>';
 
	//$connect = new PDO("mysql:host=localhost; dbname=testing", "root", "");
    require_once('../bd/connexion.php');
 
	$data = array();
 
	$limit = 8;
 
	$page = 1;
 
	if($_POST["page"] > 1)
	{
		$start = (($_POST["page"] - 1) * $limit);
 
		$page = $_POST["page"];
	}
	else
	{
		$start = 0;
	}
 
	if($_POST["query"] != '')
	{
		//echo '<script>alert('.$_POST["query"].')</script>';
 
		$query = '
		SELECT Id, Code, Nom, Adresse, Telephone, Active FROM centre
		';
 
		$query .= '
		WHERE Nom LIKE "%'.str_replace(' ', '%', $_POST['query']).'%" OR Code LIKE "%'.str_replace(' ', '%', $_POST['query']).'%" OR Telephone LIKE "%'.str_replace(' ', '%', $_POST['query']).'%"
		';
 
		$query .= 'ORDER BY Id ASC ';
		$filter_query = $query . 'LIMIT '.$start.', '.$limit.'';
 
        $statement = $bd->prepare($query);
		$statement->execute();
		$total_data = $statement->rowCount();
 
		$statement = $bd->prepare($filter_query);
		$statement->execute();
		$result = $statement->fetchAll();
 
		//$replace_array_1 = explode('%', $conditions);
		$replace_array_1 = trim($_POST["query"]);
		$replace_array_2 = '<span style="background-color:#'.rand(100000, 999999).'; color:#fff;">'.$replace_array_1.'</span>';
 
 
		foreach($result as $row)
		{
			$data[] = array(
				'Id' =>$row->Id,
				'Code' =>str_ireplace($replace_array_1, $replace_array_2, $row->Code),
				'Nom' =>str_ireplace($replace_array_1, $replace_array_2, $row->Nom),
				'Adresse' =>$row->Adresse,
				'Telephone' =>str_ireplace($replace_array_1, $replace_array_2, $row->Telephone),
				'Active' =>$row->Active
			);
		}
 
 
	}
	else
	{
        $filter_query =' LIMIT ' . $start . ', ' . $limit . '';
		$query = "SELECT * FROM centre ORDER BY Id ASC ";
		$filter_query = "SELECT * FROM centre ORDER BY Id ASC $filter_query";
 
		$statement = $bd->prepare($query);
		$statement->execute();
		$total_data = $statement->rowCount();
 
		$statement = $bd->prepare($filter_query);
		$statement->execute();
		$result = $statement->fetchAll();
 
		foreach($result as $row)
		{
			$data[] = array(
				'Id'	=>$row->Id,
				'Code'	=>$row->Code,
				'Nom'	=>$row->Nom,
				'Adresse' =>$row->Adresse,
				'Telephone' =>$row->Telephone,
				'Active' =>$row->Active
			);
		}
 
	}
 
 
	$pagination_html = '
	<div align="center">
  		<ul class="pagination">
	';
 
	$total_links = ceil($total_data/$limit);
	$previous_link = '';
	$next_link = '';
	$page_link = '';
	if($total_links > 7)
	{
		if($page < 8)
		{
			for($count = 1; $count <= 8; $count++)
			{
				$page_array[] = $count;
			}
			$page_array[] = '...';
			$page_array[] = $total_links;
		}
		else
		{
			$end_limit = $total_links - 8;
			if($page > $end_limit)
			{
				$page_array[] = 1;
				$page_array[] = '...';
 
				for($count = $end_limit; $count <= $total_links; $count++)
				{
					$page_array[] = $count;
				}
			}
			else
			{
				$page_array[] = 1;
				$page_array[] = '...';
				for($count = $page - 1; $count <= $page + 1; $count++)
				{
					$page_array[] = $count;
				}
				$page_array[] = '...';
				$page_array[] = $total_links;
			}
		}
	}
	else
	{
		for($count = 1; $count <= $total_links; $count++)
		{
			$page_array[] = $count;
		}
	}
 
	for($count = 0; $count < count($page_array); $count++)
	{
		if($page == $page_array[$count])
		{
			$page_link .= '
			<li class="page-item active">
	      		<a class="page-link" href="#">'.$page_array[$count].' <span class="sr-only">(current)</span></a>
	    	</li>
			';
			$previous_id = $page_array[$count] - 1;
 
			if($previous_id > 0)
			{
				$previous_link = '<li class="page-item"><a class="page-link" href="javascript:load_data(`'.$_POST["query"].'`, '.$previous_id.')">Previous</a></li>';
			}
			else
			{
				$previous_link = '
				<li class="page-item disabled">
			        <a class="page-link" href="#">Previous</a>
			    </li>
				';
			}
 
			$next_id = $page_array[$count] + 1;
 
			//if($next_id >= $total_links)
			if($next_id > $total_links)
			{
				$next_link = '
				<li class="page-item disabled">
	        		<a class="page-link" href="#">Next</a>
	      		</li>
				';
			}
			else
			{
				$next_link = '
				<li class="page-item"><a class="page-link" href="javascript:load_data(`'.$_POST["query"].'`, '.$next_id.')">Next</a></li>
				';
			}
 
		}
		else
		{
			if($page_array[$count] == '...')
			{
				$page_link .= '
				<li class="page-item disabled">
	          		<a class="page-link" href="#">...</a>
	      		</li>
				';
			}
			else
			{
				$page_link .= '
				<li class="page-item">
					<a class="page-link" href="javascript:load_data(`'.$_POST["query"].'`, '.$page_array[$count].')">'.$page_array[$count].'</a>
				</li>
				';
			}
		}
	}
 
	$pagination_html .= $previous_link . $page_link . $next_link;
 
 
	$pagination_html .= '
		</ul>
	</div>
	';
 
	$output = array(
		'data'				=>	$data,
		'pagination'		=>	$pagination_html,
		'total_data'		=>	$total_data
	);
 
	echo json_encode($output);
 
//}