2013-10-12 47 views
0

我想將一個彈出框合併到我的項目中。它工作,直到我嘗試從JavaScript傳遞一個值到PHP腳本。我已經包含了一個鏈接到我使用的例子並鏈接到原始腳本。原始劇本略有改動。我有標有問題的區域---->問題在javascript和php之間傳遞值 - Popup PHP Javascript HTML

完整的腳本可以在這裏找到: http://www.webdeveloper.com/forum/showthread.php?279111-Contact-form-popup-window

工作示例可以在這裏找到: http://ilolo.ru/wd/contact_form/

我有以下在hello.html

<form id='form1' style='width: 1100px'> 
    <table> 
     <th>Email</th> 
     <th>Options</th> 
     <tr> 
      <td> 
       <input type='text' id='email1' size='25' value='[email protected]'> 
      </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')); 
    }); 
    }); 
    }); 
    }); 
    }); 

email.php

低於此值是沒有得到通過JavaScript和輸入值= $電子郵件不正確寫入(我認爲)

<?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; 
} 
?> 
+4

杜de,縮進你的代碼。像這樣幾乎不可讀。 – ceejayoz

回答

1

發在你的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.'"/> 
    ... 

此外,它看起來像當你提交你不發送註釋文本區域,所以你可能需要添加爲好。

$('#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() - 它在你的PHP

$.get('email.php', email: email, function (data) { 

,然後切換到$_GET['email'] -

function showForm(){ 
$email=$_GET[ 'email']; 
+0

嘿肖恩......謝謝你的迴應。 – stapuff

+0

嘿肖恩......謝謝你的迴應。以下是我在完成所有建議的更改後發現的內容......它不會在我添加評論時發表評論:評論。表格凍結。當我點擊電子郵件按鈕彈出表單時,來自javascript的電子郵件地址仍然不顯示在php中...我只是看到窗體上顯示「echo」一詞。謝謝,Kurt – stapuff

+0

一個快速更新...找出回聲問題,但值仍然沒有得到從JavaScript傳遞到PHP。 – stapuff