2016-04-18 7 views
0

Hallo möchte ich id Zeile aktualisieren, wenn ein Benutzer auf die Schaltfläche unten klicken Sie bitte und sorry für mein schlechtes Englisch-Update-Datenbank von Javascript onClick von Ajax PHP

index.php

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
</head> 

<body> 

<a href="javascript:void(0)" onClick="updateId('1')">Id 1</a> 
<a href="javascript:void(0)" onClick="updateId('2')">Id 2</a> 
<a href="javascript:void(0)" onClick="updateId('3')">Id 3</a> 

</body> 
</html> 

update.php helfen

<?php 
include('database_connection.php'); 

$update = "UPDATE id SET id = id + 1 WHERE id = updateId"; 

if (mysqli_query($connect, $update)) { 
    echo "Record updated successfully"; 
} else { 
    echo "Error updating record: " . mysqli_error($connect); 
} 
?> 
+0

Sie müssen bei Ajax aussehen Javascript (bei jquery der Einfachheit halber sehen) Anrufe und eine Möglichkeit, Daten in PHP zu lesen (siehe $ _GET [ ''] oder $ _POST [ '']) –

+0

Sie haben festgestellt, dass Sie JavaScript/Ajax verwenden müssen, aber ich habe kein JavaScript in der Frage. Beginnen Sie mit der Suche nach einem einleitenden Ajax-Tutorial. – Quentin

Antwort

2

In der index.php

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
</head> 
<body> 

<a href="javascript:void(0)" onClick="updateId('1')">Id 1</a> 
<a href="javascript:void(0)" onClick="updateId('2')">Id 2</a> 
<a href="javascript:void(0)" onClick="updateId('3')">Id 3</a> 

</body> 
</html> 

<script> 
function updateId(id) 
{ 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
     { 
      alert(xmlhttp.responseText); 
     } 
    }; 
    xmlhttp.open("GET", "update.php?id=" +id, true); 
    xmlhttp.send(); 
} 
</script> 

Und in Ihrem update.php

<?php 
if(isset($_GET['id']) && !empty($_GET['id'])) 
{ 
    $id = $_GET['id']; 
    include('database_connection.php'); 

    $update = "UPDATE id SET id = id + 1 WHERE id = '".$id."'"; 

    if (mysqli_query($connect, $update)) 
    { 
     echo "Record updated successfully"; 
    } 
    else 
    { 
     echo "Error updating record: " . mysqli_error($connect); 
    } 
    die; 
} 
?> 
+0

@ downVoter bitte Grund für down vote ??? alles falsch .. –

+0

Das ist, was ich gesucht habe !!! Danke^_^ – Alex

+0

@Doraemon Willkommen Liebe, froh, Ihnen zu helfen ... vote meine Antwort, wenn es für Sie hilfreich ist ... –