last day (14 days later) » 

10:55 AM
0
A: Retrieving data value from database from ajax function

DwijenEverything seems to be fine, except this bit - function processSearch(data) { alert(data['username']); //Am I doing it right here? } You need to change it to - function processSearch(data) { // data is a JSON object. Need to access it with dot notation alert(data.username); } UPD...

 
Thank you for the answer. Can you give some example for the JSON response? Should I do it like this 'username' => $row['username']; ?
 
updated the answer, hope this helps.
 
I have changed it, but still, no alert is showing up? Or at least if the ajax is not working the alert should popup with blank values. But now it's just not showing any alert when the button is clicked. Perhaps there is anything wrong with the script?
 
In that case there could be 2 possible issues, either your ajaxForm is not being called or there is some error in the PHP script. To check if the call is made add a line beforeSubmit: function(){alert("MAKING AJAX CALL")} and in your success call back just do an alert of data. Also in your php script return json_encode(["hello" => "world"])
By the way do you have any errors in your console?
 
the Ajax call alert showed up after clicking the button, so the ajaxForm is working correctly isn't ? and calling the data with data fails to show the hello world alert.
 
10:55 AM
Are there any errors in your console?
 
Hello there, since it asked me to move over chat, maybe we can start here
No there is no errors reported in my console
 
Sure
Hmmmmm, can you check your network tab in the browser to see if the Ajax call is returning a 200 status code or not?
 
Yes the ajax call returned the 200 status code
I wonder what could be the problem?
 
Can you please paste the exact code in your JS?
 
The JS code that I'm using right now is the ajax function call, jquery-1.12.3, and the ajax form plugin.
$(document).ready(function() {
$('#searchForm').ajaxForm({
beforeSubmit: function(){alert("MAKING AJAX CALL")},
dataType: 'json',
success: processSearch
});
});

function processSearch(data) {
// data is a JSON object. Need to access it with dot notation
alert(data);
}
that is the function call that you fixed earlier
 
11:07 AM
Can you try this?
function processSearch(responseText, statusText, xhr, $form) { alert("HELLO"); }
 
There is no alert showed up.
Could it be the function processSearch is not being called?
There is no "Hello" alert showed up*
The ajax call alert is working fine
 
yeah thats what my guess is
 
So is there any way to fix it?
 
thats the only reason i can think of why it wouldnt be called
i am not sure, the person who wrote the plugin would have to fix it, because its called internally
oh wait
can u try one more thing
can you move the processSearch function inside the $(document).ready
 
But Ive tried to do it like this
No luck.
$(document).ready(function() {
$('#searchForm').ajaxForm({
beforeSubmit: function(){alert("MAKING AJAX CALL")},
dataType: 'json',
success: function processSearch(responseText, statusText, xhr, $form) { alert("HELLO"); }
});
and an error showed up
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /Applications/XAMPP/xamppfiles/htdocs/CONNECT/Core/Database/Connection.php on line 4
Or I just did it wrong?
is there anything to do with it?
 
11:24 AM
hmmm
in your php file can you comment everything, except return json_encode(["h" => "w"]);
 
Tried and no luck in getting alert.
 
in that case the problem seems to be with that plugin
can you remove the dataType from the ajaxForm
 
YES! It's finally showing the hello alert!!!
What makes you think it could be the problem?
 
my guess is that the response header was returning application type as text/html
you will have to manually set the header to type json in the response
u can also manually parse the string to JSON in jquery in the success function
at the top of your php file set the header like so - header('Content-Type: application/json');
 
I have changed the header as you suggested. But then the problem came back. The alert isn't showing up again
 
11:40 AM
yeah so now u need to set the dataType to json
 
Still the same. Should I put the header at the top of the "Search page" PHP or in the searchFunction.php ?
 
just after the <?php should be fine
 
Nope it didn't work after adding the header.
Does it needed for the ajax to works in transferring the data?
 
11:55 AM
no not really, you can just parse the JSON in the Success callback
var data = JSON.parse(data) will work just fine
 
So should I do it like this in calling back the data?
function processSearch(data) {
var data = JSON.parse(data);
alert(data.username);
}
 
yeah
that should work
 
12:11 PM
The alert is gone again when I parse the json
sigh
If I comment the JSON and let alone alert(data.username); it will gave an alert box saying "undefined"
 
sorry my bad JSON.parse works in ruby
for jQuery its this - jQuery.parseJSON(data)
or $.parseJSON(data)
 
Still the same. The alert won't show. :l
It's okay. As long as you're willing to help. :)
 
12:31 PM
is the php returning the $row?
 
Yes. php is return the $row
 
and what does ur JS look like now?
can you paste it here please?
 
<script type="application/javascript">
$(document).ready(function() {
$('#searchForm').ajaxForm({
beforeSubmit: function(){alert("MAKING AJAX CALL")},
success: processSearch
});

});

function processSearch(data) {
//jQuery.parseJSON(data)
alert(data.username);
}
</script>
$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 json_encode($row);
thats for the php
 
what do you get when you just alert(data)
 
I got the alert box, but instead of showing the data. It says "Undefined"
 
12:38 PM
in php before the return can you do var_dump($row); exit;
 
It still shows undefined whenever I submit a search
 

last day (14 days later) »