我正在尝试将弹出窗口合并到我的项目中。它一直工作到我尝试将值从javascript传递给php脚本。我已经包含一个指向我使用的示例的链接,并链接到原始脚本。对原始脚本进行了轻微更改。我遇到问题的区域标有---->
完整的脚本可以在这里找到:http://www.webdeveloper.com/forum/showthread.php?279111-Contact-form-popup-window
工作示例可以在这里找到:http://ilolo.ru/wd/contact_form/
我有以下内容你好.html
<form id='form1' style='width: 1100px'>
<table>
<th>Email</th>
<th>Options</th>
<tr>
<td>
<input type='text' id='email1' size='25' value='bob@mail.com'>
</td>
<td><a id='1' href='#null' class='contactus'><img src= 'images/emailbutton.jpg' title='Email' border='0' height='24' width='24'></img></a>
</tr>
</table>
</form>
以下 js 位于我在 html 中引用的.js中。我使用以下,因为我有多个表单。在我的示例中只显示了一个表单。下面的警报有效。当我单击html中的图像链接时,显示了正确的电子邮件地址。
function findCenter(obj) {
var deltaX = parseInt(obj.css('padding-left')),
deltaY = parseInt(obj.css('padding-top'));
obj.css('left', $(window).width() / 2 - obj.width() / 2 - deltaX + 'px').css('top', $(window).height() / 2 - obj.height() / 2 - deltaY + 'px');
}
$(document).ready(function () {
$('.contactus').click(function () {
var element = $(this);
var J = element.attr('id');
var email = document.getElementById('email'+J).value;
alert(email);
$('#result').html('<h3>Loading</h3>').css('display', 'block');
findCenter($('#result'));
$.get('email.php', function (data) {
$('#result').html(data);
findCenter($('#result'));
$('#snd').click(function () {
var subject = document.getElementById('subject').value;
var addy = document.getElementById('addy').value;
var comments = document.getElementById('comments').value;
$('#result').append('<br /><br /><div><i>Sending...</i></div>');
$.post('lbi_email.php',{mode: 'snd', subject: subject, addy: addy, comments: comments},function(data){
$('#result').html(data);
findCenter($('#result'));
});
});
});
});
});
电子邮件.php
下面的值不是由javascript发送的,输入值=$email没有正确编写(我认为)
<?php
include( 'connection.php');
function showForm(){
$email=$_POST[ 'email'];
echo '<form name="mf" id="mf" action="" method="post">
<h2>Send Email To Customer</h2><br />
<p><label>Address :</label><input type="text" size="35" name="addy" id="addy" value="'.$email.'"/></p>
<p><label>Subject : </label><input type="text" size="35" name="subject" id="subject" value=""/></p>
<label>Message:</label><textarea style="width: 100%;" cols="20" rows="10" id="comments" name="comments"></textarea>
<p><a href="#" class="btn" id="snd" name="snd"><img src="submitbutton.jpg" title="Submit Email" border="0" height="25" width="75"></img></a></p></form>';
}
function sndForm(){
/* here goes checking data and so on... then sending if everything 's good and sending done show message to the user*/
echo '<h3>Everything''s cool.
<br />
<br />Viva Cuba!</h3>
<script type="text/javascript">
setTimeout(''$("#result").fadeOut("fast")'',3000);
</script>';
}
/*---------------------------------------------------------------------*/
$mode=(!empty($_GET['mode']))?$_GET['mode']:$_POST['mode'];
switch($mode)
{
case 'snd':sndForm();break;
default: showForm();break;
}
?>
在你的 JavaScript 中,你发送的是文字email
而不是你的email
值 -
$.post('email.php',{mode: 'snd', email: 'email'},function(data){
将其更改为 -
$.post('email.php',{mode: 'snd', email: email},function(data){
在你的 php 代码中,你有一个变量作用域问题 - $email
超出了你的函数的范围
$email=$_POST[ 'email'];
function showForm(){
echo $email;
尝试将其设置在里面
function showForm(){
$email=$_POST[ 'email'];
echo $email;
最后,您在单引号内$email
-
echo '<form name="mf" id="mf" action="" method="post">
<h2>Send Email To Customer</h2><br />
<input type='text ' value='$email '/>
...
需要更新到
echo '<form name="mf" id="mf" action="" method="post">
<h2>Send Email To Customer</h2><br />
<input type="text" value="'.$email.'"/>
...
此外,看起来当您提交 for 时,您没有发送评论文本区域,因此您可能还需要添加它。
$('#snd').click(function () {
$('#result').append('<br /><br /><div><i>Sending...</i></div>');
var comments = document.getElementById('comments').value;
$.post('email.php',{mode: 'snd', email: email, comments: comments},function(data){
$('#result').html(data);
findCenter($('#result'));
});
});
编辑
您需要将email
值添加到$.get()
-
$.get('email.php', email: email, function (data) {
然后将其更改为 PHP 中的$_GET['email']
-
function showForm(){
$email=$_GET[ 'email'];