Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问php中的JavaScript变量值以存储在mysql中_Php_Javascript_Mysql_Ajax_Elrte - Fatal编程技术网

访问php中的JavaScript变量值以存储在mysql中

访问php中的JavaScript变量值以存储在mysql中,php,javascript,mysql,ajax,elrte,Php,Javascript,Mysql,Ajax,Elrte,很简单,, 在使用elrte时,我试图在php中访问javascript中的变量, bleow是我的index.php文件 <!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 p

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

<!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>
和php文件

<?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
    }
}
?>

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

您需要两个页面,一个页面将发送Ajax(您拥有的),另一个页面将响应(如下所示):

您可以使用AJAX(使用jQuery这样的库最简单)将变量提交到另一个PHP脚本,该脚本将
插入到数据库中

从阅读以下内容开始

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

下面是一个简单的例子,让你下车跑步。假设您想在PHP错误日志中记录一些内容

这将是我的JavaScript函数:

var log = function(val) {
    $.post("ajax.php", {"mode": 'log', "val": val}, function() {});
}
ajax.php
将是一组函数,理想情况下是一个类

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']);
}

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

这不是一个非常有用的答案。用“相信”这个词告诉我你不确定答案。是的,jQuery有许多AJAX函数,可以满足提问者的需求。但你还没解释怎么做我解释了怎么。。。我只是没有给出密码。谷歌搜索“Ajax POST request jQuery”在第一页给出了一个清晰易读的示例:我提供的链接给出了一个很好的示例。您还可以查看jQuery的$.post()函数。它简化了从JavaScript向PHP脚本提供数据的过程。在开始执行脚本之前,它会自动从$\u POST和$\u GET数组中创建“$varname”变量。但是,出于安全目的,它通常是禁用的。更好的做法是使用“$\u POST['varname']”或“$\u GET['varname']”引用所有变量,这样您就不必依赖正在打开的功能。我不同意询问者需要两个“页面”。通过巧妙地使用params,只需一个脚本即可完成。您还发送了一个空字符串(我不知道是为了什么目的),而
成功
操作没有任何意义。php
ajax.php
不响应任何内容。
ajax.php
中的JavaScript将无法工作,因为脚本不是在浏览器中处理的,而是在后端处理的单独线程中处理的。@GordonFreeman首先需要工作,然后进行优化。OP不够熟练,无法理解一页中的所有“技巧”。我添加了$('btnsub')。单击(function(){var content=$('#editor').elrte('val');var title=document.getElementById('atitle').val;var tag=document.getElementById('atag').val;$.ajax({type:'POST',url:'abc.php',data:data,success:success,dataType:'html'});})但仍然不起作用…如果调用自己的页面,则无法显示结果(因为结果都是新页面),或者需要禁用在设置时返回任何内容($\u POST['updabt'])若我不能得到结果,但数据也并没有添加到数据库中,那个么这是可以的
data['updabt'] = '';

$.ajax({
  type: "POST",
  url: 'ajax.php',
  data: data,
  success: function(html) { $('result').append(html); },
  dataType: 'html'
});
var log = function(val) {
    $.post("ajax.php", {"mode": 'log', "val": val}, function() {});
}
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']);
}