jQuery get () php button submit

advertisements

I have the following jquery code

$(document).ready(function() {
//Default Action
$("#playerList").verticaltabs({speed: 500,slideShow: false,activeIndex: <?=$tab;?>});
$("#responsecontainer").load("testing.php?chat=1");
var refreshId = setInterval(function() {
  $("#responsecontainer").load('testing.php?chat=1');
}, 9000);
$("#responsecontainer2").load("testing.php?console=1");
var refreshId = setInterval(function() {
  $("#responsecontainer2").load('testing.php?console=1');
}, 9000);

$('#chat_btn').click(function(event) {
  event.preventDefault();
  var say = jQuery('input[name="say"]').val()
 if (say) {
  jQuery.get('testing.php?action=chatsay', { say_input: say} );
  jQuery('input[name="say"]').attr('value','')
} else {
  alert('Please enter some text');
}
});

$('#console_btn').click(function(event) {
event.preventDefault();
var sayc = jQuery('input[name="sayc"]').val()
if (sayc) {
  jQuery.get('testing.php?action=consolesay', { sayc_input: sayc} );
  jQuery('input[name="sayc"]').attr('value','')
} else {
  alert('Please enter some text');
}
});

$('#kick_btn').click(function(event) {
event.preventDefault();
var player_name = jQuery('input[name="player"]').val()
if (player_name) {
  jQuery.get('testing.php?action=kick', { player_input: player_name} );
} else {
  alert('Please enter some text');
}
});

});

Sample Form

      <form id=\"kick_player\" action=\"\">
    <input type=\"hidden\" name=\"player\" value=\"$pdata[name]\">
    <input type=\"submit\" id=\"kick_btn\" value=\"Kick Player\"></form>

And the handler code

if ($_GET['action'] == 'chatsay') {
$name = USERNAME;
$chatsay = array($_GET['say_input'],$name);
$api->call("broadcastWithName",$chatsay);
die("type: ".$_GET['type']." ".$_GET['say_input']);
}

if ($_GET['action'] == 'consolesay') {
$consolesay = "§4[§f*§4]Broadcast: §f".$_GET['sayc_input'];
$say = array($consolesay);
$api->call("broadcast",$say);
die("type: ".$_GET['type']." ".$_GET['sayc_input']);
}
if ($_GET['action'] == 'kick') {
 $kick = "kick ".$_GET['player_input'];
 $kickarray = array($kick);
 $api->call("runConsoleCommand", $kickarray);
 die("type: ".$_GET['type']." ".$_GET['player_input']);
}

When I click the button, it reloads the page for starters, and isn't supposed to, it also isn't processing my handler code. I've been messing with this for what seems like hours and I'm sure it's something stupid.

What I'm trying to do is have a single button (0 visible form fields) fire an event. If I have to have these on a seperate file, I can, but for simplicity I have it all on the same file. The die command to stop rest of file from loading. What could I possibly overlooking?

I added more code.. the chat_btn and console_btn code all work, which kick is setup identically (using a hidden field rather than a text field). I cant place whats wrong on why its not working :(


use return false event.instead of preventDefault and put it at the end of the function

ie.

$(btn).click(function(event){
    //code
    return false;
});

And you should probably be using json_decode in your php since you are passing json to the php script, that way it will be an array.