gpt4 book ai didi

php - 使用 PHP+Jquery 隐藏和显示 div 时遇到问题

转载 作者:行者123 更新时间:2023-12-02 20:22:57 24 4
gpt4 key购买 nike

用户提交他们的电子邮件地址,检查电子邮件是否有效且不在数据库中。

我遇到的问题是,一旦表单提交成功,我就无法隐藏表单并显示感谢 div。目前,它用“谢谢!”替换了电子邮件文本框。我不想要的文本。

必须更改或添加哪些内容才能隐藏表单(在通过错误/验证检查后)并显示感谢 div?

谢谢!!

Index.php

<?php

require "includes/connect.php";

$msg = '';

if($_POST['email']){

// Requested with AJAX:
$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');

try{
if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
throw new Exception('Invalid Email!');
}

$mysqli->query("INSERT INTO coming_soon_emails
SET email='".$mysqli->real_escape_string($_POST['email'])."'");

if($mysqli->affected_rows != 1){
throw new Exception('You are already on the notification list.');
}

if($ajax){
die('{"status":1}');
}

$msg = "Thank you!";

}
catch (Exception $e){

if($ajax){
die(json_encode(array('error'=>$e->getMessage())));
}

$msg = $e->getMessage();
}
}
?>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Notify me test</title>

<link rel="stylesheet" type="text/css" href="css/styles.css" />

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="js/script.js"></script>

</head>

<body>

<div id="launch">

<h1>Notify</h1>

<h2>Notify me</h2>

<form id="form" method="post" action="">
<input type="text" id="email" name="email" value="<?php echo $msg?>" />
<input type="submit" value="Submit" id="submitButton" />
</form>

<div style="display:none" id="thankyou">
Thank you!
</div>


</div>

</body>
</html>

script.js

$(document).ready(function(){

// Binding event listeners for the form on document ready

$('#email').defaultText('Enter your Email Address');

// 'working' prevents multiple submissions
var working = false;

$('#page form').submit(function(){

if(working){
return false;
}
working = true;

$.post("./index.php",{email:$('#email').val()},function(r){
if(r.error){
$('#email').val(r.error);
}
else $('#email').val('Thank you!');

working = false;
},'json');

return false;
});
});

// A custom jQuery method for placeholder text:

$.fn.defaultText = function(value){

var element = this.eq(0);
element.data('defaultText',value);

element.focus(function(){
if(element.val() == value){
element.val('').removeClass('defaultText');
}
}).blur(function(){
if(element.val() == '' || element.val() == value){
element.addClass('defaultText').val(value);
}
});

return element.blur();
}

最佳答案

如果您不想用感谢文本替换输入,为什么要这样做?

else $('#email').val('Thank you!');

相反,其他{
$("#form").hide();
$("#thankyou").show();
}

关于php - 使用 PHP+Jquery 隐藏和显示 div 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5299819/

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