Wie kann ich einige Ajax-Daten an die Controller-Funktion senden und zurückerhalten? Denn ich möchte eine Ganzzahl an die Funktion senden und eine andere Ganzzahl erhalten (Gesamtstimmen für das Element, dessen ID gesendet wird), und bei Erfolg möchte ich diese Stimmenzahl wiederholen. Ich weiß nicht, wie ich die “ID” an die Controller-Funktion senden kann. Siehe bitte meinen Code:
//post this integet
the_id = $(this).attr('id');
$.ajax({
type: "POST",
data: the_id,
url: "http://localhost/test/index.php/data/count_votes",
success: function(){
//the controller function count_votes returns an integer.
//echo that with the fade in here.
}
});

Rafay
$.ajax({
type: "POST",
data: {data:the_id},
url: "http://localhost/test/index.php/data/count_votes",
success: function(data){
//data will contain the vote count echoed by the controller i.e.
"yourVoteCount"
//then append the result where ever you want like
$("span#votes_number").html(data); //data will be containing the vote count which you have echoed from the controller
}
});
im Steuergerät
$data = $_POST['data']; //$data will contain the_id
//do some processing
echo "yourVoteCount";
Klärung
ich glaube du verwirrst dich
{data:the_id}
mit
success:function(data){
beide data
zu Ihrer eigenen Klarheit anders sind, können Sie es als ändern
success:function(vote_count){
$(span#someId).html(vote_count);
Versuchen Sie es für das JS
data: {id: the_id}
...
success: function(data) {
alert('the server returned ' + data;
}
und
$the_id = intval($_POST['id']);
in PHP

Serienwurm
Wie sieht also count_votes aus? Ist es ein Drehbuch? Alles, was Sie von einem Ajax-Aufruf zurückbekommen möchten, kann mit einem einfachen Echo abgerufen werden (natürlich könnten Sie JSON oder XML verwenden, aber für dieses einfache Beispiel müssten Sie nur etwas in count_votes.php ausgeben wie:
$id = $_POST['id'];
function getVotes($id){
// call your database here
$query = ("SELECT votes FROM poll WHERE ID = $id");
$result = @mysql_query($query);
$row = mysql_fetch_row($result);
return $row->votes;
}
$votes = getVotes($id);
echo $votes;
Dies ist nur Pseudocode, sollte Ihnen aber eine Idee vermitteln. Was auch immer Sie von count_votes zurückgeben, wird in Ihrem Ajax-Aufruf an “data” zurückgegeben.
10149500cookie-checkAjax-Daten an PHP senden und Daten zurückgebenyes