简体   繁体   English

在javascript和php之间传递值-弹出PHP Javascript HTML

[英]Issue passing value between javascript and php - Popup PHP Javascript HTML

I am trying to incorporate a popup into my project. 我正在尝试将弹出窗口合并到我的项目中。 It works up until I try and pass a value from javascript to the php script. 在我尝试将javascript的值传递给php脚本之前,它一直有效。 I have included a link to the example I used and link to the original script. 我提供了我所使用的示例的链接以及原始脚本的链接。 There were slight changes made to the original script. 对原始脚本进行了一些更改。 The area I am having an issue with is marked with a ----> 我遇到问题的区域标有---->

full script can be found here: http://www.webdeveloper.com/forum/showthread.php?279111-Contact-form-popup-window 完整的脚本可以在这里找到: http : //www.webdeveloper.com/forum/showthread.php?279111-Contact-form-popup-window

working example can be found here: http://ilolo.ru/wd/contact_form/ 可以在以下位置找到工作示例: http : //ilolo.ru/wd/contact_form/

I have the following in hello.html 我在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='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>

The following js is in a .js that I reference in my html. 以下js位于我在html中引用的.js中。 I use the following since I have more than one form. 我使用以下表格,因为我有多个表格。 Just showed one form in my example. 在我的示例中仅显示了一种形式。 The alert below works....shows the correct email address when I click on the image link in the html. 当我单击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 email.php

This value below is not getting sent by the javascript and the input value=$email is not written correctly (I think) 下面的这个值不是由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; 
} 
?> 

In your javascript you are sending literal email instead of your email value - 在您的JavaScript中,您发送的是原义email而不是email值-

$.post('email.php',{mode: 'snd', email: 'email'},function(data){

Change this to - 更改为-

$.post('email.php',{mode: 'snd', email: email},function(data){

In your php code you have a variable scope issue - $email is outside the scope of your function 在您的php代码中,您遇到了范围可变的问题- $email在函数范围之外

$email=$_POST[ 'email']; 
function showForm(){ 
echo $email;

Try setting it inside 尝试将其设置在里面

function showForm(){ 
$email=$_POST[ 'email']; 
echo $email;

Finally, you have $email inside single quotes- 最后,您在单引号内包含$email email-

echo '<form name="mf" id="mf" action="" method="post">
    <h2>Send Email To Customer</h2><br />
    <input type='text ' value='$email '/> 
    ...

Need to update to 需要更新到

echo '<form name="mf" id="mf" action="" method="post">
    <h2>Send Email To Customer</h2><br />
    <input type="text" value="'.$email.'"/>
    ...

Additionally, it looks like when you are submitting the for you are not sending the comments textarea, so you probably need to add that as well. 此外,看起来好像在提交时,因为您没有发送评论文本区域,因此您可能还需要添加它。

$('#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'));
        });
});

Edit 编辑

You need to add the email value to your $.get() - 您需要将email值添加到$.get() -

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

and then change it to $_GET['email'] in your php - 然后在您的php中将其更改为$_GET['email'] -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM