0

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?

1
  • Also, in your function you are calling a form with id bill. Edit your form tag, and add this id Commented Apr 12, 2012 at 20:52

5 Answers 5

1

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); 
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

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"

Comments

0

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.

Comments

0

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>

Comments

0

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() } ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.