Un utente ha chiesto 👇
Sono completamente nuovo in AJAX. Ottengo un errore di 500 e non riesco a trovarlo.
ecco il mio modulo di input:
$out = '<strong>Straße</strong><br><input class="weit20" type="text" id="strasse" name="strasse" onkeyup="testStrasse()" value="' . $strasse . '"><br><br>';
Questa è la mia routine Javascript:
<script>
function testStrasse() {
var xhttp;
var str = document.getElementById("strasse");
var sstr = str.value;
if(sstr.length >= 3) {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("result_st").innerHTML = this.responseText;
}
}
xhttp.open("GET", "http://huppenbroich.de/simwae/sw_sstrasse.php?q="+sstr, true);
xhttp.send();
}
return;
}
</script>
Ed ecco il mio programma AJAX:
<?php
global $wpdb;
$sstr = $_GET['q'];
$sel = 'SELECT strasse FROM sw_strassen WHERE strasse LIKE ' . $sstr . ' LIMIT 5';
$ststr = $wpdb->get_results($sel);
if (!($ststr)) {
$anz = $wpdb->num_rows;
if ($anz == 1) {
foreach ($ststr as $strow) {
echo $ststr->strasse;
}
}
else {
echo '<select size=“1″>';
foreach ($ststr as $strow) {
echo '<option>';
echo $strow->strasse;
echo '</option>';
}
echo '</select>';
}
}
else {
echo '<strong style=color: red;>Straße existiert nicht!</<strong>';
};
?>
Ed ecco il messaggio di errore:
HTTP/1.1 500 Internal Server Error
Date: Sat, 16 Mar 2019 17:04:35 GMT
Server: Apache/2.4.37 (Unix)
X-Powered-By: PHP/7.2.15
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
Cosa potrebbe causare la 500? Qualsiasi aiuto richiesto!
Lanciatore di thread
(@gottfrieds)
1 anno, 11 mesi fa
Ho capito: è il collegamento wpdb. Non puoi usarlo in un ambiente AJAX!
(@diondesigns)
1 anno, 11 mesi fa
Se questo è il tuo gestore AJAX completo, non funzionerà perché $wpdb
Non esserci. È necessario aggiungere due righe all’inizio del gestore AJAX affinché funzioni. Avrebbero un aspetto simile a questo:
define('SHORTINIT', true);
require('wp-load.php');
Il percorso del file wp-load.php di WordPress deve essere nella seconda riga, che si trova nella radice della directory WP (dove si trova la directory / wp-include).
Altrimenti, controlla il tuo registro degli errori PHP, ti dirà qual è stato l’errore HTTP 500.
Con FYI, l’esecuzione di query utilizzando input non elaborati ti espone ad attacchi di SQL injection. Ci sono $wpdb->prepare()
una funzione che sfuggirà correttamente alla tua query.
Lanciatore di thread
(@gottfrieds)
1 anno, 11 mesi fa
Grazie mille, funziona!
Was this helpful?
0 / 0