0

I'm currently trying to make live form validation with PHP and AJAX. So basically - I need to send the value of a field through AJAX to a PHP script(I can do that) and then I need to run a function inside that PHP file with the data I sent. How can I do that?

JQuery:

$.ajax({
  type: 'POST',
  url: 'validate.php',
  data: 'user=' + t.value,   //(t.value = this.value),
  cache: false,
  success: function(data) {
    someId.html(data);
  }
});

Validate.php:

// Now I need to use the "user" value I sent in this function, how can I do this?

function check_user($user) {
  //process the data
}

If I don't use functions and just raw php in validate.php the data gets sent and the code inside it executed and everything works as I like, but if I add every feature I want things get very messy so I prefer using separate functions.

I removed a lot of code that was not relevant to make it short.

3
  • you can get data in $_POST and than call that function whats problem ? Commented Feb 4, 2013 at 14:38
  • You could pass in a post data value of 'function=check_user' and then in the php code use a switchcase command pattern to execute accordingly, see stackoverflow.com/questions/2269307/… for more deats... Commented Feb 4, 2013 at 14:40
  • check stackoverflow.com/q/5004233/1723893 Commented Feb 4, 2013 at 14:44

5 Answers 5

2

1) This doesn't look nice

data: 'user=' + t.value,   //(t.value = this.value),

This is nice

data: {user: t.value},  

2) Use $_POST

function check_user($user) {
    //process the data
}
check_user($_POST['user'])
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I read that previously today, but why exactly is the {user: t.value} better?
It is easier to read and it is syntax of official documentation. So I suppose you better hold to this syntax if you don't want to get in trouble now and later.
1

You just have to call the function inside your file.

if(isset($_REQUEST['user'])){
    check_user($_REQUEST['user']);
}

2 Comments

Thanks! This is what I was looking for. With some more tinkering I managed to get things working by calling the function at the top of my PHP, but this is much better!
I did not include this in my answer - but please take care to sanitize ANY input you receive via GET, POST etc.! If you don't this will open doors wide for XSS and SQL injection.
1

In your validate.php you will receive classic POST request. You can easily call the function depending on which variable you are testing, like this:

<?php

if (isset($_POST['user'])) {
    $result = check_user($_POST['user']);
}
elseif (isset($_POST['email'])) {
    $result = check_email($_POST['email']);
}
elseif (...) {
    // ...
}

// returning validation result as JSON
echo json_encode(array("result" => $result));
exit();

function check_user($user) {
   //process the data
   return true; // or flase
}

function check_email($email) {
   //process the data
   return true; // or false
}

// ...
?>

Comments

0

The data is send in the $_POST global variable. You can access it when calling the check_user function:

check_user($_POST['user']);

If you do this however remember to check the field value, whether no mallicious content has been sent inside it.

Comments

-1

Here's how I do it

Jquery Request

$.ajax({
            type: 'POST',
            url: "ajax/transferstation-lookup.php",
            data: {
                'supplier': $("select#usedsupplier").val(),
                'csl': $("#csl").val()
            },
            success: function(data){
                if (data["queryresult"]==true) {
                    //add returned html to page
                    $("#destinationtd").html(data["returnedhtml"]);
                } else {
                    jAlert('No waste destinations found for this supplier please select a different supplier', 'NO WASTE DESTINATIONS FOR SUPPLIER', function(result){ return false; });
                }
            },
            dataType: 'json'
        });

PHP Page Just takes the 2 input

$supplier = mysqli_real_escape_string($db->mysqli,$_POST["supplier"]);
$clientservicelevel = mysqli_real_escape_string($db->mysqli,$_POST["csl"]);

Runs them through a query. Now in my case I just return raw html stored inside a json array with a check flag saying query has been successful or failed like this

$messages = array("queryresult"=>true,"returnedhtml"=>$html);
echo json_encode($messages); //encode and send message back to javascript

If you look back at my initial javascript you'll see I have conditionals on queryresult and then just spit out the raw html back into a div you can do whatever you need with it though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.