Jump to content

$_POST not grabbing the value?


Tenaciousmug

Recommended Posts

I think this belongs here, but my $_POST['gender'] won't grab the gender that was submitted through a form.

 

I am using AJAX so the page doesn't have to reload so it can go in a smooth transition, but the AJAX is grabbing the value perfectly fine.

I have a feeling the $_POST isn't grabbing the value because the page isn't reloading.. but I don't want to reload it.

These codes are all on the same page.

 

Here is my Javascript:

<script>
function chooseGender()
{
	var gender = $('input[name=gender]:checked', '#submitgender').val();
 	if(gender)
	{
		$.ajax(
		{
			type: "POST",
			url: window.location.pathname,
			data: gender,
			success: function()
			{
				alert("You have chosen to be a " + gender); //It's grabbing it perfectly fine!
				$("#submitgender").hide(); //It hides the gender table so they can't choose a gender since they already have chosen one.
				$("#rest").fadeIn(); //Shows the other table that's labeled "rest" as it's ID so they can choose what base, eyes, etc for that specific gender they've chosen.
			}
		});
	}
	else
	{
		alert('Select a gender.');
	}
}

$(function tabs()
{
	$( "#tabs" ).tabs();
});
</script>

 

But here is the PHP inside the #rest table:

<?php
						$gender = $_POST['gender'];

						$sql = "SELECT * FROM habases WHERE gender='".$gender."'";
						$result = mysqli_query($cxn, $sql) or die(mysqli_error($cxn));
						print_r($sql);
						while ($row = mysqli_fetch_assoc($result))
						{
						$baseimage = $row['image'];
						$baseskin = $row['skin'];

						echo "<img src=\"http://www.elvonica.com/".$baseimage."\" value=\"".$baseskin."\">";
						}
						?>

 

And this is what I'm getting for the print_r:

SELECT * FROM habases WHERE gender=''
Link to comment
Share on other sites

Oops forgot to fix that.

But it still doesn't work. That's why it was like that because I was trying everything possible. :/

Does the page actually have to load or is it possible to show part of the page that was hidden after clicking a button and passing a value?

Link to comment
Share on other sites

But here is the PHP inside the #rest table:

 

PHP does NOT reside in a table, or any other element for that matter.  IT is strictly server side.  Ajax MUST submit the form to a PHP page, then retrieve the results.

 

You should put your PHP code in a separate file, and send the ajax request to that file. You then use javascript to populate the #rest table, then you show that table on the page.  Simply hiding the table on page load, and then requesting to show it, will not show the appropriate table, because you haven't chosen the gender yet.  In order for AJAX to work, you MUST separate the scripts, you cannot call half a page from the server.

Link to comment
Share on other sites

The PHP code doesn't have to be in a separate file. It can be in the same file, but once the part of the script that gets invoked via AJAX executes, an exit() must be executed. I do that all the time.

 

Ken

Link to comment
Share on other sites

The PHP code doesn't have to be in a separate file. It can be in the same file, but once the part of the script that gets invoked via AJAX executes, an exit() must be executed. I do that all the time.

 

Ken

 

I never thought about that, Ken.  Although, I can see how that would work.  My post was made to late at night. :P ,with the opinion that the OP needs to rethink how AJAX works.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.