2012-04-12 19 views
0

Ich versuche, Daten auf meinen Tisch schreiben mit AJAX, ich habe das folgende Formular,Post-Daten über jQuery AJAX

<form> 
    Total <input type="text" id="total" name="total" /><br /> 
    Bill name<input type="text" id="bill-name" name="bill-name" /><br /> 
    bill descriptiion <input type="text" id="bill-description" name="bill-description" /><br /> 
    bill colour<input type="text" id="bill-colour" name="bill-colour" /> 
    <input type="button" value="submit" onClick="insertBill();" /> 
</form>  

Und mein AJAX-Code ist als ...

<script type="text/javascript"> 
function insertBill() 
{ 
    var bill = $("#bill").val(); 
    $.post('insert_bill.php', {total: total, bill-name: bill-name, bill-description: bill-description, bill-colour: bill-colour}, function(data) 
    { 
     $("#bills").html(data); 
    }); 
} 
</script> 

Aus irgendeinem Grund werden meine Werte jedoch nicht weitergegeben. Ich bin neu in AJAX und folge daher einem Tutorial, also entschuldige meine Naivität! Wie mache ich das?

+1

warum die PHP-Tag? – hjpotter92

+0

In Ihrer Funktion rufen Sie auch ein Formular mit der ID 'bill' an. Bearbeiten Sie Ihr Formular-Tag und fügen Sie diese ID – hjpotter92

Antwort

1

Für diese und so viele andere PHP, Probleme jQuery Ajax, I highly recommend serialize dies versuchen:

<div id="bills"></div> 
<script type="text/javascript"> 
function insertBill() 
{ 
    $.post('insert_bill.php', $('form').serialize(), 
     function(data) { 
     $("#bills").html(data); 
    }); 
} 
+0

Wie ein Charme! Danke, noch nie von Serialisierung vor – Liam

+0

gehört. Glücklich zu helfen - so ist das die akzeptierte Antwort? :) – mikevoermans

+0

Sicher ist @mikevoermans! – Liam

0

Ihre Javascript-Variablen wurden noch nicht definiert. Ihre JavaScript-Funktion kennt das Formular nicht.

so etwas wie dieses Versuchen:

$.post('insert_bill.php', {total: $('#total').val(), bill-name: $('#bill-name').val()... 

Oder Sie können Ihre Variablen vorher

<script type="text/javascript"> 
function insertBill() 
{ 
    var total = $('#total').val(); 

Ich bin nicht sicher festgelegt, was diese allerdings tut:

var bill = $("#bill").val(); 

I don sehe keine ID = "Rechnung"

0

Sie übergeben Variablen, die nicht einmal definiert sind. Sie müssen alle Post-Variablen definieren, wie Sie es mit getan haben, und sie dann übergeben.

Ich würde Ihnen empfehlen, this jQuery Plugin zu verwenden, das alles für Sie tut. Ich benutze es und es funktioniert gut.

0

Ihr form Tag Um dies ändern (oder etwas ähnliches)

<form id="bill" onsubmit="return insertBill();"> 

und in Ihrer Funktion, dies zu tun:

<script type="text/javascript"> 
    function insertBill() { 
    var bill = $("#bill").val(); 
    $.post('insert_bill.php', {total: total, bill-name: bill-name, bill-description: bill-description, bill-colour: bill-colour}, function(data) 
     { 
     $("#bills").html(data); 
     }); 
    return true;  // So that form may proceed. 
    } 
</script> 
0

die mis Ignorieren singen Werte, bill-name ist keine gültige identfier, wenn Sie wirklich verwenden möchten bill-name sollten Sie es in Anführungszeichen wickeln und den Wert abrufen jQuerys val() mit:

$.post('insert_bill.php',{ 'bill-name' : $('#bill-name').val() } ....