我如何将jquery值传递给php函数


How can i pass the jquery value to php function

这里我想用一些参数来调用php函数,这些参数将保存在一些jquery函数中。

我有一个php函数,它接受一个参数并执行sql查询。我也有一个jquery函数,我得到不同的值时,从下拉框选择项目。

现在我想把下拉框的值传递给PHP函数,我该怎么做?以下是我所做的

$("#product_category").change(function(){
    var category = $(this).val(); //getting dropdown value
    // here i want to pass this variable value to php function
});
php

    function productManagement($procat){
        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
    $sql = "SELECT product_name,product_category,product_image_url FROM product_list where product_category='$procat'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
        echo "<tr><td>". $row["product_name"]."</td><td><a href=''><span class='glyphicon glyphicon-remove'></span>Remove</a></td><td><a href='edit-product.php'><span class='glyphicon glyphicon-edit'></span>Edit</a></td></tr>";
        }
    } else {
        echo "No results found";
    }
    $conn->close();
}

我该怎么做?

另一种方法是使用Ajax,如:

Jquery代码:

$("#product_category").change(function(){
    var category = $(this).val(); //getting dropdown value
    // here i want to pass this variable value to php function
-----------------------------------------------------
$.ajax({
  url: 'newFunctionFile.php',
  type: 'POST',
  data: 'category='+category,
  success: function(data) {
    //you can add the code to show success message
  },
  error: function(e) {
    //called when there is an error
    //console.log(e.message);
  }
});
});

你必须为php函数创建一个文件比如文件名是newfunctionfile。php我在ajax调用中提到过:

if(isset($_POST['category'])) {                
    productManagement($_POST['category']);    
}
 function productManagement($procat){
        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
    $sql = "SELECT product_name,product_category,product_image_url FROM product_list where product_category='$procat'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
        echo "<tr><td>". $row["product_name"]."</td><td><a href=''><span class='glyphicon glyphicon-remove'></span>Remove</a></td><td><a href='edit-product.php'><span class='glyphicon glyphicon-edit'></span>Edit</a></td></tr>";
        }
    } else {
        echo "No results found";
    }
    $conn->close();
}

这样写怎么样?

在你的javascript:

$("#product_category").change(function(){
var category = $(this).val();                        //getting dropdown value
location.href = location.href + "?val="+category;    //reload the page with a parameter
});

然后在PHP中,添加以下代码作为函数调用

if(isset($_GET['val'])) {                //if 'val' parameter is set (i.e. is in the url)
    productManagement($_GET['val']);     //call function, with that parameter
}