I have an ajax function that will retrieve data from my database and display it in my text box. I am using jQuery Form Plugin to shorten ajax process a bit. Now what I want to do is return the data from the php script that I called from the ajax function.
Layout Form:
<form action="searchFunction.php" method="post" id="searchForm">
<input type="text" name="searchStudent" id="searchStudent" class="searchTextbox" />
<input type="submit" name="Search" id="Search" value="Search" class="searchButton" />
</form>
Server code in searchFunction.php
$cardid = $_POST['searchStudent'] ;
$cardid = mysql_real_escape_string($cardid);
$sql = mysql_query("SELECT * FROM `users` WHERE `card_id` = '$cardid'") or trigger_error(mysql_error().$sql);
$row = mysql_fetch_assoc($sql);
return $row;
ajax script processing php,
$(document).ready(function() {
$('#searchForm').ajaxForm({
dataType: 'json',
success: processSearch
});
});
function processSearch(data) {
alert(data['username']);
}
In PHP, if I want to call data, I just simply create a function for the database and just, for example, echo $row['username']it. But how do I do this with ajax? I am new to this, please explain this process.
source
share