2016-07-26 7 views
0

Ich habe die folgende SQL-Abfrage, die über eine AJAX-Server-Anfrage bei der Formularübergabe durchgeführt wird. Es prüft eine Formular-POST-Variable $ product für alle zugehörigen Produkte in der Datenbank und begrenzt die Ergebnisse auf 2. Ich muss in der Lage sein, jedes Ergebnis der Abfrage mit PHP eindeutig zu identifizieren und sie in Variablen als $ product1 und $ product2 zu speichern und dann zurückzugeben Diese Werte in das Formular ohne Seitenaktualisierung. Warum wird den PHP SQL-Abfrageergebnissen im assoziativen Array nicht automatisch ein eindeutiger Schlüssel zugewiesen? (Siehe Bild unten)PHP-Array AJAX-Anfrage teilt Schlüssel

$product = $_POST['product']; 

$sql_query3 = "Select tbl_mixed_case.related_product 
FROM tbl_mixed_case JOIN tbl_product_info 
ON tbl_product_info.id = tbl_mixed_case.prod_code_id 
AND tbl_product_info.product + ' ' = '$product' LIMIT 2" ; 

$result3 = mysqli_query($dbconfig, $sql_query3); 

while ($products = mysqli_fetch_array($result3, MYSQL_ASSOC)) { 
print_r(array_values($products)); 
} 

enter image description here

+0

Welche Rolle spielt 'tbl_product_info.product + ''? – madalinivascu

+0

Sie müssen die eindeutige Kennung, die Sie in Ihrer Tabelle haben (wie eine eindeutige ID) auswählen. Also, etwas wie "SELECT ID, Feld". Dann ändere deine 'mysqli_fetch_array()' zu 'mysqli_fetch_assoc()', und 'print_r ($ products)', um deine Daten zu sehen – andrew

Antwort

1

Versuchen Sie Folgendes:

$product = $_POST['product']; 

$sql_query3 = "Select tbl_mixed_case.related_product 
FROM tbl_mixed_case JOIN tbl_product_info 
ON tbl_product_info.id = tbl_mixed_case.prod_code_id 
AND tbl_product_info.product + ' ' = '$product' LIMIT 2" ; 

$result3 = mysqli_query($dbconfig, $sql_query3); 
var $dispProd 
while ($products = mysqli_fetch_array($result3, MYSQL_ASSOC)) { 
$dispProd[] = $products; 
} 
header('Content-Type: application/json');//add the proper header 
echo json_encode(['products'=>$dispProd]);//convert to json 

in Ihrer Ajax-Erfolg Funktion, die Sie wie folgt vor:

success:function(data) { 
    $('#product1').val(data.products[0]);//add the first value to a input with the id of product1 
    $('#product2').val(data.products[1]); 
} 

ich die Ajax aufgenommen haben Anfrage und Formular Eingabe unten:

function submitdata() { 
var product = document.getElementById("product").value; 
// Returns successful data submission of associated products 
var dataString = 'product=' + product; 
     // AJAX code to submit form. 
       $.ajax({ 
       type: "POST", 
       url: "product.php", 
       data: 'application/json; charset=utf-8', 
       cache: false, 
       success: function(data) { 
       alert(data); 
       $('#product1').val(data.products[0]);//add the first value to a input with the id of product1 
       $('#product2').val(data.products[1]); 
       } 
       }); 

} 


<input type="text" value="" placeholder="" class="" id="product1" name="product1" tabindex="-1"/> 
<input type="text" value="" placeholder="" class="" id="product2" name="product2" tabindex="-1"/> 
+0

Danke madalin, das funktioniert. Wie kann ich das erste Ergebnis als Variable innerhalb einer Formulareingabe zurückgeben? – paynod

+0

siehe meine aktualisierte Antwort – madalinivascu

+0

Danke für das Update. Ich bekomme einen Parse-Fehler auf var $ dispProd. Ich habe es mit dem Delimeter versucht; – paynod