首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问php中的JavaScript变量值以存储到mysql中。

访问php中的JavaScript变量值以存储到mysql中。
EN

Stack Overflow用户
提问于 2013-05-29 13:27:46
回答 3查看 5.3K关注 0票数 0

很简单,当我使用elrte时,我试图在php中访问javascript中的变量,bleow是我的index.php文件。

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>One textarea with elRTE and file upload plus one text field with elFinder</title>
<!-- jQuery and jQuery UI -->
<script src="js/jquery-1.4.4.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery-ui-1.8.7.custom.min.js" type="text/javascript" charset="utf- 8"></script>
<link rel="stylesheet" href="css/smoothness/jquery-ui-1.8.7.custom.css" type="text/css" media="screen" charset="utf-8">
<!-- elRTE -->
<script src="js/elrte.min.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="css/elrte.min.css" type="text/css" media="screen"   charset="utf-8">
<link rel="stylesheet" href="css/elrte.full.css" type="text/css" media="screen" charset="utf-8">
<!-- elFinder -->
<link rel="stylesheet" href="css/elfinder.css" type="text/css" media="screen" charset="utf-8" /> 
<script src="js/elfinder.full.js" type="text/javascript" charset="utf-8"></script>
<!-- elRTE and elFinder translation messages -->
<!--<script src="js/i18n/elrte.ru.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/i18n/elfinder.ru.js" type="text/javascript" charset="utf-8"></script>-->
<script type="text/javascript" charset="utf-8">

        // elRTE with elFinder on a textarea
        $().ready(function() {
            var opts = {
                cssClass : 'el-rte',
                lang     : 'en',  // Set your language
                allowSource : 1,
                height   : 450,
                toolbar  : 'maxi',   // 'tiny', 'compact', 'normal', 'complete', 'maxi', or 'custom' (see advanced documentation for 'custom')
                cssfiles : ['css/elrte-inner.css'],
                fmAllow  : 1,
                fmOpen : function(callback) {
                    $('<div id="myelfinder" />').elfinder({
                        url : 'connectors/php/connector.php',
                        places : '',
                        lang : 'en',    // Set your language
                        dialog : { width : 900, modal : true, title : 'Files' }, // Open in dialog window
                        closeOnEditorCallback : true, // Close after file select
                        editorCallback : callback     // Pass callback to file manager
                    })
                }

            }
            $('#editor').elrte(opts);
        // Text field with elFinder
            var opt = {
                url : 'connectors/php/connector.php',
                places : '',
                lang : 'en',
                editorCallback : function(url) {document.getElementById('field').value=url;},       // The id of the field we want elfinder to return a value to.
                closeOnEditorCallback : true,
                docked : false,
                dialog : { title : 'File Manager', height: 500 },
            }

            $('#open').click(function() {                   // The id of the button that opens elfinder
                $('#finder').elfinder(opt)                  // The id of the div that elfinder will open in
                $('#finder').elfinder($(this).attr('id'));  // it also has to be entered here.
            })

        $('#btnsub').click(function() {
        var content = $('#editor').elrte('val');
        });
        })
    })
    </script>
</head>
<body>
<?php
    $q=mysql_query("select * from aw_about_us")or die(mysql_error());
    $r=mysql_fetch_array($q);
    extract($r);
?>
<div id="finder"></div>
<table cellpadding="5" cellspacing="5" border="0" width="100%">
<form name="feedback" id="frm" method="post">
<tr>
  <td>Title : </td>
  <td><input type="text" id="atitle" size="75" value="<?=$abt_title?>"></td>
</tr>
<tr>
  <td>Tag Line : </td>
  <td><input type="text" id="atag" size="75" value="<?=$abt_small_line?>"></td>
</tr>
<tr>
  <td colspan="2"><textarea id="editor" id="acontent" cols="50" rows="4">
    <?=$abt_content?>
    </textarea></td>
</tr>
<!--<input type="text" id="field" name="field" size="60"/>&nbsp;-->
<!--<input type="button" id="open" value="Browse..." /><br>-->
<tr>
  <td><input type="submit" id="btnsub" value="Submit"></td>
</tr>
  </form>
