Author Topic: Jquery posting to php  (Read 382 times)

Sabmin

  • Senior Member
  • ****
  • Posts: 154
  • Karma: 4
    • View Profile
Jquery posting to php
« on: June 01, 2012, 08:23:39 PM »
Dragging and dropping and on the drop event I'm supposed to be posting data to my php file, which then inserts it into a database.  However it doesn't work the way I expected from the documentation.  And the whole cross language debugging has me wanting to just shut the laptop and go play with my dog... while I'm sure the dog would appreciate that, He's the only one who would be benefiting.  So I would really appreciate some sort of direction...

The full jquery is here, specific post function at the bottom:
PHP Code: [Select]
  $(window).load(function(){
    $(
".make-draggable, .draggable, .drag").draggable({
        
helper"clone",
        
snap".draggable",
        
snapMode"inner"
    
});
    $(
".draggable").droppable({
        
drop: function(eventui) {
            var 
elemText ui.draggable.text();
            $(
this).html(elemText);
            var 
outB ui.draggable.attr('id').split("-");
            var 
pl1;
            var 
pl2;
            if (
outB[0] == "go") {
                var 
num outB.length;
                var 
loser;
                var 
loserval;
                var 
losloc;
                var 
losid outB[1];
                var 
numchars outB[1].length;
                if (
num === 2) {
                    var 
1;
                    
loser = (losid.charAt(0) + "-");
                    
pl1 elemText;
                    for (
1numcharsi++) {
                        
loser = (loser+losid.charAt(i));
                    }
                    
loserval = $("#go-" loser);
                    
losloc = ("#"+losid);
                    $(
losloc).html(loserval.text());
                    
pl2 loserval.text();
                } else if (
num === 3) {
                    
loser = (outB[1] + outB[2]);
                    
loserval = $("#go-" loser);
                    
losloc = ("#"+loser);
                    $(
losloc).html(loserval.text());
                    
pl1 loserval.text();
                    
pl2 elemText;
                }
            } else {
              var 
getround = $(this).attr('id');
              if (
getround == 'round-final') {
                
pl1 = $('#winnerb').text();
                
pl2 = $('#loserb').text();
              } else {
                var 
inti getround.substring(1);
                if (
inti%== 0) {
                  
pl1 = $('l'+(inti+1));
                  
pl2 elemText;
                } else {
                 
pl1 elemText;
                 
pl2 = $('l'+(inti+1));
                }
              }
            }
	
	
	
var 
matrou = $(this).parent().attr("id").split("-");
	
	
	
$.
urlParam = function(name){
	
	
	
	
var 
results = new RegExp('[\\?&]' name '=([^&#]*)').exec(window.location.href);
	
	
	
return 
results[1] || 0;
	
	
	
}
            $.
post("sub_match.php", {winelemTextp1pl1p2pl2matchmatrou[4], roundmatrou[2], wloc: $(this).idlloclosloctid: $.urlParam('tid')});
        }
    });
  });


with sub_match.php to follow:

PHP Code: [Select]
<?php
session_start
();
require(
"goodies.php");
	
$tid clean($_REQUEST['tid']);
	
$round clean($_REQUEST['round']);
	
$match clean($_REQUEST['match']);
	
$player1 clean($_REQUEST['p1']);
	
$player2 clean($_REQUEST['p2']);
	
$winner clean($_REQUEST['win']);
	
$wdest clean($_REQUEST['wloc']);
	
$ldest clean($_REQUEST['lloc']);
	
$query = ("INSERT INTO matches (id, tid, season, round, match, player1, player2, winner, wdest, ldest) VALUES ('', '" $tid "', '" $season "', '" $round "', '" $match "', '" $player1 "', '" $player2 "', '" $winner "', '" $wdest "', '" $ldest "', )");
	
mysql_query($query);
?>


I really never have liked anything about javascript except for this first time in my life where I feel for this project I just have to have this.  So any help/direction greatly appreciated.

Sabmin

  • Senior Member
  • ****
  • Posts: 154
  • Karma: 4
    • View Profile
