Sending data with AJAX to a PHP file and using tha

2019-08-26 03:25发布

问题:

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.

回答1:

You just have to call the function inside your file.

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


回答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'])


回答3:

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
}

// ...
?>


回答4:

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.



回答5:

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.