php - 将 Post var 发送到 Jquery 模式弹出窗口

标签 php jquery html

我是Jquery的初学者,我试着做一些简单的例子来练习, 在此示例中,我尝试将 POST var 从 php 表单发送到我在互联网上找到的 Jquery modale 弹出窗口。

我的问题是,当我点击提交链接发送我的 php 表单时,我无法将我的表单 var 发送到我的模式弹出窗口,

弹出窗口在我的例子 $name 中没有 var。

这里是我的代码:

Index.php :

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="test.css" />
<script type="text/javascript" src="script.js"></script>

<form method = "POST" id = "formm" name="form1" action="recep.php" > 

<input type="text" name="nom"/> <br>

<a href="#?w=500" rel="popup_name" class="poplight" name="nom" onclick="openwindow(this)";">test</a><br>

</form>

最后是我的 script.js :

$(document).ready(function() {

$('a.poplight[href^=#]').click(function() {
    var popID = $(this).attr('rel'); //Trouver la pop-up correspondante
    var popURL = $(this).attr('href'); //Retrouver la largeur dans le href

    //Récupérer les variables depuis le lien
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //La première valeur du lien

    $('#' + popID).fadeIn().css({
        'width': Number(popWidth)
    })
    .prepend('');

    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });

    $('body').append(''); 
    //Apparition du fond - .css({'filter' : 'alpha(opacity=80)'}) 
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();

    return false;
});

$('a.close, #fade').live('click', function() { //Au clic sur le bouton ou sur le calque...
    $('#fade , .popup_block').fadeOut(function() {
        $('#fade, a.close').remove(); 
    });
    return false;
}); 

});
});

recep.php

<div id="popup_name" class="popup_block">
    <h2>Developpez.com</h2>
    <p>Soh Tanaka est traduit sur dev.com.<?php  if (isset($_POST['nom'])) { $nom = $_POST['nom']; echo 'la var est'.$nom; } ?></p>
</div>

我认为我必须向我的 script.js 添加 submit() 条件,因为当我单击我的链接时,Jquery 仅选择 href,但他不提交表单。

有什么想法吗?? :(

谢谢!!!

最佳答案

它看起来试图发送一个表单,但你没有提交它,你试图在你的 recep.php 上获得的值位于一个没有被发送的 anchor 上!此外,输入和 anchor 都具有 name="nom"。单击 anchor 时,您还会调用两个函数。所以有很多事情要分开。

首先,您应该决定是使用 jQuery、javascript 还是两者都使用来调用您需要的任何函数。 如果您使用的是 Jquery,则需要一种在页面加载时调用元素的方法,方法是使用

$('.whateverClass').click(function() { etc });

在这种情况下,我会使用 class 或 id 属性。 如果您使用的是 onclick,您将调用一个 javascript 函数。在这种情况下,您应该将所需的所有参数作为数组传递

onclick="callTheFunction(this , '{put an array here with what you want?}')"

所以这个 anchor 将位于表单的底部,但不会成为表单的一部分。没关系,因为您不想提交表单...您想从表单中获取所有参数,然后使用 jQuery 或 javascript hhtp post/get 请求将值 ajax 到服务器。

因为你想使用 jQuery,所以做一些类似的事情

<input type="text" name="nameField" id="nameInput" /> <br>
<a href="#?w=500" rel="popup_name" id="popLight">test</a> <br>

<script>
$(document).ready(function() {

    $('.popLight').click(function() {

        //Get the form input value
        var nameInput = $('#nameInput').val(); 

        //Get the anchor attributes
        var href = $('#popLight').attr('href');

        //Do your stuff with the attr parameters, if you really need this here
        //...

        //Send your stuff to the server
        $.post("recep.php", { pars: "Your pars here"},
               function(receivedData) {
               //open whatever you want with the html from the server
                $('#whateverId').html('receivedData');
        });
    });
});
</script>

要使用表单来检索值,只需标识表单并从函数中调用它 var $input = $('#form :input').val();

关于php - 将 Post var 发送到 Jquery 模式弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12851364/

相关文章:

jquery - 具有可调整大小的元素的可调整大小的 jQuery UI 小部件

javascript - 不可知的方式来匹配和包装 body 中的所有字符串(无需知道 html 结构)

javascript - 使用 jcrop 裁剪多图像

javascript - HighCharts Linechart MySQL/PHP 多行数据

php - 如何在 php 样式表中使用 @import?

javascript - 窗口绑定(bind)在 Firefox 中不起作用

jquery - 移动响应时隐藏表的表头

javascript - 如何在知道确切的 'th' 文本的情况下获取正确的元素

html - 将水平制表符转换为垂直制表符(仅限 HTML 和 CSS,无 js)

php - 如何将yii中的输入保存到数据库