gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:04:14 25 4
gpt4 key购买 nike

一切都与以下文件一样完美,但在 process.php 中,如果我在 if (mysql_query($sql)) 之后回显注释掉的消息,则 form.php 页面转到 process.php 页面,该页面在 my_script.js 中被阻止 - 换句话说,来自 my_script 的一些 jQuery 功能。 js 不再工作了。有时,恢复到原始回显线仍然不会恢复 jQuery 功能,直到我删除并重新创建 process.php 文件。

这是我的第一个完整表格,但发生的事情有点令人毛骨悚然......我是否在某个地方遗漏了一些东西?

表单.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');

?>

流程.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.";

此外,当我尝试重置回之前的模式时,有时事情不会立即开始正常工作,需要几分钟......

最佳答案

您遇到 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?

关于javascript - PHP -(如果 INSERT true)回显消息禁用/关闭 .js 文件中的 jQuery 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36961028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com