dongyin8991 2010-01-06 19:55
浏览 86
已采纳

使用PHP / jQuery UI将额外数据值发送到ajax.php文件

I am trying to send an extra $php_var with jQuery to an Ajax post function. In the 1st $.ajax I have:

    $('.ui-icon-closethick').click(function(e) {
    e.preventDefault();
    var parent = $(this).parent().parent();
    $.ajax({
        type: "POST",
        url: "server_items_reorder.php",
        data: 'id=' + parent.attr('id'),

        success: function() {
            parent.slideUp(300,function() {
                parent.remove();
            });
        }
    });
});

The data: 'id=' + parent.attr('id') needs a extra $php_var sent to the URL: server_items_reorder.php

A little further down in my code I have:

function savelayout(){
var positions = "";
var weight = 0;
var wb_name = "$wb_name";
$(".portlet").each(function(){weight++;positions+=(this.id + "=" + this.parentNode.id + "|" + weight + "&");});

$.ajax({
type: "POST",
url: "server_items_reorder.php",
data: positions
});
}

As you can see, I already tried there to add a $var called wb_name.

Full code here:

/************************************************************************/
/* WB_BlocksManager v1.0                                                */
/* Module for phpnuke 6.x and 7.x by Paulo FERREIRA                     */
/* Copyright (C) 2003 Paulo Ferreira                                    */
/* Web:   http://www.phpnuke-belgique.org/                              */
/* Email: webmaster@phpnuke-belgique.org                                */
/* =====================================================================*/
/* James Johnston                                                       */
/* http://www.techknowpro.com                                           */
/* =====================================================================*/
/* nono                                                                 */
/* http://osc2nuke.org/                                                 */
/* =====================================================================*/
/* PHP-NUKE: Web Portal System                                          */
/* =====================================================================*/
/*                                                                      */
/* Copyright (c) 2002 by Francisco Burzi                                */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

if (!defined('ADMIN_FILE')) {
    die('Access Denied');
}
global $prefix, $db, $admin_file, $wb_name;
$aid = substr($aid, 0, 25);
$row = $db->sql_fetchrow($db->sql_query('SELECT radminsuper FROM ' . $prefix . '_authors WHERE aid=\'' . $aid . '\''));
if ($row['radminsuper'] == 1) {

/*********************************************************/
/* Blocks_Manager Functions                              */
/*********************************************************/

function BlocksManager($wb_name) {
    global $db, $prefix, $currentlang, $multilingual, $bgcolor2, $admin_file;
    OpenHeader(_BLOCKMGR);
    $wb_url="".$admin_file.".php?op=BlocksManager&wb_name=";
    echo "<center><form action=\"".$admin_file.".php\" method=\"post\">
"
        ._BLOCKMGR_MODULEDROP."&nbsp;<select name=\"wb_name\" size=\"1\" onChange=\"top.location.href=this.options[this.selectedIndex].value\">
";
    if (!isset($wb_name)) { $wb_name = "admin"; }
    echo "<option value=\"".$wb_url."admin\""; if ($wb_name=="admin") { echo " selected"; } echo ">Admin</option>
";
    $result = $db->sql_query("SELECT mid, title, custom_title, active, view, inmenu FROM ".$prefix."_modules WHERE active=1 ORDER BY title ASC");
    while(list($mid, $title, $custom_title, $active, $view, $inmenu) = $db->sql_fetchrow($result)) {
        echo "<option value=\"".$wb_url.$title."\""; if ($wb_name==$title) { echo " selected"; } echo ">$custom_title</option>
";
        if (!isset($wb_name)) { $wb_name = $title; }
    }
    echo "</select>
"
        ."</form>
</center>
";






    echo "<br /><br />";

    echo "<center><b>"._BLOCKMGR_ADDNEWBLOCK."</b></center>
"
        ."<form name=\"BM_EDIT\" action=\"".$admin_file.".php\" method=\"post\">
"
        ."<table width=\"80%\" align=\"center\">
"
        ."<tr>
";
    //List all inactive Blocks for selected module
    echo "<td align=\"center\" valign=\"top\">
"
        ._BLOCKMGR_ACTIVE_BLOCKS."<br>
"
        ."<select name=\"bida[]\" size=\"10\" multiple>
";
    $sql = "SELECT bid, title FROM ".$prefix."_blocks ORDER BY title ASC";
    $result = $db->sql_query($sql);
    while (list($bid, $title) = $db->sql_fetchrow($result)) {
        $ii = 0; $wb_affiche = 1;
        while ($ii < $i) { if ($wb_tabblocks[$ii] == $bid) { $wb_affiche = 0; } $ii++; }
        if ($wb_affiche == 1) { echo "<option value=\"".$bid."\">$title</option>
"; }
    }
    echo "</select><br>
"
        ."<input type=\"submit\" value=\""._BLOCKMGR_ADD_BLOCK."\" onclick=\"document.BM_EDIT.op.value='BlocksManager_Add';\">
"
        ."</td>
";

    //List all active Modules
    echo "<td align=\"center\" valign=\"top\">"
         .""._BLOCKMGR_MODULES."<br><select name=\"title[]\" size=\"10\" multiple>
";
    echo "<option value=\"admin\""; if ($wb_name=="admin") { echo " selected"; } echo ">Admin</option>
";
    $sql = "SELECT mid, title, custom_title FROM ".$prefix."_modules WHERE active=1 ORDER BY title ASC";
    $result = $db->sql_query($sql);
    while(list($mid, $title, $custom_title) = $db->sql_fetchrow($result)) {
        echo "<option value=\"$title\""; 
        if ($wb_name==$title) { echo " selected"; } 
        echo ">$custom_title</option>
";
        if (!isset($wb_name)) { $wb_name = $title; }
    }
    echo "</select><br>"
        ."</td>
";

    //List all active Blocks for selected module
    echo "<td align=\"center\" valign=\"top\">
"
        ._BLOCKMGR_INACTIVE_BLOCKS."<br>
"
        ."<select name=\"bidr[]\" size=\"10\" multiple>
";
    $sql = "SELECT b.bid, b.title FROM ".$prefix."_blocks b, ".$prefix."_blocks_manager m WHERE b.bid=m.bid AND m.title='$wb_name' ORDER BY title ASC";
    $result = $db->sql_query($sql);
    while(list($bid, $title) = $db->sql_fetchrow($result)) {
        echo "<option value=\"$bid\">$title</option>
";
    }
    echo "</select><br>
"
        ."<input type=\"submit\" value=\""._BLOCKMGR_REMOVE_BLOCK."\" onclick=\"document.BM_EDIT.op.value='BlocksManager_Remove';\">
"
        ."</td>
"
        ."</tr>
"
        ."</table>
"
        ."<input type=\"hidden\" name=\"wb_name\" value=\"$wb_name\">
"
        ."<input type=\"hidden\" name=\"op\" value=\"BlocksManager_Add\">
"
        ."</form>
";

?>








<style type="text/css">
        body { font-size: 62.5%; }
        label, input { display:block; }
        input.text { margin-bottom:12px; width:95%; padding: .4em; }
        fieldset { padding:0; border:0; margin-top:25px; }
        h1 { font-size: 1.2em; margin: .6em 0; }
        div#users-contain {  width: 350px; margin: 20px 0; }
        div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
        div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
        .ui-button { outline: 0; margin:0; padding: .4em 1em .5em; text-decoration:none;  !important; cursor:pointer; position: relative; text-align: center; }
        .ui-dialog .ui-state-highlight, .ui-dialog .ui-state-error { padding: .3em;  }


    .column { width: 170px; float: left; padding-bottom: 100px; }
    .portlet { margin: 0 1em 1em 0; }
    .portlet-header { margin: 0.3em; padding-bottom: 4px; padding-left: 0.2em; }
    .portlet-header .ui-icon { float: right; }
    .portlet-content { padding: 0.4em; }
    .ui-sortable-placeholder { border: 1px dotted black; visibility: visible !important; height: 50px !important; }
    .ui-sortable-placeholder * { visibility: hidden; }

    </style>
    <script type="text/javascript">




$(function() {
        $(".column").sortable({
            connectWith: '.column',
            update: savelayout
        });

        $(".portlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all delete")

            .find(".portlet-header")
                .addClass("ui-widget-header ui-corner-all")
                .prepend('<span class="ui-icon ui-icon-wrench"></span>')
                .prepend('<span class="ui-icon ui-icon-plusthick"></span>')
                .prepend('<span class="ui-icon ui-icon-closethick"></span>')
            .end()
            .find(".portlet-content").toggle();

      $(".portlet-header .ui-icon-plusthick").toggle(function() {
        $(this).removeClass("ui-icon-plusthick");
        $(this).addClass("ui-icon-minusthick");
        $(this).parents(".portlet:first").find(".portlet-content").toggle();
    }, function() {
        $(this).removeClass("ui-icon-minusthick");
        $(this).addClass("ui-icon-plusthick");
        $(this).parents(".portlet:first").find(".portlet-content").toggle();
    }





    );

    $('.ui-icon-closethick').click(function(e) {
        e.preventDefault();
        var parent = $(this).parent().parent();
        $.ajax({
            type: "POST",
            url: "server_items_reorder.php",
            data: 'id=' + parent.attr('id'),

            success: function() {
                parent.slideUp(300,function() {
                    parent.remove();
                });
            }
        });
    });
    $(".column").disableSelection();
    });
    </script>

<div class="column" id="l">
<?php 
    $sql = "SELECT b.bid, b.bkey, b.title, b.url, m.bposition, m.weight, b.active, b.blanguage, b.blockfile, b.view FROM ".$prefix."_blocks b, ".$prefix."_blocks_manager m WHERE b.bid=m.bid AND m.title='$wb_name' AND m.bposition = 'l' ORDER BY weight";

$result = $db->sql_query($sql);
    while(list($bid, $bkey, $title, $url, $bposition, $weight, $active, $blanguage, $blockfile, $view) = $db->sql_fetchrow($result)) {

?>
    <div class="portlet" id="<?php echo $bid; ?>">
        <div class="portlet-header" id="<?php echo $bid; ?>"><?php echo $title; ?></div>
        <div class="portlet-content"><?php echo $title . '---(' . $view . ')'; ?></div>
    </div>
<?php
}
?>  

</div>

<div class="column" id="c">
<?php  
    $sql = "SELECT b.bid, b.bkey, b.title, b.url, m.bposition, m.weight, b.active, b.blanguage, b.blockfile, b.view FROM ".$prefix."_blocks b, ".$prefix."_blocks_manager m WHERE b.bid=m.bid AND m.title='$wb_name' AND m.bposition = 'c' ORDER BY weight";

$result = $db->sql_query($sql);
    while(list($bid, $bkey, $title, $url, $bposition, $weight, $active, $blanguage, $blockfile, $view) = $db->sql_fetchrow($result)) {

?>
    <div class="portlet" id="<?php echo $bid; ?>">
        <div class="portlet-header"><?php echo $title; ?></div>
        <div class="portlet-content"><?php echo $title . '---(' . $bposition . '---' . $wb_name .')'; ?></div>
    </div>
<?php
}
?>  


</div>

<div class="column" id="d">
<?php  
    $sql = "SELECT b.bid, b.bkey, b.title, b.url, m.bposition, m.weight, b.active, b.blanguage, b.blockfile, b.view FROM ".$prefix."_blocks b, ".$prefix."_blocks_manager m WHERE b.bid=m.bid AND m.title='$wb_name' AND m.bposition = 'd' ORDER BY weight";

$result = $db->sql_query($sql);
    while(list($bid, $bkey, $title, $url, $bposition, $weight, $active, $blanguage, $blockfile, $view) = $db->sql_fetchrow($result)) {

?>
    <div class="portlet" id="<?php echo $bid; ?>">
        <div class="portlet-header"><?php echo $title; ?></div>
        <div class="portlet-content"><?php echo $title . '---(' . $bposition . ')'; ?></div>
    </div>
<?php
}
?>  


</div>  

<div class="column" id="r">
<?php  
    $sql = "SELECT b.bid, b.bkey, b.title, b.url, m.bposition, m.weight, b.active, b.blanguage, b.blockfile, b.view FROM ".$prefix."_blocks b, ".$prefix."_blocks_manager m WHERE b.bid=m.bid AND m.title='$wb_name' AND m.bposition = 'r' ORDER BY weight";

$result = $db->sql_query($sql);
    while(list($bid, $bkey, $title, $url, $bposition, $weight, $active, $blanguage, $blockfile, $view) = $db->sql_fetchrow($result)) {

?>
    <div class="portlet" id="<?php echo $bid; ?>">
        <div class="portlet-header"><?php echo $title; ?></div>
        <div class="portlet-content"><?php echo $title . '---(' . $bposition . ')'; ?></div>
    </div>
<?php
}
?>  


</div>  
<script type="text/javascript">
function savelayout(){
var positions = "";
var weight = 0;
var wb_name = "$wb_name";
$(".portlet").each(function(){weight++;positions+=(this.id + "=" + this.parentNode.id + "|" + weight + "&");});

$.ajax({
type: "POST",
url: "server_items_reorder.php",
data: positions
});
}
</script>


<?php   


    CloseFooter();
}

function BlocksManager_Add($bid, $title) {
    global $db, $prefix;

    foreach($title as $tKey => $tValue) {
        $sql = "SELECT MAX(weight) FROM ".$prefix."_blocks_manager WHERE title='$tValue'";
        $result = $db->sql_query($sql);
        list($weight, $bposition) = $db->sql_fetchrow($result);
        foreach($bid as $bKey => $bValue) {
            $weight++;
            $db->sql_query("INSERT INTO ".$prefix."_blocks_manager VALUES ($bValue, '$tValue', 'l', $weight)");
        }
        BlocksManager_FixWeight($tValue);
    }
}

function BlocksManager_Remove($bid, $title) {
    global $db, $prefix;

    foreach($title as $tKey => $tValue) {
        foreach($bid as $bKey => $bValue) {
            $db->sql_query("DELETE FROM ".$prefix."_blocks_manager WHERE bid='$bValue' AND title='$tValue'");
        }
        BlocksManager_FixWeight($tValue);
    }
}

function BlocksManager_FixWeight($wb_name) {
    global $db, $prefix;
    $position[] = 'l';
    $position[] = 'r';
    $position[] = 'c';
    $position[] = 'd';
    $position[] = '';
    foreach($position as $pKey => $pValue) {
        $result = $db->sql_query("SELECT bid FROM ".$prefix."_blocks_manager WHERE title='$wb_name' AND bposition='$pValue' ORDER BY weight ASC");
        $weight = 0;
        while(list($bid) = $db->sql_fetchrow($result)) {
            $weight++;
            if ($pValue != '') : $db->sql_query("UPDATE ".$prefix."_blocks_manager SET weight='$weight' WHERE title='$wb_name' AND bid='$bid'");
            else : $db->sql_query("UPDATE ".$prefix."_blocks_manager SET weight='$weight', bposition='l' WHERE title='$wb_name' AND bid='$bid'<br>");
            endif;
        }
    }
}

function BlocksManager_BlockPosition($bid, $wb_name, $position) {
    global $db, $prefix;
    $db->sql_query("UPDATE ".$prefix."_blocks_manager SET bposition='$position' WHERE bid='$bid' AND title='$wb_name'");
    BlocksManager_FixWeight($wb_name);
}

function BlocksManager_BlockOrder($wb_name, $weightrep, $weight, $bidrep, $bidori) {
    global $db, $prefix;
    $result = $db->sql_query("UPDATE ".$prefix."_blocks_manager SET weight='$weight' WHERE bid='$bidrep'");
    $result2 = $db->sql_query("UPDATE ".$prefix."_blocks_manager SET weight='$weightrep' WHERE bid='$bidori'");
    BlocksManager_FixWeight($wb_name);
}

function OpenHeader($title="") {
    include("header.php");
    GraphicAdmin();
    title($title);
    OpenTable();
}

function CloseFooter() {
    CloseTable();
    include("footer.php");
}

switch($op) {

    case "BlocksManager":
    BlocksManager($wb_name);
    break;

    case "BlocksManager_Add":
    BlocksManager_Add($bida, $title);
    Header("Location: ".$admin_file.".php?op=BlocksManager&wb_name=$wb_name");
    break;

    case "BlocksManager_Remove":
    BlocksManager_Remove($bidr, $title);
    Header("Location: ".$admin_file.".php?op=BlocksManager&wb_name=$wb_name");
    break;

    case "BlocksManager_BlockPosition":
    BlocksManager_BlockPosition($bid, $title, $position);
    Header("Location: ".$admin_file.".php?op=BlocksManager&wb_name=$title");
    break;

    case "BlocksManager_FixWeight":
    BlocksManager_FixWeight($wb_name);
    Header("Location: ".$admin_file.".php?op=BlocksManager&wb_name=$wb_name");
    break;

    case "BlocksManager_BlockOrder":
    BlocksManager_BlockOrder ($title, $weightrep, $weight, $bidrep, $bidori);
    Header("Location: ".$admin_file.".php?op=BlocksManager&wb_name=$title");
    break;  
}

} else {
    echo "Access Denied";
}

The purpose of the code is to control a block system by modules. You can move/add/change/delete blocks by its module.

  • 写回答

2条回答 默认 最新

  • dongshenling6585 2010-01-06 20:06
    关注

    You need to mix your PHP inline with your AJAX request:

    $('.ui-icon-closethick').click(function(e) {
        e.preventDefault();
        var parent = $(this).parent().parent();
        $.ajax({
            type: "POST",
            url: "server_items_reorder.php",
            data: 'id=' + parent.attr('id') + '&php_var=<?php echo $php_var; ?>',
            success: function() {
                parent.slideUp(300,function() {
                    parent.remove();
                });
            }
        });
    });
    

    And for your second example:

    function savelayout(){
        var positions = "";
        var weight = 0;
        var wb_name = "<?php echo $wb_name; ?>";
        $(".portlet").each(function(){
            weight++;
            positions += (this.id + "=" + this.parentNode.id + "|" + weight + "&");
        });
    
        $.ajax({
            type: "POST",
            url: "server_items_reorder.php",
            data: positions
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。