我编写了一份联系表格。如果mail()成功,我想调用我的jquery弹出窗口。
我该怎么做?
jQuery(function($) {
var popup_zustand = false;
$(".kontakt_oeffnen").submit(function() {
if(popup_zustand == false) {
$("#kontakt").fadeIn("normal");
$("#hintergrund").css("opacity", "0.7");
$("#hintergrund").fadeIn("normal");
popup_zustand = true;
}
return false;
});
});
if(mail($address, $e_subject, $msg))
{
//?????
} else
{
echo 'ERROR!';
}/**/
如果邮件功能成功,我只想显示弹出窗口。
两者都不适合我... sry ..也许我太傻了。
这是我的完整代码:
file:popup.js
jQuery(function($) {
var popup_zustand = false;
$(".kontakt_oeffnen").submit(function() {
if(popup_zustand == false) {
$("#kontakt").fadeIn("normal");
$("#hintergrund").css("opacity", "0.7");
$("#hintergrund").fadeIn("normal");
popup_zustand = true;
}
return false;
});
$(".schliessen").click(function() {
if(popup_zustand == true) {
$("#faq").fadeOut("normal");
$("#kontakt").fadeOut("normal");
$("#imprint").fadeOut("normal");
$("#hintergrund").fadeOut("normal");
popup_zustand = false;
}
});
});
档案:contact.php
<?php>
if(mail($address, $e_subject, $msg))
{
//?????
} else
{
echo 'ERROR!';
}
?>
文件:contact_inc.php
<form method="post" action="classes/contact.php" name="contactform" id="contactform" autocomplete="on">
<input name="name" type="text" id="input_text" required="required" size="24" maxlength="30"/>
<input name="email" type="email" id="input_text" pattern="^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$" required="required" size="24" maxlength="30">
<input name="phone" type="tel" id="input_text" size="24" />
<input name="company" type="text" id="input_text" placeholder="Firma..." size="24" maxlength="30">
<textarea style="height:100%" name="comments" cols="30" rows="10" id="input_text" spellcheck="true" required="required"></textarea>
<input type="submit" id="SubmitButton" value="Absenden" />
</form>
如果我添加到我的按钮classes =“kontakt_oeffnen”,它将始终弹出...而不发送电子邮件。 但是我应该只在电子邮件发送正确时弹出。 我不能只从php调用jquery函数(问题标记是......)
答案 0 :(得分:1)
我会将源更改为lil位
HTML
<form>
<input name="name" type="text" id="input_text" required="required" size="24" maxlength="30" />
<input name="email" type="email" id="input_text" pattern="^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$" required="required" size="24" maxlength="30" />
<input name="phone" type="tel" id="input_text" size="24" />
<input name="company" type="text" id="input_text" placeholder="Firma..." size="24" maxlength="30" />
<textarea style="height:100%" name="comments" cols="30" rows="10" id="input_text" spellcheck="true" required="required"></textarea>
<input type="submit" id="SubmitButton" value="Absenden" />
</form>
的jQuery
$(document).ready(function () {
$('#SubmitButton').on('click', function () {
var formData = $('form').serializeArray();
var fData = Array();
var postActive = false; // only because i dont have the contact.php (denie error)
$(formData).each(function(){
//console.log(this.name + ":" + this.value);
fData[this.name] = this.value;
});
if(postActive)
{
//now submit data to php file
$.post('classes/contact.php', {
data: fData // deal with info in phpfile, $_POST['data']['name'] or
// $_POST['data']['email'] .. return your mail result with a echo.
// lets say echo 1; for true or echo 0 for false
// because true and false in php is not the same as in js
}).done(function (response) {
//response contains 1 (mail was send!)
// by the way... you have a function befor/complete/error for handeling results...
// check out jquery.com
alert(response);
/* CANT find this in your "ALL MY SOURCE" post :)
if (response === false) {
$("#kontakt").fadeIn("normal");
$("#hintergrund").css("opacity", "0.7");
$("#hintergrund").fadeIn("normal");
popup_zustand = true;
}
*/
});
}
console.log(fData); // open console to browse the array
});
});
PHP文件(classes / contact.php)
<?php
//some source
// Data from Form is available like next row
$recipient = $_POST['data']['email'];
$name = $_POST['data']['name'];
....
$result = 0;
if(mail($rescipient, ... ))
$result = 1;
echo $result;
?>
当然,您可以使用JSON来更好地使用:)
这样一切都很干净,你的HTML,你的PHP和你的jQuery源分开了:)
PS:查看 FIDDLE
答案 1 :(得分:0)
您可以尝试将其混合:
<?php
if(mail($address, $e_subject, $msg)) {
?>
<script>
jQuery(function($) {
var popup_zustand = false;
$(".kontakt_oeffnen").submit(function() {
if(popup_zustand == false) {
$("#kontakt").fadeIn("normal");
$("#hintergrund").css("opacity", "0.7");
$("#hintergrund").fadeIn("normal");
popup_zustand = true;
}
return false;
});
});
</script>
<?
} else {
echo 'ERROR!';
}/**
?>