Hello, I hope that I am posting this in the correct forum. This particular situation involves PHP & AJAX.
I have created a database, form and an alphabetic menu of sorts that grabs all results housed in the database and displays them by last name alphabetically when a letter is clicked. I would then like to be able to click on a single result within the populated results and have the information autofill the fields in my form.
Here is a snippet of my html:
Here is my PHP:HTML Code:<!-- Letter Search --> <div class="searchBox span12"> <h3>Choose the First Letter of the Person's Last Name</h3> <ul> <li class="alphabets" id="A"><a href="?by=A">A</a></li> <li class="alphabets" id="B"><a href="?by=B">B</a></li> <li class="alphabets" id="C"><a href="?by=C">C</a></li> <li class="alphabets" id="D"><a href="?by=D">D</a></li> <li class="alphabets" id="E"><a href="?by=E">E</a></li> <li class="alphabets" id="F"><a href="?by=F">F</a></li> <li class="alphabets" id="G"><a href="?by=G">G</a></li> <li class="alphabets" id="H"><a href="?by=H">H</a></li> <li class="alphabets" id="I"><a href="?by=I">I</a></li> <li class="alphabets" id="J"><a href="?by=J">J</a></li> <li class="alphabets" id="K"><a href="?by=K">K</a></li> <li class="alphabets" id="L"><a href="?by=L">L</a></li> <li class="alphabets" id="M"><a href="?by=M">M</a></li> <li class="alphabets" id="N"><a href="?by=N">N</a></li> <li class="alphabets" id="O"><a href="?by=O">O</a></li> <li class="alphabets" id="P"><a href="?by=P">P</a></li> <li class="alphabets" id="Q"><a href="?by=Q">Q</a></li> <li class="alphabets" id="R"><a href="?by=R">R</a></li> <li class="alphabets" id="S"><a href="?by=S">S</a></li> <li class="alphabets" id="T"><a href="?by=T">T</a></li> <li class="alphabets" id="U"><a href="?by=U">U</a></li> <li class="alphabets" id="V"><a href="?by=V">V</a></li> <li class="alphabets" id="W"><a href="?by=W">W</a></li> <li class="alphabets" id="X"><a href="?by=X">X</a></li> <li class="alphabets" id="Y"><a href="?by=Y">Y</a></li> <li class="alphabets" id="Z"><a href="?by=Z">Z</a></li> </ul> <? include('search.php'); ?> </div> <!-- End Letter Search --> <hr style="color:#ccc; margin-bottom:20px;" /> <!-- Main Form --> <div id="mainForm"> <form method="post" id="icsForm" class="searchBox span12"> <div id="col1" class"span6"> <h3>Contact Information</h3> <label>Church / Organization:</label><input type="text" name="organization" id="organization" class="span6 upright" /><br /> <label>First Name:</label><input type="text" name="firstName" id="firstName" class="span6 upright" /> <label>Last Name:</label><input type="text" name="lastName" id="lastName" class="span6 left upright" /> <label>Email Address:</label><input type="text" name="email" id="email" class="span6 left upright" /> <label>Phone Number:</label><input type="text" name="phone" id="phone" class="span6 left upright" /> </div>
Here is my javascript / ajax:PHP Code:include("dbconnect.php");
if(preg_match("/^[A-Z | a-z]+/", $_POST['name'])){
$name=$_POST['name'];
}
if(isset($_GET['by'])){
$letter=$_GET['by'];
//query to sort by last name
$sql="SELECT contact_id, first_name, last_name, church_org, email_address, phone_number FROM ics_data WHERE last_name LIKE '$letter%' ORDER BY last_name ASC";
//run the query against the mysql query function
$result=mysql_query($sql);
//count the results
$numrows=mysql_num_rows($result);
echo "<p>" .$numrows . " results found for " . $letter . "</p>";
//Create a while loop and loop through result set - array
while($row=mysql_fetch_array($result)){
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$church_org=$row['church_org'];
$email_address=$row['email_address'];
$phone_number=$row['phone_number'];
$contact_id=$row['contact_id'];
//display the result of the array
echo "<div id=\"search-results\">";
echo "<ul class=\"letter-results\">\n";
echo "<li class=\"result-row\">" . "<a href=\"\" class=\"testclass\">" .$first_name . " " .$last_name . "". ", " ."" .$church_org ."</a></li>\n";
echo "</ul>";
echo "</div>";
}
}
$formData = json_encode($row);
Any help or guidance would be so much appreciated!! I am learning this bit by bit and this is my first big attempt to create something useful.Code:$(document).ready( function() { function formfill() { var organization = $('#organization').val(); var firstname = $('#firstname').val(); var lastname = $('#lastname').val(); var email = $('#email').val(); var phone = $('#phone').val(); $.ajax ({ method: "GET", url: "search.php", dataType: 'json', data: { organization:organization, firstname:firstname, lastname:lastname, email:email, phone:phone }, type: "POST", success: function(data) { $organization $firstname $lastname $email $phone }, failure: function() { alert('fail!'); } }); } $('.result-row').click(function() { formfill(); }); });![]()



Reply With Quote
I'm sure it's unintentional . . .

Bookmarks