gpt4 book ai didi

php - 表单提交上的成功/错误消息未显示

转载 作者:行者123 更新时间:2023-11-28 21:12:17 25 4
gpt4 key购买 nike

我有一个 php/ajax/javascript 表单,我正在尝试开始工作。在 John Fable 的大力帮助下,我只需要显示成功和错误消息即可。在这里查看我们的主题: Putting multiple forms on the same page

我的表格位于: http://www.testing123.co.za/try/test-toggle.htm

我的目标是在一页上有多个表单,但当您在任一表单上单击提交时,不会显示成功或错误消息,而是在提交按钮附近旋转一个加载 gif。

表单的 JavaScript 如下所示:

 // JavaScript Document

jQuery(document).ready(function() {
$('.contactform').submit(function() {
var action = $(this).attr('action');
$('.submit', this).attr('disabled', 'disabled').after('<img src="assets/ajax-
loader.gif" class="loader" />');
$('.message', this).slideUp(750, function() {
$('.message', this).hide();
$.post(action, {
name: $('.name', this).val(),
email: $('.email', this).val(),
phone: $('.phone', this).val(),
dayin: $('.dayin', this).val(),
dayout: $('.dayout', this).val(),
comments: $('.comments', this).val(),
verify: $('.verify', this).val()
},
function(data) {
$('.message', this).html(data);
$('.message', this).slideDown('slow');
$('img.loader', this).fadeOut('fast', function() {
$(this).remove();
});
$('.submit', this).removeAttr('disabled');
if (data.match('success') != null);
$('.message', this).show().delay(5000).fadeOut();
});
});
return false;
});
});

HTML 格式:

<body>

<div id="container">

<div id="accommodation_listing_options_box">

<ul id="accommodation_listing_options">

<li>Price Range: <a href="#" class="tooltip" style="cursor:help;" title="Mid-range
Accommodation"><img src="../mid-range-yellow.png" width="28" height="19"
align="absmiddle" style="padding-left:0px;" /></a></li>

<li><a href="#">View Comments</a> <img src="../comments_bubble_small.png" width="18"
height="16" align="absmiddle" style="padding-left:5px;" /></li>

</ul>

</div>

<div id="quick_enquiry_box">Make Quick Enquiry</div>

<div style="clear:both; width:710px;"></div>



<div style="clear:both;"></div>

<div id="slide_panel">

<div id="contact">

<form method="post" action="contact.php" name="contactform" id="contactform"
class="contactform"><div class="message"></div>

<div id="my_contact_left">



<label for="name" accesskey="U"><span class="required">*</span>Name</label><br
/>

<input name="name" type="text" id="name" size="30" class="name" />

<br />

<label for="email" accesskey="E"><span class="required">*
</span>Email</label><br />

<input name="email" type="text" id="email" size="30" class="email" />

<br />

<label for="phone" accesskey="P"><span class="required">*</span>Phone:
</label><br />

<input name="phone" type="text" id="phone" size="30" class="phone" />

<br />

<label for="dayin" accesskey="P">Day in:</label><br />

<input name="dayin" type="text" id="dayin" size="30" class="dayin" />

<br />

<label for="dayout" accesskey="P">Day out:</label><br />

<input name="dayout" type="text" id="dayout" size="30" class="dayout" />

</div>

<div id="my_contact_right">

<label for="comments" accesskey="C"><span class="required">*</span>Your
Comments</label><br />

<textarea name="comments" cols="40" rows="3" id="comments" class="comments"
style="width: 350px; height:100px;"></textarea>

<p><span class="required">*</span>Type the validation code in below</p>

<div style="width:100px; height:40px; float:left;"><label for="verify"
accesskey="V">&nbsp;&nbsp;&nbsp;<img src="image.php" alt="Image verification"
border="0"/></label></div>

<div style="width:310px; height:40px; float:right;"><input name="verify"
type="text" id="verify" class="verify" size="6" value="" style="width: 50px;" />

<input type="submit" style="margin-left:112px;" class="submit" id="submit"
value="Send it!" /></div>

<div style="clear:both; width:410px;"></div>

</div>

<div style="clear:both; width:710px;"></div>

</form>

</div>

<div id="quick_form_wrapper_close"><a href="#"><img src="../close-panel-button.gif"
/></a>
</div>

</div>



<div id="accommodation_listing_options_box">

<ul id="accommodation_listing_options">

<li>Price Range: <a href="#" class="tooltip" style="cursor:help;" title="Mid-range
Accommodation"><img src="../mid-range-yellow.png" width="28" height="19"
align="absmiddle" style="padding-left:0px;" /></a></li>

<li><a href="#">View Comments</a> <img src="../comments_bubble_small.png" width="18"
height="16" align="absmiddle" style="padding-left:5px;" /></li>

</ul>

</div>

<div id="quick_enquiry_box2">Make Quick Enquiry</div>

<div style="clear:both; width:710px;"></div>



<div style="clear:both;"></div>

<div id="slide_panel2">

<div id="contact">



<form method="post" action="contact2.php" name="contactform" id="contactform"
class="contactform"><div class="message"></div>

<div id="my_contact_left">



<label for="name" accesskey="U"><span class="required">*</span>Name</label><br
/>

<input name="name" type="text" id="name" size="30" class="name" />

<br />

<label for="email" accesskey="E"><span class="required">*
</span>Email</label><br />

<input name="email" type="text" id="email" size="30" class="email" />

<br />

<label for="phone" accesskey="P"><span class="required">*</span>Phone:
</label><br />

<input name="phone" type="text" id="phone" size="30" class="phone" />

<br />

<label for="dayin" accesskey="P">Day in:</label><br />

<input name="dayin" type="text" id="dayin" size="30" class="dayin" />

<br />

<label for="dayout" accesskey="P">Day out:</label><br />

<input name="dayout" type="text" id="dayout" size="30" class="dayout" />

</div>

<div id="my_contact_right">

<label for="comments" accesskey="C"><span class="required">*</span>Your
Comments</label><br />

<textarea name="comments" cols="40" rows="3" id="comments" class="comments"
style="width: 350px; height:100px;"></textarea>

<p><span class="required">*</span>Type the validation code in below</p>

<div style="width:100px; height:40px; float:left;"><label for="verify"
accesskey="V">&nbsp;&nbsp;&nbsp;<img src="image.php" alt="Image verification"
border="0"/></label></div>

<div style="width:310px; height:40px; float:right;"><input name="verify"
type="text" id="verify" class="verify" size="6" value="" style="width: 50px;" />

<input type="submit" style="margin-left:112px;" class="submit" id="submit"
value="Send it!" /></div>

<div style="clear:both; width:410px;"></div>

</div>

<div style="clear:both; width:710px;"></div>

</form>

</div>

<div id="quick_form_wrapper_close2"><a href="#"><img src="../close-panel-button.gif"
/></a> </div>

</div>

</div>

</body>

成功/错误消息应出现在 div 中:

<div class="message"></div>您可以看到上面的 html 表单。

您知道为什么提交时没有显示成功/错误消息吗?

提前致谢。

最佳答案

试试这个(可能仍然不是 100% 正确):

jQuery(document).ready(function() {
$('.contactform').submit(function() {
var action = $(this).attr('action');
var form = this;
$('.submit', this).attr('disabled', 'disabled').after(
'<img src="assets/ajax-loader.gif" class="loader" />');
$('.message', this).slideUp(750, function() {
$(this).hide();
$.post(action, {
name: $('.name', form).val(),
email: $('.email', form).val(),
phone: $('.phone', form).val(),
dayin: $('.dayin', form).val(),
dayout: $('.dayout', form).val(),
comments: $('.comments', form).val(),
verify: $('.verify', form).val()
},
function(data) {
$('.message', form).html(data);
$('.message', form).slideDown('slow');
$('img.loader', form).fadeOut('fast', function() {
$(this).remove();
});
$('.submit', form).removeAttr('disabled');
if (data.match('success') != null)
$('.message', form).show().delay(5000).fadeOut();
});
});
return false;
});
});

关于php - 表单提交上的成功/错误消息未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8357127/

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