Jump to content

ask if any post data is present


jimmyt1988

Recommended Posts

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.

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.