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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同