Jquery posting to php

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] $(window).load(function(){
$(".make-draggable, .draggable, .drag").draggable({
helper: “clone”,
snap: “.draggable”,
snapMode: “inner”
});
$(".draggable").droppable({
drop: function(event, ui) {
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 i = 1;
loser = (losid.charAt(0) + “-”);
pl1 = elemText;
for (i = 1; i < numchars; i++) {
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%2 == 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”, {win: elemText, p1: pl1, p2: pl2, match: matrou[4], round: matrou[2], wloc: $(this).id, lloc: losloc, tid: $.urlParam(‘tid’)});
}
});
});[/php]

with sub_match.php to follow:

[php]<?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);
?>[/php]

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.

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’, )

"

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] $(window).load(function(){
$(".make-draggable, .draggable, .drag").draggable({
helper: “clone”,
snap: “.draggable”,
snapMode: “inner”
});
$(".draggable").droppable({
drop: function(event, ui) {
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 i = 1;
loser = (losid.charAt(0) + “-”);
pl1 = elemText;
for (i = 1; i < numchars; i++) {
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%2 == 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 + '=([^&#])’).exec(window.location.href);
return results[1] || 0;
}
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && 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’)});*/
}
});
});[/php]

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…

Sponsor our Newsletter | Privacy Policy | Terms of Service