如果数据库中存在输入,则重定向到URL

时间:2017-03-20 11:29:35

标签: javascript php html mysql ajax

如果用户输入的值已经存在于数据库中,我正在尝试将用户重定向到现有的URL(存储在MySQL数据库中)。我已经大多数工作,但我遇到了一些问题。

我正在使用AJAX来检查数据库。它将输入值发送到另一个PHP文件&检查数据库以查看它是否已存在。

goodbye cruel world

我在AJAX代码中遇到了问题,尤其是:

if($row) {
    echo ".$row["itemNum"].";
} else {

}

success: function (response) { if (response == <?php ".$row["itemNum"]." ?>) { window.location.href = "http://www.example.com/" <?php echo "+"; if (".$row["itemNum"].") { echo ".$row["itemNum"]." } else {""} } else { // do other stuff } } 并不总是存在,所以它在条件语句中以防止语法错误。

无论我怎么做,上述都不起作用。

我觉得主要问题是$row["itemNum"]行吗?

2 个答案:

答案 0 :(得分:3)

基于此代码: -

if($row) {
    echo $row["itemNum"]; // check the change here
} else {

}

你必须这样做: -

success: function (response) {
    if (response !== '') { // if response have some value
        window.location.href = "http://www.example.com/"+response;
    } else {
        // do other stuff
    }
}

答案 1 :(得分:0)

如您所知,您对下一行有疑问。

if (response == <?php ".$row["itemNum"]." ?>) {

问题是您使用(&#34;)和(。)字符

以下语法将为您提供所需的内容。

if (response == <?=$row["itemNum"]?>) {
相关问题