PHP - (如果INSERT为true)echo消息禁用/关闭.js文件中的jQuery功能

时间:2016-04-30 22:41:21

标签: javascript php jquery mysql

所有内容都与以下文件一样完美,但process.php如果我在if (mysql_query($sql))之后回显已注释的邮件,那么form.php页面将转到process.php页面在my_script.js中阻止了 - 换句话说,来自my_script.js的一些jQuery功能不再起作用。在擦除并重新创建process.php文件之前,有时恢复到原始回显线仍然不会带回jQuery功能。

这是我的第一份完整表格,但发生的事情有点怪异......我在某处错过了什么?

form.php的

<?php
//filename - form.php
?>

<html>
<head><title>Registration Form: jQuery</title></head>
<body>

<form id="myForm" action="process.php" method="POST">
Username: <input type="text" name="username"/><br />
Password: <input type="password" name="pass"/><br />
First Name: <input type="text" name="fname"/><br />
Last Name: <input type="text" name="lname"/><br />
<button id="submit">register</button>
</form>

<div id="ack"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
<script type="text/javascript" src="script/my_script.js"></script>
</body>
</html>

db.php中

<?php
//filename - db.php

// mysql_connect for process.php -
$conn = mysql_connect('myhost.mysql.com', 'formdb_user', 'pwd');
$db   = mysql_select_db('formdb');

?>

process.php

<?php
//filename - process.php

//capture errors
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

//initialize
//global $message;

//connect to formtable
include_once ('db.php');

//tried this file with mysqli method and parameters and it didin't solve the problem!
$username = mysql_real_escape_string ($_POST['username']);
$password = mysql_real_escape_string (md5($_POST['pass']));
$fname = mysql_real_escape_string ($_POST['fname']);
$lname = mysql_real_escape_string ($_POST['lname']);

$sql = "INSERT INTO `formtable` (`username`, `password`, `fname`, `lname`) VALUES ('$username', '$password', '$fname', '$lname')";

if (mysql_query($sql))
echo "Inserted Successfully";
//echo "Thank You! We'll Review Your Application.";
else
echo "Insertion Failed";
//echo "Submit Failed - Please Refresh Page And Try Again.";
//this method didn't help either - echo htmlspecialchars($message, ENT_QUOTES, 'UTF-8');

?>

my_script.js

$("#submit").click( function() {
     $.post( $("#myForm").attr("action"),
             $("#myForm :input").serializeArray(),
             function(info) {
               $("#ack").empty();
               $("#ack").html(info);
                clear();
             });
    $("#myForm").submit( function() {
       return false;    
    });
});

function clear() {

    $("#myForm :input").each( function() {
          $(this).val("");
    });

}

修改

以下任何问题都可以解决:

if (mysql_query($sql))
echo "Inserted Successfully";
//echo "Thank You! We'll Review Your Application.";
else
echo "Insertion Failed";
//echo "Submit Failed - Please Refresh Page And Try Again.";

使用以下内容时问题开始了。表单数据被插入到表中,但是我在另一个页面上获得成功消息 - 表单的操作页面process.php,根据jQuery文件中的代码,my_script.js,不应该发生。

if (mysql_query($sql))
//echo "Inserted Successfully";
echo "Thank You! We'll Review Your Application.";
else
//echo "Insertion Failed";
echo "Submit Failed - Please Refresh Page And Try Again.";

此外,当我尝试重置回上一个模式时,有时事情不能立即开始正常工作,需要几分钟......

1 个答案:

答案 0 :(得分:0)

您收到PHP语法错误。如果在ifelse中有多个语句,则需要将它们包装在大括号中。如果您在if之后省略大括号,然后放置多个语句,则else不再匹配,因此您会收到错误。

if (mysql_query($sql)) {
    echo "Inserted Successfully";
    echo "Thank You! We'll Review Your Application.";
} else {
    echo "Insertion Failed";
    echo "Submit Failed - Please Refresh Page And Try Again.";
}

我建议您养成始终的习惯,使用if/while/for/foreach身体周围的大括号,即使他们只是一行。见Why is it considered a bad practice to omit curly braces?