2017-02-17 4 views
0

Ich habe zwei Eingabefelder zum Beispiel (A und B), deren Werte durch die Datenbank kommen, hängt der Wert von Feld B von dem Wert von A. Ich benutze Ajax Anruf die Werte der Eingabefeld B zu ändern ist mein Code:wie Wert der Variablen durch AJAX in php übergeben Funktion

<!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> 

und meine process.php war:

$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>' 

Es funktioniert gut für mich. Nun ist das Problem, das ich in den Klassen arbeite, und ich möchte den Wert in einer Funktion erhalten, jetzt habe ich die folgende Struktur 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, was die URL in Ajax sein sollte, wie ich Wert übergeben in ajax jetzt, gerade jetzt ich so bin vorbei:

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

Grüße

+0

Wenn Sie neuen Code schreiben, verwenden Sie bitte nicht die 'mysql_ *' -Funktionen **. Sie sind alt und kaputt, wurden in PHP 5.5 (das so alt ist, dass es nicht einmal mehr Sicherheits-Updates erhält) veraltet und in PHP 7 vollständig entfernt. Verwenden Sie ['PDO'] (https://secure.php.net/manual /de/book.pdo.php) oder ['mysqli_ *'] (https://secure.php.net/manual/de/book.mysqli.php) mit _prepared statements_ und _parameter binding_ statt. Weitere Informationen finden Sie unter http://stackoverflow.com/q/12859942/354577. – Chris

Antwort

0

Sie benötigen keine Änderung von der Clientseite. Um jedoch den Wert in Ihrer Klasse zu erhalten, sollte dies der Code für Ihre neue Datei process.php sein.

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']); 

} 
+0

aber ich habe zu viele Funktionen in meiner process.php, ich möchte nicht verwenden, wenn (isset ($ _ GET ['val1'])) { $ location = new location(); $ location = $ location-> getLocation ($ _GET ['val1']); } Gibt es einen anderen Weg ?? –

+0

Können Sie bitte Ihren ursprünglichen Beitrag bearbeiten und mehr von Ihrem Code dort, damit wir es überprüfen können? – Areeb

+0

ich bearbeite bitte überprüfen –

0

Sie nicht pHP-Variable direkt an ajax-Aufruf für das passieren können wir versteckte Felder den Wert des ausgeblendeten Feldes und zu dem Zeitpunkt verwenden können Aufruf von Ajax ruft den Wert aus dem versteckten Feld ab.

Verwandte Themen