简体   繁体   English

如何通过php函数中的ajax传递变量的值

[英]how to pass value of variable through ajax in php function

i have two input fields for example(A and B) whose values are coming through database, the value of field B depend upon the value of A. I am using ajax call to change the values of input field B. here is my code: 我有两个输入字段,例如(A和B),其值通过数据库传递,字段B的值取决于A的值。我正在使用ajax调用来更改输入字段B的值。这是我的代码:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
    function ajaxfunction(parent)
    {
        $.ajax({
            url: 'http://localhost/test/process.php',
            data: { vall : parent }, 
            success: function(data) {
                $("#sub").html(data);
            }
        });
    }
</script>
</head>

<body>
<select onchange="ajaxfunction(this.value)">
<?php 
$q= mysql_query("select * from tab1");
while ($row= mysql_fetch_array($q)){
$name= $row['name'];    
echo '

<option value="'.$name.'"> '.$name.' </option>
';
}
?>
</select>

<select id="sub"></select>

</body>
</html>

and my process.php was: 而我的process.php是:

$e =$_GET['vall'];
echo $e;
    $result = mysql_query("SELECT * FROM tab2 WHERE name = '".$e."' ");
    while(($data = mysql_fetch_array($result)) !== false)
        echo '<option value="', $data['id'],'">', $data['lname'],'</option>'

It is working fine for me. 对我来说很好。 Now the problem is I am working in classes and I want to get the value in a function, Now i have the following structure of process.php 现在的问题是我在类中工作,我想在一个函数中获取值,现在我具有以下process.php结构

    class location{ 

    public function getlocation($e)
        {

            $sql="SELECT * FROM tab2 WHERE name = '".$e."'";
            return $this->objConnect->GetData($sql);
        }
public function GetallpropertyFeatured(){ 

        $sql="select * from sale_property WHERE category='Featured' AND accept_status='Active'  ORDER BY property_id DESC  ";   
        return $this->objConnect->GetData($sql);
    }//for showing all propery on the base of recent status.
    public function GetallpropertyRecent(){ 

        $sql="select * from sale_property WHERE category='Recent' AND accept_status='Active' ORDER BY property_id DESC  ";  
        return $this->objConnect->GetData($sql);
    }//for showing all propery on the base of trending status.
    public function GetallpropertyTrending(){ 

        $sql="select * from sale_property WHERE category='Trending' AND accept_status='Active' ORDER BY property_id DESC  ";    
        return $this->objConnect->GetData($sql);
    }
    //for Getting propery on the base of featured status.
    public function GetpropertyByFeatured(){ 

        $sql="select * from sale_property WHERE category='Featured' AND accept_status='Active' ORDER BY property_id DESC  ";    
        return $this->objConnect->GetData($sql);
    }
    //for Getting propery on the base of latest status.
    public function GetpropertyByRecent()
    {
            $sql="select * from sale_property WHERE category='Recent' AND accept_status='Active' ORDER BY property_id DESC ";
        return $this->objConnect->GetData($sql);
    }
    //for Getting propery on the base of trending status.
    public function GetpropertyByTrending()
    {

        $sql="select * from sale_property WHERE category='Trending' AND accept_status='Active' ORDER BY property_id DESC ";
        return $this->objConnect->GetData($sql);
    }
    //for Getting propery on the base of File status.
    public function GetpropertyByFile()
    {
            $sql="select * from sale_property WHERE type='File' AND accept_status='Active' ORDER BY property_id DESC ";
        return $this->objConnect->GetData($sql);
    }
    public function GetallpropertyByFile()
    {
            $sql="select * from sale_property WHERE type='File' AND accept_status='Active' ORDER BY property_id DESC  ";
        return $this->objConnect->GetData($sql);
    }

        public function getimages($propertyid)
    {
        $sql="select images from images_property  where property_id=$propertyid";
        return $this->objConnect->GetData($sql);


    }
    // for getting images of property
    public function getimg($propertyid)
    {
        $sql="select images from images_property  where property_id=$propertyid  limit 0,3";
        return $this->objConnect->GetData($sql);


    }

    }

so what should be the url in ajax how I pass value in ajax now, right now i am passing like this : 所以我应该如何在ajax中传递url,现在我如何像这样传递ajax中的url:

 $.ajax({
            url: 'http://localhost/test/process.php',
            data: { vall : parent },

Regards 问候

You don't need any change from the client side. 您不需要客户端进行任何更改。 However, to obtain the value in your class, this should be the code for your new process.php file. 但是,要在您的类中获取值,这应该是新的process.php文件的代码。

class location {

    public function getLocation($e) {

        global $sql;

        $sql = "SELECT * FROM tab2 WHERE name = '" . $e . "'";
        return $this->objConnect->GetData($sql);
    }

}

if (isset($_GET['val1'])) {

    $location = new location();
    $location = $location->getLocation($_GET['val1']);

}

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

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