Reputation: 21
hi i have some ajax coding in which the if condition is not at all working, whenever the program executes else statement only works even the program satisfies the if statement.
<script type="text/javascript">
function CheckDetails()
{
var http = false;
if (navigator.appName == "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
http = new XMLHttpRequest();
}
var rid = document.new_booking.ph_number.value;
http.onreadystatechange = function() {
if (http.readyState == 4) {
var str_d = http.responseText;
if (str_d == "no") {
document.getElementById('cus_name').focus();
} else {
var str_details = http.responseText;
var arr_details = str_details.split("~");
document.new_booking.cus_name.value = arr_details[0];
document.new_booking.pick_aline1.value = arr_details[1];
document.new_booking.pick_aline2.value = arr_details[2];
document.new_booking.pick_area.value = arr_details[3];
document.new_booking.pick_pincode.value = arr_details[4];
document.new_booking.drop_aline1.focus();
}
}
}
http.open("GET", "ajax.php?evnt=det&rid=" + rid);
http.send();
}
</script>
and its ajax.php file is given below
<?php
if ($_GET['evnt'] == 'det') {
$rid = $_GET['rid'];
include("configure.php");
$select = mysql_query("select * from new_booking where ph_number = '$rid'");
$count = mysql_num_rows($select);
if ($count > 0) {
$row = mysql_fetch_array($select);
echo $row['cus_name']
. "~" . $row['pick_aline1']
. "~" . $row['pick_aline2']
. "~" . $row['pick_area']
. "~" . $row['pick_pincode']
. "~" . $row['drop_aline1']
. "~" . $row['drop_aline2']
. "~" . $row['drop_area']
. "~" . $row['drop_pincode'];
} else {
echo "no";
}
}
?>
Upvotes: 0
Views: 648
Reputation:
I was having similar problem. This worked for me.
if (str_d.replace(/^\s+|\s+$/g, '') == "no")
Upvotes: 0
Reputation: 1884
If I had to guess, I would say that some whitespace is sneaking into your AJAX response somewhere. Because "no "
isn't equal to "no"
, it is always hitting your else
branch.
You might consider sending back a whitespace-agnostic value. You could rewrite the whole mess to use JSON, which would require a lot less work on both ends:
// PHP:
if ($_GET['evnt'] == 'det') {
$rid = $_GET['rid'];
include("configure.php");
$select = mysql_query("select * from new_booking where ph_number = '$rid'");
$count = mysql_num_rows($select);
if ($count > 0) {
$row = mysql_fetch_array($select);
// YOU PROBABLY WANT TO WHITELIST WHAT YOU PASS TO JSON_ENCODE
echo json_encode($row);
} else {
echo json_encode([ "error" => "no" ]);
}
}
// js:
http.onreadystatechange = function() {
if (http.readyState == 4) {
var str_d;
try {
str_d = JSON.parse(http.responseText);
} catch(e) {
str_d = false;
}
if (str_d && str_d.error === "no") {
document.getElementById('cus_name').focus();
} else {
document.new_booking.cus_name.value = str_d.pick_aline1;
document.new_booking.pick_aline1.value = str_d.pick_aline2;
document.new_booking.pick_aline2.value = str_d.pick_area;
// etc...
}
}
}
Upvotes: 0
Reputation: 59
You can open your page with Chrome (or Chromium) and then debug your javascript code using builtin debugger (Ctrl+Shift+I, "Console" tab). I guess you will see some JS errors there.
Basically, your code works OK (at least when I removed all database access from it, since I don't have your DB).
If you don't like Chrome, use Firefox and FireBug extension. On 'Network' page you can see that your ajax request was executed (or not executed).
Upvotes: 1