Re: Jquery posting to php
« Reply #1 on: June 02, 2012, 06:33:47 PM »
Okay so I changed to the following code, now I'm getting information sent but not as I expected... my alart comes back with a little more than it should:
"INSERT INTO matches (id, tid, season, round, match, player1, player2, winner, wdest, ldest)VALUES('', '4', '', '1', '1','\n         John doe','\n         BYE',\n         John doe', 'undefined','#l4', )
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->"

Here's the new java code, only change to the php file is I removed the , that didn' belong at the end of the query, and added an echo $query instead of running it.
PHP Code: [Select]
  $(window).load(function(){
    $(
".make-draggable, .draggable, .drag").draggable({
        
helper"clone",
        
snap".draggable",
        
snapMode"inner"
    
});
    $(
".draggable").droppable({
        
drop: function(eventui) {
            var 
elemText ui.draggable.text();
            $(
this).html(elemText);
            var 
outB ui.draggable.attr('id').split("-");
            var 
pl1;
            var 
pl2;
            if (
outB[0] == "go") {
                var 
num outB.length;
                var 
loser;
                var 
loserval;
                var 
losloc;
                var 
losid outB[1];
                var 
numchars outB[1].length;
                if (
num === 2) {
                    var 
1;
                    
loser = (losid.charAt(0) + "-");
                    
pl1 elemText;
                    for (
1numcharsi++) {
                        
loser = (loser+losid.charAt(i));
                    }
                    
loserval = $("#go-" loser);
                    
losloc = ("#"+losid);
                    $(
losloc).html(loserval.text());
                    
pl2 loserval.text();
                } else if (
num === 3) {
                    
loser = (outB[1] + outB[2]);
                    
loserval = $("#go-" loser);
                    
losloc = ("#"+loser);
                    $(
losloc).html(loserval.text());
                    
pl1 loserval.text();
                    
pl2 elemText;
                }
            } else {
              var 
getround = $(this).attr('id');
              if (
getround == 'round-final') {
                
pl1 = $('#winnerb').text();
                
pl2 = $('#loserb').text();
              } else {
                var 
inti getround.substring(1);
                if (
inti%== 0) {
                  
pl1 = $('l'+(inti+1));
                  
pl2 elemText;
                } else {
                 
pl1 elemText;
                 
pl2 = $('l'+(inti+1));
                }
              }
            }
	
	
	
var 
matrou ui.draggable.parent().attr("id").split("-");
	
	
	
$.
urlParam = function(name){
	
	
	
	
var 
results = new RegExp('[\\?&]' name '=([^&#38;#]*)').exec(window.location.href);
	
	
	
return 
results[1] || 0;
	
	
	
}
	
	
	
xmlhttp=new XMLHttpRequest();
	
	
	
xmlhttp.onreadystatechange=function() {
	
	
	
	
if (
xmlhttp.readyState==&& xmlhttp.status==200) {
	
	
	
	
	
alert(xmlhttp.responseText);
	
	
	
	
}
	
	
	
}
	
	
	
xmlhttp.open("POST","sub_match.php",true);
	
	
	
var 
params 'win='+elemText+'&p1='+pl1+'&p2='+pl2+'&match='+matrou[3]+'&round='+matrou[1]+'&wloc='+$(this).id+'&lloc='+losloc+'&tid='+$.urlParam('tid');
	
	
	
xmlhttp.setRequestHeader("Content-type""application/x-www-form-urlencoded");
	
	
	
xmlhttp.setRequestHeader("Content-length"params.length);
	
	
	
xmlhttp.setRequestHeader("Connection""close");
	
	
	
xmlhttp.send(params);
            
/*$.post("sub_match.php", {win: elemText, p1: pl1, p2: pl2, match: matrou[3], round: matrou[1], wloc: $(this).id, lloc: losloc, tid: $.urlParam('tid')});*/
        
}
    });
  });


On a side note, the variable that is coming back as undefined: 'wloc' before I tried passing everything to the php page I put this into an alert and it didn't come back any of the weird spaces or new lines, and that variables was properly defined so I'm really curious whats causing this as well as my webhosts injected script file code to be thrown in there...

edit: fixed the undefined, I had it fixed earlier but reverted.  Just need to figure out the rest now...
« Last Edit: June 02, 2012, 06:41:17 PM by Sabmin »