【问题标题】:Wordpress jQuery to PHP data transferWordpress jQuery 到 PHP 数据传输
【发布时间】:2012-04-06 03:28:58
【问题描述】:

每个人。我是一个相对较新的开发人员,在 jQuery AJAX 和 PHP 方面经验有限。我正在开发一个 Wordpress 插件,这对我来说也是一个学习练习。

基本描述:在我插件的管理页面上,我有一堆表单,它们将显示为模式窗口(使用 jQuery UI),填写后,会将它们的字段提交到单独的 PHP 文件进行处理。该文件将接受数据并准备将其插入到我设置的 wpdb 表中。

插件管理页面 (PHP):

<div id="form1" title="My Awesome Form">
    <form id="frmNewCom">
        <fieldset>
        <table>
            <tr>
                <td><label for="name">Community Name</label>
                <td><input type="text" name="newComName" id="newComName" />
            </tr>
            <tr>
                <td><label for="lefthead">Left Column Header</label></td>
                <td><input type="text" name="newComLefthead" id="newComLefthead" /></td>
            </tr>
            <tr>
                <td><label for="righthead">Right Column Header</label></td>
                <td><input type="text" name="newComRighthead" id="newComRighthead" /></td>
            </tr>
        </table>
        </fieldset>
    </form>
</div>

表单的 jQuery UI 代码($pcal 是我没有冲突的东西):

$pcal('#form1').dialog({
        autoOpen: false,
        height: 275,
        width: 400,
        modal: true,
        buttons: {
            "Add Living Option": function() {
                // Functionality for submit
            },
            Cancel: function() {
                $pcal(this).dialog("close");
            }
        },
        close: function() {
            // close function
        }
    });

现在问题来了。从这一点开始,我真的不确定该怎么做。我已经阅读了很多关于使用 .post().serialize() 的内容,但此时我让自己非常困惑。

你们对适当的 javascript/jQuery 到 PHP 处理有一些了解吗?我也在 Wordpress 论坛上问过这个问题,但我总是在这里找到很好的建议,所以我想问一下。感谢您提供任何和所有帮助。

【问题讨论】:

    标签: php javascript jquery wordpress plugins


    【解决方案1】:

    一个非常简单的例子是:

    $pcal.post("test.php", $pcal("#frmNewCom").serialize(), function() {
       // this will run once returned from PHP
       alert('thanks');
       $pcal(this).dialog("close");
    });
    

    这会使用.post().serialize() 以序列化格式(newComName=1&amp;newComLefthead=2&amp;newComName=3 其中 1,2 和 3 是您在表单中输入的值)发布表单 (id = frmNewCom)

    然后在test.php 中,您可以像这样访问您的值:

    $newComName = $_POST['newComName'];
    $newComLefthead = $_POST['newComLefthead'];
    $newComName = $_POST['newComName'];
    // insert into table
    

    以上是 a) 未经测试的 b) 如果您想将其存储在数据库中,则不包含任何针对 SQL injection 的预防措施

    【讨论】:

    • 太棒了。实际上,这正是我现在所拥有的。现在我遇到了 wpdb->query() 方法抛出错误的问题,但这是一个完全不同的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 2015-05-18
    • 2011-03-05
    • 2015-02-16
    • 1970-01-01
    • 2014-03-06
    相关资源
    最近更新 更多