jimmyt1988 Posted November 14, 2010 Share Posted November 14, 2010 I'm doing an ajax call from javascript using jquery library: ajaxCall: function(process, obj, type){ $.post("registration-post.php", type + "=" + process, function(data){ if(data==""){ $(obj).css(registration.errorNotificationType, registration.validatedColour); }else{ $('.error').append($.trim(data) + "<br />"); $(obj).css(registration.errorNotificationType, registration.errorColour); registration.progress = false; } } ); } When the ajax calls the php page, I want the php page to know what post information the javascript has sent, rather than pre-defining what the PHP should be trying to retrive from the POST data. Something like // pseudo $_POST["whateverwassentfromajax"] I want to do this because I want to have one function doing everything. So in my javascript I am saying, check if the username and email have already been used... But i dont want to call two different php functions. //pseudo function 1(){ $_POST["username"]; } function 2(){ $_POST["email"]; } Is there a special way to know which post data has been sent. Or do i need to do a few if statements in php. Quote Link to comment https://forums.phpfreaks.com/topic/218651-ask-if-any-post-data-is-present/ Share on other sites More sharing options...
marcus Posted November 14, 2010 Share Posted November 14, 2010 The question seems a bit ambiguous, I'm not sure if what I've derived is what you want but... You can use array_keys on your $_POST variable. That will store just the names of the values sent. So if the user only passed username and email the array would be: Array ( [0] => username [1] => email ) Quote Link to comment https://forums.phpfreaks.com/topic/218651-ask-if-any-post-data-is-present/#findComment-1134096 Share on other sites More sharing options...
jimmyt1988 Posted November 14, 2010 Author Share Posted November 14, 2010 I guess a better question would be: if i didnt know what the field names were on a form... How could i get the data the form is sending. so $_POST["I dont need to know the field name, to know that i have something to recieve"] Quote Link to comment https://forums.phpfreaks.com/topic/218651-ask-if-any-post-data-is-present/#findComment-1134107 Share on other sites More sharing options...
marcus Posted November 14, 2010 Share Posted November 14, 2010 You could check if if(count($_POST) > 0){ // something has been posted } Quote Link to comment https://forums.phpfreaks.com/topic/218651-ask-if-any-post-data-is-present/#findComment-1134108 Share on other sites More sharing options...
jimmyt1988 Posted November 14, 2010 Author Share Posted November 14, 2010 perfect, and if i wanted to get the value of the data from a POST that i do not know the name to? $data = $_POST ; echo $data (this would echo out jimmyt1988) if the field that was sent contained this info. EDIT: Oh hangon, does that mean $_POST[0] will return the first post entry because $_POST is an array of values? If so thats answered the question. $_POST[0] will get me the data i need when i do not know the field name. Quote Link to comment https://forums.phpfreaks.com/topic/218651-ask-if-any-post-data-is-present/#findComment-1134110 Share on other sites More sharing options...
marcus Posted November 14, 2010 Share Posted November 14, 2010 No, $_POST is an array of keys and values. So $_POST[0]; wouldn't return anything. You could do something like: if(count($_POST) > 0){ $keys = array_keys($_POST); echo $_POST[$keys[0]]; } Quote Link to comment https://forums.phpfreaks.com/topic/218651-ask-if-any-post-data-is-present/#findComment-1134115 Share on other sites More sharing options...
jimmyt1988 Posted November 14, 2010 Author Share Posted November 14, 2010 Oh so close, So array_keys can return the data of a field, is there a way to return the fieldname rather than the data. i tried array_values but it didnt work hehe. so the above statement returned jimmyt1988 but now i need to know what that data belonged to.. username, email etc Quote Link to comment https://forums.phpfreaks.com/topic/218651-ask-if-any-post-data-is-present/#findComment-1134119 Share on other sites More sharing options...
jimmyt1988 Posted November 14, 2010 Author Share Posted November 14, 2010 *bump* i'm still stuck with the same question above. this: if(count($_POST) > 0){ $keys = array_keys($_POST); echo $_POST[$keys[0]]; } returns the value of the field i dont know the name of. How do i return the fieldname of the field i dont know the name of? Quote Link to comment https://forums.phpfreaks.com/topic/218651-ask-if-any-post-data-is-present/#findComment-1134140 Share on other sites More sharing options...
marcus Posted November 14, 2010 Share Posted November 14, 2010 No, that returns the name of the field processing through. Here is my example: <form method="post"> <input type="hidden" name="name1" value="cat1" /> <input type="hidden" name="name2" value="cat2" /> <input type="hidden" name="name3" value="cat3" /> <input type="submit" name="submit" /> </form> <?php if(count($_POST) > 0){ $keys = array_keys($_POST); foreach($keys AS $field){ echo $field . " has the value of " . $_POST[$field] . "<br />"; } } ?> When I hit submit I get the following: name1 has the value of cat1 name2 has the value of cat2 name3 has the value of cat3 submit has the value of Submit Query Quote Link to comment https://forums.phpfreaks.com/topic/218651-ask-if-any-post-data-is-present/#findComment-1134149 Share on other sites More sharing options...
jimmyt1988 Posted November 14, 2010 Author Share Posted November 14, 2010 HAHAHA, im so sorry.. THankyou!!! Ah what a numb nut i am. Thanks a bunch, yehaa, working. Quote Link to comment https://forums.phpfreaks.com/topic/218651-ask-if-any-post-data-is-present/#findComment-1134158 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.