0

Just simple, I am trying to access the variable in javascript inside the php while working with elrte, bleow is my index.php file

<!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>

I am able to get the value of elrte inside the javascript variable, but now I wanted store this value inside the mysql database, as I am using PHP I want to access this value inside a php so that I can store it in database, I tried using window.open("abc.php?val="+content); but the value is very large so get method cannot be acceptable here, so is there any way to get this value inside the php? or any alternate way to do this?

** Edit :**

Now it gives me a value of content variable after making following changes, but I want all 3 variables, but unable to get

            $('#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 );
});

and php file

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

Now how to get all three variables??

Darpan Kulkarni
  • 1,362
  • 1
  • 17
  • 36

3 Answers3

1

You'll need to submit the value to your PHP script using a POST request to your server. You can do this with Ajax requests, and I believe jQuery has built-in methods for Ajax which are cross-browser.

  • This is not a terribly useful answer. Use of the word 'believe' tells me you're not sure of the answer. Yes, jQuery has a number of AJAX functions that will do what the asker wants. But you haven't explained how. – Ian Atkin May 29 '13 at 05:33
  • I did explain how... I just didn't give code. Googling "Ajax POST request jQuery" gives the following result with a clean easy-to-read example on the first page: http://stackoverflow.com/questions/5004233/jquery-ajax-post-example – Mike Gallagher May 29 '13 at 05:54
  • The link I supplied gave a good example. You can also look into jQuery's $.post() function. It simplifies giving data to a PHP script from JavaScript. – Mike Gallagher May 29 '13 at 06:30
  • There is a PHP configuration setting called 'register_globals'. It automatically creates '$varname' variables from the $_POST and $_GET arrays before it starts executing your script. However, for security purposes, it is normally disabled. It is better practice to reference all variables using '$_POST['varname']' or '$_GET['varname']' so you don't depend on the feature being turned on. – Mike Gallagher May 29 '13 at 08:02
0

You need 2 pages, one which will send AJAX (this you have) and one wich will respond (below):

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

In javascript you create AJAX post

data['updabt'] = '';

$.ajax({
  type: "POST",
  url: 'ajax.php',
  data: data,
  success: function(html) { $('result').append(html); },
  dataType: 'html'
});
Igor S.
  • 3,332
  • 1
  • 25
  • 33
  • I don't agree that the asker needs two "pages". With some clever use of params it can be done with one script. You're also sending an empty string (to what end I don't know), and there is no purpose to the `success` action. The `ajax.php` does not `echo` anything. The JavaScript in `ajax.php` will not work because the script is not handled in the browser but in a separate thread that is handled on the back end. – Ian Atkin May 29 '13 at 05:37
  • @GordonFreeman First it needs to work, then optimization. OP is not skilled enough to understand all the "tricks" for one page. – Igor S. May 29 '13 at 05:40
  • I added this, $('#btnsub').click(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' }); }) but still not works... – Darpan Kulkarni May 29 '13 at 05:41
  • If you call your own page, you cannot display results (because the result is all new page) or you need to disable returning anything when isset($_POST['updabt']) – Igor S. May 29 '13 at 05:42
  • Its ok if I cannot get results but data is also not gets added in database – Darpan Kulkarni May 29 '13 at 05:54
  • in php add: print_r($_POST), open Firebug or F12 for developer console, and check what kind of response your site is giving you (network tab). print_r($_POST) will tell you what variables are in post – Igor S. May 29 '13 at 05:56
  • here is response, -- [12:08:10.676] POST http://localhost/bmw/_admin/abc.php [1ms] [12:08:10.678] POST http://localhost/bmw/_admin/about-us-iframe.php [HTTP/1.1 200 OK 1021ms] – Darpan Kulkarni May 29 '13 at 06:38
  • @IgorS.The above code works perfectly on localhost but not working on the server, can you help? – Darpan Kulkarni May 30 '13 at 08:21
0

You can use AJAX (easiest with a library such as jQuery) to submit the variables to another PHP script that will INSERT the values to your database.

Start by reading the following...

jQuery.ajax

jQuery.post

This is actually very simple once you get your head around the subtleties of AJAX.

Here is a simple example to get you off and running. Suppose you wanted to log something to your PHP error log...

This would be my JavaScript function:

var log = function(val) {
    $.post("ajax.php", {"mode": 'log', "val": val}, function() {});
}

ajax.php would be a collection of functions, ideally a class...

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

The same idea can easily be adapted to write data to a database.

Ian Atkin
  • 6,302
  • 2
  • 17
  • 24