Tenaciousmug Posted June 1, 2011 Share Posted June 1, 2011 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='' Quote Link to comment https://forums.phpfreaks.com/topic/238147-_post-not-grabbing-the-value/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 1, 2011 Share Posted June 1, 2011 The data parameter needs to be in the form of a key/value - data: key1=value1 For your code, this should work - data: "gender=" + gender, Quote Link to comment https://forums.phpfreaks.com/topic/238147-_post-not-grabbing-the-value/#findComment-1223772 Share on other sites More sharing options...
Tenaciousmug Posted June 1, 2011 Author Share Posted June 1, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/238147-_post-not-grabbing-the-value/#findComment-1223774 Share on other sites More sharing options...
jcbones Posted June 2, 2011 Share Posted June 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238147-_post-not-grabbing-the-value/#findComment-1223875 Share on other sites More sharing options...
kenrbnsn Posted June 2, 2011 Share Posted June 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238147-_post-not-grabbing-the-value/#findComment-1223877 Share on other sites More sharing options...
jcbones Posted June 2, 2011 Share Posted June 2, 2011 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. ,with the opinion that the OP needs to rethink how AJAX works. Quote Link to comment https://forums.phpfreaks.com/topic/238147-_post-not-grabbing-the-value/#findComment-1224351 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.