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 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败