Liam
Liam

Reputation: 9855

Post data via jQuery AJAX

I'm trying to post data to my table using AJAX, I have the following form,

<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>    

And my AJAX code is as...

<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>

For some reason however my values aren't being passed. I'm new to AJAX and so I've been following a tutorial so excuse my naivety! How do I make this work?

Upvotes: 0

Views: 201

Answers (5)

Kristoffer Sall-Storgaard
Kristoffer Sall-Storgaard

Reputation: 10636

Ignoring the missing values, bill-name is not a valid identfier, if you really want to use bill-name you should wrap it in quotes and retrieve the value using jQuerys val():

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

Upvotes: 0

mikevoermans
mikevoermans

Reputation: 4007

For this and so many other PHP, jQuery ajax problems, I highly recommend serialize try this:

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

Upvotes: 1

hjpotter92
hjpotter92

Reputation: 80629

Change your form tag to this(or something similar)

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

and in your function, do this:

<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>

Upvotes: 0

Stan
Stan

Reputation: 26501

You are passing variables that are not even defined. You need to define all your post variables like you did with var bill = $("#bill").val(); and then pass them.

I would recommend you to use this jQuery plugin that does everything for you. I use it and it works fine.

Upvotes: 0

Noodles
Noodles

Reputation: 928

Your javascript variables haven't been defined yet. Your javascript function doesn't know about the form.

Try something like this:

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

Or you can set your variables beforehand

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

I'm not sure what this is doing though:

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

I don't see any id="bill"

Upvotes: 0

Related Questions