Jump to content

AJAX Success returning whole page html


Adamhumbug
 Share

Go to solution Solved by Adamhumbug,

Recommended Posts

Hi All,

I have an ajax call:

$('#createNewgame').click(function(){
		var errors = [];

		var gv = $('#gameVenue').val();
		var ht = $('#homeTeam').find(":selected").val();
		var at = $('#awayTeam').find(":selected").val();
		
		if(gv == '') {
			$out = "Game Venue";
			errors.push($out);
		}
		if(ht == 0) {
			$out = "Home Team";
			errors.push($out);
		}
		if(at == 0) {
			$out = "Away Team";
			errors.push($out);
		}
		if(errors.length != 0){
			var finalArray = arrayToStringWithComma(errors)
			$('.newGameAlertContainer').html("<div class='alert alert-warning newGameAlert hiddenForFade'>"+finalArray+" must be completed!</div>")
			$('.newGameAlert').fadeIn().delay(3000).fadeOut(1000);

			return;
		}

		if (errors.length == 0){
			$.ajax({
				type: 'post',
				data: {'ajax': 'createNewgame', 'venue' : gv, 'home': ht,'away': at
				},
				success: function(resp){
					console.log(resp)
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) { 
					alert("Status: " + textStatus); alert("Error: " + errorThrown); 
				}       
			})
		}

	})

then i have

if(isset($_POST['ajax'])){
	switch($_POST['ajax']){
		case 'createNewGame':
		exit(createNewGame($_POST['gv'], $_POST['ht'], $_POST['at']));
		break;
	}
}


function createNewGame($venue, $home, $away){
	include 'includes/dbconn.php';

	$qry = "INSERT INTO game (venue, home_team, away_team) VALUES (?,?,?)";
	$stmt = $conn -> prepare($qry);
	$stmt -> bind_param('sii', $venue, $home, $away);
	$stmt -> execute();

	$gameID = mysqli_insert_id();
	$_SESSION['activeGame'] = $gameID;

	return "i ran";

}

the success response that i am console logging is the entire html of the whole page.

All of this code appears in the same file.

Any ideas or pointers?

All help appreciated, and have a great new year!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.