Re: Inserting databasecontent within the same form

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Niel,

> SELECT Kunden.*, City.name, City.vorwahl FROM Kunden LEFT JOIN City
> USING (zipcode)

I'm understanding you this is a way for designing the sql-statement.
My customer want that they have inserted the city after typing in the
zipcode in the zipfield. The city should be searched in a database and
inserted in the field city automaticly.

I think the only way is to do that with some ajax-code combined with a
short phpscript.

Actually I have these codes, but the automatic inserting fails, I don't
know why.

---------------------

ajax.js

function Ajax() {
  //Eigenschaften deklarieren und initialisieren
  this.url="";
  this.params="";
  this.method="GET";
  this.onSuccess=null;
  this.onError=function (msg) {
    alert(msg)
  }
}

Ajax.prototype.doRequest=function() {
  //Üeberpruefen der Angaben
  if (!this.url) {
    this.onError("Es wurde kein URL angegeben. Der Request wird
abgebrochen.");
    return false;
  }

  if (!this.method) {
    this.method="GET";
  } else {
    this.method=this.method.toUpperCase();
  }

  //Zugriff auf Klasse für readyStateHandler ermöglichen
  var _this = this;

  //XMLHttpRequest-Objekt erstellen
  var xmlHttpRequest=getXMLHttpRequest();
  if (!xmlHttpRequest) {
    this.onError("Es konnte kein XMLHttpRequest-Objekt erstellt werden.");
    return false;
  }

  //Fallunterscheidung nach Übertragungsmethode
  switch (this.method) {
    case "GET": xmlHttpRequest.open(this.method,
this.url+"?"+this.params, true);
                xmlHttpRequest.onreadystatechange = readyStateHandler;
                xmlHttpRequest.send(null);
                break;
    case "POST": xmlHttpRequest.open(this.method, this.url, true);
                 xmlHttpRequest.onreadystatechange = readyStateHandler;
                 xmlHttpRequest.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
                 xmlHttpRequest.send(this.params);
                 break;
  }

  //Private Methode zur Verarbeitung der erhaltenen Daten
  function readyStateHandler() {
    if (xmlHttpRequest.readyState < 4) {
      return false;
    }
    if (xmlHttpRequest.status == 200 || xmlHttpRequest.status==304) {
      if (_this.onSuccess) {
        _this.onSuccess(xmlHttpRequest.responseText,
xmlHttpRequest.responseXML);
      }
    } else {
      if (_this.onError) {
        _this.onError("["+xmlHttpRequest.status+"
"+xmlHttpRequest.statusText+"] Es trat ein Fehler bei der
Datenbertragung auf.");
      }
    }
  }
}

//Gibt browserunabhängig ein XMLHttpRequest-Objekt zurück
function getXMLHttpRequest()
{
  if (window.XMLHttpRequest) {
    //XMLHttpRequest für Firefox, Opera, Safari, ...
    return new XMLHttpRequest();
  } else
  if (window.ActiveXObject) {
    try {
      //XMLHTTP (neu) für Internet Explorer
      return new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        //XMLHTTP (alt) für Internet Explorer
        return new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        return null;
      }
    }
  }
  return false;
}
-----------------------------------------

einfuegen.js

//Instanz der Klasse Ajax erzeugen und mit der Datenübertragung starten
function load()
{

  var plz=document.getElementById("plz").value;

  with (new Ajax()){

    url="ort_suchen.php";
    method="POST";
    params="plz="+plz;
    onSuccess=successHandler;
    onError=errorHandler;
    doRequest();
  }


//Den Text in die Seite einfügen
function successHandler(txt,xml){
  document.getElementById("ort").innerHTML=txt;

}

//Fehler
function errorHandler(msg){
  document.getElementById("text").innerHTML=msg;
}

}

-----------------------------------------------
ort_suchen.php

<?
include("authentication.php");
$plz = mysql_real_escape_string ( $_POST['plz'],$link);



$ort="";

$result=mysql_db_query($datenbank,"SELECT * from Orte WHERE PLZ = '$plz'");

  while ($row=mysql_fetch_object($result)){


  $ort= $row->Bezeichnung;


  }

echo $ort;

?>

-----------------------------------------

in the source-phpscript that do the request I have this

html-header
databaseconnection

...
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript" src="einfuegen.js"></script>
...

some formcode
...
<tr>
      <td>PLZ</td>
      <td><INPUT type="text" id="plz" name="plz" onkeyup="load()"></td>
    </tr>
    <tr>
      <td>Ort</td>
      <td><INPUT type="text" id="ort" name="ort">
	<div id="ort">Ort</div>
...


Regards,
Ruprecht Helms

Web: http://www.rheyn.de

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux