weixin_33692284 2014-04-04 14:01 采纳率: 0%
浏览 13

Ajax代码不起作用

So I have this program in which the user enters a city and a country. The program looks in the database to see if the city doesn't already exists, if it does I show a warning message using ajax, if not i add the city to the database.

This is the form:

<form action="addCity.php" method="get" onsubmit="return validateCityInfoForm();">

onsumbit I call the javascript function validateCityInfoForm() that looks like this:

function validateCityInfoForm() {

                    var xmlhttp;
                    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 && xmlhttp.status == 200)
                        {


                            if (xmlhttp.responseText == "true") {
                                document.getElementById("checkIfCityExistsWarning").style.display = "block";
                                document.getElementById("checkIfCityExistsWarning").innerHTML = "This city already exists!";
                                return false;
                            }
                        }
                    }
                    xmlhttp.open("GET", "checkIfCityExists.php?city=" + cityInput + "&country=" + countryInput, true);
                    xmlhttp.send();




} 

checkIfCityExists.php echoes "true" if the city already exists in the database and "false" otherwise.

The problem is that it always adds the city in the db even though the city already exists. checkIfCityExists.php returns "true" but it doesn't seem to matter.

I really don't know what the problem is, any help would be greatly appreciated. Thanks!

here is checkIfCityExists.php:

<?php
include ('database_connection.php');
$city = mysqli_real_escape_string($dbc, $_GET['city']);
$country = mysqli_real_escape_string($dbc, $_GET['country']);

//check if the city and country already exists in the database
$query_verify = "SELECT * FROM city WHERE name = '$city' AND country = '$country'";
$result_verify = mysqli_query($dbc, $query_verify);

if(mysqli_num_rows($result_verify) == 0) { //if the city does not appear in the database
    echo "false";
}
else {
    echo "true";
}

?>
  • 写回答

2条回答 默认 最新

  • elliott.david 2014-04-04 14:09
    关注

    The problem is, your onsubmit has no return. So validateCityInfoForm() returns undefined which does not prevent the Browser from executing the action. validateCityInfoForm() should return false to prevent the Browser from submitting the form. And then in the onreadystatechange call form.submit() if necessary.

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序