繁体   English   中英

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

[英]How can i pass the jquery value to php function

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

我有一个接受一个参数并执行 sql 查询的 php 函数。 而且我还有一个 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,例如:

查询代码:

$("#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 函数创建一个文件,比如我在 ajax 调用中提到的文件名是 newFunctionFile.php:

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM