• xajax无刷新上传实例


    http://www.corange.cn/archives/2008/10/2080.html

    主要的xajax结合php上传文件和保存数据的函数集中存放在 ./admin/includes/function.xajax.php 里面
    在language.php文件载入并初始化xajax上传扩展
    require_once(ROOT_PATH . '/includes/xajax/xajax.inc.php');

    require_once(ROOT_PATH . '/admin/includes/function.xajax.php');

    require_once(ROOT_PATH . '/includes/xajax/xajaxExtend.php');

    //$xajax = new xajax();

    $xajax = new xajaxExtend;

    //$xajax->debugOn();

    $xajax->registerFunction("ShowCreateLanguage");

    $xajax->registerFunction("CreateLanguage");

    $xajax->registerFunction("DeleteLanguage");

    $xajax->registerFunction("ModifyLanguage");

    $xajax->registerFunction("UpdateLanguage");

    $xajax->processRequests();

    主要处理上传函数
    <?php

    //显示添加语言表单

    function ShowCreateLanguage($hidden = 1) {

    $objResponse = new XajaxResponse();

    if($hidden == 1){

    $objResponse->addAssign('createlanguage','innerHTML',"<a href=/"/" onclick=/"xajax_ShowCreateLanguage(2);return false/" title=/"/">create language</a>");

    $objResponse->addAssign('h','style.display','block');

    }else{

    $objResponse->addAssign('createlanguage','innerHTML',"<a href=/"/" onclick=/"xajax_ShowCreateLanguage(1);return false/" title=/"/">create language</a>");

    $objResponse->addAssign('h','style.display','none');

    }

    return $objResponse;

    }



    function CreateLanguage($f1) {

    global $db,$tablefix,$LANG;

    $objResponse = new xajaxResponse();

    $name = xml_escape($f1['name']);

    $charset = xml_escape($f1['charset']);

    $ord = xml_escape($f1['ord']);

    $ord = (($ord == '') OR !is_numeric($ord)) ? '1' : $ord;



    if(empty($name)){

    $objResponse->addAlert("Language name is empty");

    }



    if(empty($charset)){

    $objResponse->addAlert("charset is empty");

    }



    if(!empty($name) AND !empty($charset)){

    //判断是否有上传文件

    if($_FILES['filename']['tmp_name'] <>""){

    require_once(ROOT_PATH . '/includes/class.upload.php');



    $fileFormat = array('gif','jpg','jpge','png');

    $upload = new Upload( ROOT_PATH . '/attaches/', $fileFormat);

    if(!$upload->run('filename')){

    $objResponse->addAlert("upload image file false");

    }else{

    //如果成功上传则添加进数据库

    $filename = $upload->returnArray[0][saveName];

    $insertsql = "INSERT INTO `language` (`languageid`,`name`,`filename`,`charset`,`ord`) VALUES (NULL,'$name','$filename','$charset','$ord')";

    if($db->query($insertsql)){

    //如果添加成功

    $languageid = $db->insertid();

    $innerHTML = <<<EOT

    <ul id="page_" class="two">

    <li>$languageid</li>

    <li>$name</li>

    <li><img src="../attaches/" width="21" /></li>

    <li>$charset</li>

    <li>$ord</li>

    <li style="180px;"><a class="button1" href="" onclick="if(confirm('sure?')) xajax_DeleteLanguage($languageid);return false;" title="">$LANG[delete]</a> <a class="button1" href="" onclick="xajax_ModifyLanguage($languageid); return false;" title="">$LANG[edit]</a></li>

    </ul>

    EOT;

    $objResponse->addAppend("language","innerHTML",$innerHTML);

    }

    }

    //如果没有文件上传

    }else{

    $insertsql = "INSERT INTO `language` (`languageid`,`name`,`filename`,`charset`,`ord`) VALUES (NULL,'$name','','$charset','$ord')";

    if($db->query($insertsql)){

    //如果添加成功

    $languageid = $db->insertid();

    $innerHTML = <<<EOT

    <ul id="page_" class="two">

    <li>$languageid</li>

    <li>$name</li>

    <li>&nbsp;</li>

    <li>$charset</li>

    <li>$ord</li>

    <li style="180px;"><a class="button1" href="" onclick="if(confirm('sure?')) xajax_DeleteLanguage($languageid);return false;" title="">$LANG[delete]</a> <a class="button1" href="" onclick="xajax_ModifyLanguage($languageid); return false;" title="">$LANG[edit]</a></li>

    </ul>

    EOT;

    $objResponse->addAppend("language","innerHTML",$innerHTML);

    }

    }

    //清空表单

    $objResponse->addClear('name','value');

    $objResponse->addClear('filename','value');

    $objResponse->addClear('charset','value');

    $objResponse->addClear('ord','value');

    $objResponse->addAssign("h","style.display","none");

    }



    return $objResponse;

    }



    //此函数未完全

    function DeleteLanguage($languageid) {

    global $db,$tablefix;

    $languageid = xml_escape($languageid);

    $objResponse = new XajaxResponse();

    if(!is_numeric($languageid)){

    $objResponse->addAlert("language id is empty");

    }else{

    //取得filename,并删除图片

    $result = $db->query("SELECT `filename` FROM `language` WHERE `languageid` = '$languageid'");

    $r = $db->fetch_array($result);

    @unlink('../attaches/'.$r['filename']);



    if($db->query("DELETE FROM `language` WHERE `languageid` = '$languageid' LIMIT 1")){

    //执行删除 应该还要删除与语言相关的东西

    //$objResponse->addAlert("success delete a session");

    $objResponse->addRemove("page_".$languageid);

    }else{

    $objResponse->addAlert("failed to delete a session");

    }

    }



    return $objResponse;

    }



    function ModifyLanguage($languageid) {

    global $db,$tablefix,$LANG;

    $languageid = xml_escape($languageid);

    $objResponse = new XajaxResponse();

    if(!is_numeric($languageid)){

    $objResponse->addAlert("Language id error");

    }else{

    $result = $db->query("SELECT * FROM `language` WHERE `languageid` = '$languageid' LIMIT 1");

    $language = $db->fetch_array($result);



    if(count($language)>0){

    if($language['filename'] != ''){

    $image = "<img src=/"../attaches//" width=/"21/">";

    }else{

    $image = '';

    }

    $innerHTML = <<<EOT

    <div id="midifyinput" style="margin:0 auto;padding:0;760px;">

    <form id="f2" name="f2" onsubmit="return updateForm();" method="post" enctype="multipart/form-data">

    <input type="hidden" name="languageid" value="" />

    <table id="addlanguage" class="th">

    <th>Language Name</th>

    <th>Icon(21px * 14px)</th>

    <th>Charset</th>

    <th>Order</th>

    <th width="150">Control</th>

    <tr>

    <td><input type="text" name="name" id="name" value="" /></td>

    <td>$image<input type="file" name="filename" /></td>

    <td><input type="text" name="charset" id="charset" value="" /></td>

    <td><input type="text" name="ord" id="ord" value="" /></td>

    <td><input type="submit" class="button1" name="" value="$LANG[add]" /></td>

    </tr>

    </table>

    </form>

    </div>

    EOT;

    $objResponse->addAssign("h","style.display",'');

    $objResponse->addAssign("h","innerHTML",$innerHTML);

    }else{

    $objResponse->addAlert("Language is not installed");

    }

    }

    return $objResponse;

    }



    function UpdateLanguage($f2) {

    global $db,$tablefix,$LANG;

    $objResponse = new xajaxResponse();

    $name = xml_escape($f2['name']);

    $charset = xml_escape($f2['charset']);

    $languageid = xml_escape($f2['languageid']);

    $ord = xml_escape($f2['ord']);

    $ord = (($ord == '') OR !is_numeric($ord)) ? '1' : $ord;



    if(empty($name)){

    $objResponse->addAlert("Language name is empty");

    }



    if(empty($charset)){

    //$objResponse->addAlert("charset is empty");

    }



    if(!empty($name) AND !empty($charset)){

    //判断是否有上传文件



    if($_FILES['filename']['tmp_name'] != ""){

    require_once(ROOT_PATH . '/includes/class.upload.php');



    $fileFormat = array('gif','jpg','jpge','png');

    $upload = new Upload( ROOT_PATH . '/attaches/', $fileFormat);

    if(!$upload->run('filename')){

    $objResponse->addAlert("upload image file false");

    }else{

    //如果成功上传则添加进数据库

    $filename = $upload->returnArray[0][saveName];

    //获取原来的图片路径并删除

    $result = $db->query("SELECT `filename` FROM `language` WHERE `languageid` = '$languageid' LIMIT 1");

    $image = $db->fetch_array($result);

    if(count($image)>0){

    @unlink('../attaches/'.$image['filename']);

    }



    //更新数据库

    $updatesql = "UPDATE `language` SET `name` = '$name',`charset` = '$charset',`ord` = '$ord',`filename` = '$filename' WHERE `languageid` = '$languageid' LIMIT 1";

    if($db->query($updatesql)){

    //如果添加成功

    $RooPath = ROOT_PATH;

    $innerHTML = <<<EOT

    <li>$languageid</li>

    <li>$name</li>

    <li><img src="../attaches/" width="21" /></li>

    <li>$charset</li>

    <li>$ord</li>

    <li style="180px;"><a class="button1" href="" onclick="if(confirm('sure?')) xajax_DeleteLanguage($languageid);return false;" title="">$LANG[delete]</a> <a class="button1" href="" onclick="xajax_ModifyLanguage($languageid); return false;" title="">$LANG[edit]</a></li>

    EOT;

    $objResponse->addAssign("page_","innerHTML",$innerHTML);



    }

    }

    //如果没有文件上传

    }else{

    //更新数据库

    $updatesql = "UPDATE `language` SET `name` = '$name',`charset` = '$charset',`ord` = '$ord' WHERE `languageid` = '$languageid' LIMIT 1";

    if($db->query($updatesql)){

    //如果添加成功

    $result = $db->query("SELECT `filename` FROM `language` WHERE `languageid` = '$languageid' LIMIT 1");

    $image = $db->fetch_array($result);

    if(count($image)>0){

    $filename = $image['filename'];

    }

    $innerHTML = <<<EOT

    <li>$languageid</li>

    <li>$name</li>

    <li><img src="../attaches/" width="21" /></li>

    <li>$charset</li>

    <li>$ord</li>

    <li style="180px;"><a class="button1" href="" onclick="if(confirm('sure?')) xajax_DeleteLanguage($languageid);return false;" title="">$LANG[delete]</a> <a class="button1" href="" onclick="xajax_ModifyLanguage($languageid); return false;" title="">$LANG[edit]</a></li>

    EOT;

    $objResponse->addAssign("page_","innerHTML",$innerHTML);

    }

    }

    //隐藏表单

    $objResponse->addAssign("h","style.display","none");

    }



    return $objResponse;

    }

    ?>

    在调用扩展上传,其中CreateLanguage是php中处理上传的函数 f1是表单的id
    <script language="Javascript">
    function checkForm() {
    return xajax.upload('CreateLanguage','f1');
    }

    function updateForm() {
    return xajax.upload('UpdateLanguage','f2');
    }
    </script>
    http://www.corange.cn//uploadfiles/upload_23529.rar

  • 相关阅读:
    FileReader读数据
    FileWriter写数据
    装饰者设计模式
    数据库连接池
    事务
    EL表达式
    console.log是异步的吗?
    使用i3wm时出现的若干问题的解决办法
    使用xmodmap修改键盘映射
    CentOS7 Minimal 安装后出现的若干问题解决办法
  • 原文地址:https://www.cnblogs.com/zerogo/p/2209179.html
Copyright © 2020-2023  润新知