</table> 
<?php
    /*echo $_GET['val'];
    if(isset($_POST['updabt']))
    {
        extract($_POST);
        $q1=mysql_query("update aw_about_us set abt_title='$atitle', abt_small_line='$atag', abt_content=''") or die(mysql_error());
        if($q1==true)
        {
        ?><script>alert("Page Updated Successfully!!");</script><?php
        }
        else
        {
        ?><script>alert("Page Not Updated!!");</script><?php
        }
    }
*/?>
</body>
</html>

我可以在javascript变量中获得elrte的值,但是现在我想将这个值存储在mysql数据库中,因为我正在使用PHP,所以我想在php中访问这个值,这样我就可以将它存储在数据库中,我尝试使用window.open("abc.php?val="+content);但是这个值非常大,所以get方法在这里是不能接受的,所以有什么方法可以在php中获得这个值吗?或者有其他的替代方法吗?

**编辑:**

现在,在进行以下更改后,它为我提供了content变量的值,但我想要所有3个变量,但无法获取

代码语言:javascript
复制
            $('#btnsub').click(function() {
        var content = $('#editor').elrte('val');
        var title = document.getElementById('atitle').val;
        var tag = document.getElementById('atag').val;
        alert('title'+title);
        $.ajax({
          type: "POST",
          url: 'abc.php',
          data: {title : title, tag : tag, content : content},
          success: function(html) { $('result').append(html); },
          dataType: 'html'
        }).done(function( msg ) {
alert( "Data Saved: " + msg );
});

和php文件

代码语言:javascript
复制
<?php
include('inc/conn.php');
if(isset($_POST['updabt']))
{
    $cont=$_POST['updabt'];
    $q1=mysql_query("update aw_about_us set abt_title='$title', abt_small_line='$tag', abt_content='$cont'") or die(mysql_error());
    if($q1==true)
    {
    ?><script>alert("Page Updated Successfully!!");</script><?php
    }
    else
    {
    ?><script>alert("Page Not Updated!!");</script><?php
    }
}
?>

现在如何获得所有这三个变量?

EN

回答 3

Stack Overflow用户

发布于 2013-05-29 13:30:02

您需要使用POST请求将值提交到PHP脚本。你可以通过Ajax请求做到这一点,我相信jQuery有内置的跨浏览器的Ajax方法。

票数 1
EN

Stack Overflow用户

发布于 2013-05-29 13:34:15

你需要两个页面,一个将发送AJAX (你已经有了),另一个将响应(如下):

代码语言:javascript
复制
<?php
///ajax.php
    if(isset($_POST['updabt']))
    {
        extract($_POST);
        $q1=mysql_query("update aw_about_us set abt_title='$atitle', abt_small_line='$atag', abt_content=''") or die(mysql_error());
        if($q1==true)
        {
        ?><script>alert("Page Updated Successfully!!");</script><?php
        }
        else
        {
        ?><script>alert("Page Not Updated!!");</script><?php
        }
    }
?>

在javascript中创建AJAX帖子

代码语言:javascript
复制
data['updabt'] = '';

$.ajax({
  type: "POST",
  url: 'ajax.php',
  data: data,
  success: function(html) { $('result').append(html); },
  dataType: 'html'
});
票数 0
EN

Stack Overflow用户

发布于 2013-05-29 13:36:04

您可以使用AJAX (对于像jQuery这样的库是最简单的)将变量提交给另一个PHP脚本,该脚本将值INSERT到您的数据库。

从阅读下面的内容开始。

jQuery.ajax

jQuery.post

一旦理解了AJAX的微妙之处,这实际上就非常简单了。

这里有一个简单的示例,可以让您开始运行。假设您想要将某些内容记录到PHP错误日志中...

这将是我的JavaScript函数:

代码语言:javascript
复制
var log = function(val) {
    $.post("ajax.php", {"mode": 'log', "val": val}, function() {});
}

ajax.php应该是一个函数的集合,理想情况下是一个类...

代码语言:javascript
复制
public function __construct() {
    $arrArgs = empty($_GET)?$_POST:$_GET;

    /**
     * using 'mode' I can send the AJAX request to the right method,
     * and therefore have any number of calls using the same class
     */
    if (array_key_exists('mode', $arrArgs)) {
    $strMethod = $arrArgs['mode'];

    unset($arrArgs['mode']);
    $this->$strMethod($arrArgs);
    }
}

protected function log($arrArgs) {
    error_log($arrArgs['val']);
}

同样的想法也可以很容易地用于将数据写入数据库。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16806126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档