Note that there are some explanatory texts on larger screens.

plurals
  1. POuse one text field to search database, then pupulate remaining fields in PHP
    text
    copied!<p>I'm new here, so I hope this makes sense I am new to PHP and Ajax...so I am quite lost.</p> <p>I have a MySQL database setup, and several forms created in PHP. I have several text fields on one of the forms, which is supposed to show customer info. The first text (phone #) field is supposed to be a search field ... so I need to query my database once the user finishes entering the phone #, to find the records and populate the remaining fields.</p> <p>I've tried various ways of doing this...but nothing comes out quite right! The method which seems to show the most promise is by using the Onblur event to call a php page (using Ajax) which will perform the search.</p> <p>This <em>works</em>, in that I can query the database to find the results .. and have them displayed back. The trouble is that this page with the data inside is actually displayed in the middle of my first page ...so basically I have one web page displaying inside the other...which is obviously not what I want.</p> <p>The basic code I have is:</p> <p>HTML side --</p> <pre><code> &lt;script&gt; function showUser(str) { if (str == "") { document.getElementById("divider").innerHTML = ""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4) { document.getElementById("divider").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "GetCustomer.php?q="+str, true); xmlhttp.send(); } &lt;/script&gt; &lt;/head&gt; &lt;label for="CustPhone"&gt;Phone:&lt;/label&gt; &lt;input type="text" name="CustPhone" id="CustPhone" tabindex="1" onchange="showUser(this.value)" value="&lt;?php ?&gt;"&gt; &lt;br&gt; &lt;div id="divider"&gt;&lt;/div&gt; &lt;label for="CustLastName"&gt;Last Name:&lt;/label&gt; &lt;input type="text" name="CustLastName" id="CustLastName" tabindex="2" value="&lt;?php echo $clast; ?&gt;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;label for="CustFirstName"&gt;First Name:&lt;/label&gt; &lt;input type="text" name="CustFirstName" id="CustFirstName" tabindex="3" value="&lt;?php echo $cfirst; ?&gt;"&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;label for="CustAddress1"&gt;Address 1:&lt;/label&gt; &lt;input type="text" name="CustAddress1" id="CustAddress1" tabindex="4" value="&lt;?php echo $caddr1; ?&gt;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;label for="CustAddress2"&gt;Address2:&lt;/label&gt; &lt;input type="text" name="CustAddress2" id="CustAddress2" tabindex="5" value="&lt;?php echo $caddr2; ?&gt;"&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; </code></pre> <p>and the PHP --</p> <pre><code>include ("AddEdit.php"); require_once("customer_info.php"); function LoadData() { $pn = $_POST['CustPhone']; if (strlen($Pn) &lt; 10) { $query = "SELECT * FROM customer WHERE Phone='$Pn'"; $result = mysql_query($query); if (!$result) die ("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); $rowinfo = mysql_fetch_array($result); if ($rows &gt; 0) { $clast = $rowinfo['LastName']; $cfirst = $rowinfo['FirstName']; $caddr1 = $rowinfo['Address1']; $caddr2 = $rowinfo['Address2']; $ccity = $rowinfo['City']; $cprov = $rowinfo['Province']; $cpostal = $rowinfo['Postal']; } } </code></pre> <p>} </p> <p>Any ideas as to how I can do this without jumping around as much ... or more importantly just show the data on the existing page, rather than showing a smaller version of my entire page within the page ??</p> <p>Thx!!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload