1 Replies - 1506 Views - Last Post: 13 August 2012 - 11:20 PM

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

jQuery saving to database

Posted 13 August 2012 - 07:34 PM

I am not sure what to use in the data pair {value:value} in jQuery .post? nothing shows in error console, and nothing in jQuery return?

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript">

numBets=0;
function chooseBets(f)
{
	var c=f['ckbox[]'];	
	for(var i=0;i<c.length;i++){
	   if(c[i].checked) { numBets++; }   
	}
}
</script>


<form>
<table cellspacing=5 cellpadding=5 border=1>
<tr><td id="cell2_1" style="display:none;">
<input name="ckbox[]" type="checkbox" id="trn2_1" onclick="chooseBets(this.form)">2=1</input>
</td><td id="cell3_1" style="display:none;">
<input name="ckbox[]" type="checkbox" id="trn3_1" onclick="chooseBets(this.form)">3=1</input>
</td></tr>
<tr><td colspan=5><input type="submit" name="btnOrder" id="btnOrder" value="place order"/></td></tr>
</table>
</form>


<script type="text/javascript">
	$("#btnOrder").click( function(){

	      $.post('save2tdb.php', { total_bets: numBets }, function(data){
	          if(data == 1){
	               alert('Data was saved in db!');
	          } else {
		       alert('failed to execute save2db.php...');
		  }
	      });
	});
</script>

save2tdb.php:
<?php
if ($_POST && isset($_POST['value'])) {
   .... connecting to db and table..
     $num_bets=mysql_real_escape_string($_POST['value']);
     $sql = "INSERT INTO orders (order_number,order_date,total_bets) VALUES ('5',2012-08-13','$num_bets')";
   .....
}
?>

This post has been edited by g37752: 13 August 2012 - 07:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: jQuery saving to database

#2 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: jQuery saving to database

Posted 13 August 2012 - 11:20 PM

I changed the post to .ajax, but still same results - nothing?

$("#btnOrder").click( function(){
    $.ajax({
        url: "save2tdb.php",
             type: "post",
	     value:	numBets,
	success: function(response, textStatus, jqXHR){
            console.log("Hooray, it worked!");
        },
        error: function(jqXHR, textStatus, errorThrown){
            console.log(
                "The following error occured: "+
                textStatus, errorThrown
            );
        },
        complete: function(){
            console.log("complete..");
        }
    });
});

This post has been edited by g37752: 13 August 2012 - 11:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1