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

时间:2017-02-17 06:19:32

标签: javascript php jquery ajax

我有两个输入字段,例如(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>

我的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>'

这对我来说很好。现在问题是我在类中工作,我想在函数中获取值,现在我有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);


    }

    }

那么ajax中的url应该是什么,我现在在ajax中传递值,现在我正在这样传递:

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

此致

2 个答案:

答案 0 :(得分:0)

你不能直接将php变量传递给ajax调用,因为我们可以使用隐藏字段设置隐藏字段的值,并在当时从隐藏字段调用ajax检索值。

答案 1 :(得分:0)

您无需从客户端进行任何更改。但是,要获取类中的值,这应该是新的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']);

}