3

I'm trying to store the editable content of a Div in an SQL database. However from what I can tell isset($_POST["des"]) never returns true.

(I haven't done much website coding so I am probably missing something quite fundamental/trivial)

overview_page.php:

<?php session_start();
include_once("php_includes/check_login_status.php");
?>

<?php
    if(isset($_POST["des"]))
    {
    echo "Inside IF statement";
    include_once("php_includes/db_conx.php");
    $des = mysqli_real_escape_string($db_conx, $_POST['des']);    
    $sql = "UPDATE users SET project_description='$des' WHERE   username='$log_username'";
    }
?>

<!DOCTYPE html>
<html>
<head>

    <script src="js/main.js"></script> <!--Points to external java script file-->
    <script src="js/ajax.js"></script> <!--Points to external java script file-->

    <script type="text/javascript">

    function save_description()
    {
        var des = _("project_description").value;
        var ajax = ajaxObj("POST", "overview_page.php"); 
        //ajax.onreadystatechange = function() sorry this one shouldn't be here
        ajax.send("des="+des); 
    }

    </script>
</head>    
<body>    

  <?php include_once("template_pagetop.php"); ?>  

  <div id="pageMiddle">
    <div id="left_window">

            <div id="project_description" name="project_description" contentEditable="true">
            Please enter project description...
            </div>
            <center>
            <button id="save" onclick="save_description()">Save</button>
            </center>
    </div>
  </div>      
</body>    
</html>

and external scripts:

ajax.js:

function ajaxObj( meth, url ) {
var x = new XMLHttpRequest();
x.open( meth, url, true );
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200){
   return true; 
}}

main.js:

function _(x)
{
return document.getElementById(x);
}
lolcatIV
  • 35
  • 6
  • Are you sure that you want to be using `_("project_description").value`? Shouldn't you be using `_("project_description").html()`? (maybe that is just jQuery...) – Tim Groeneveld Sep 03 '13 at 10:50
  • @timgws It didn't fix the problem, but I think you might be right. From looking at the [jQuery API](http://api.jquery.com/?s=.html) it looks like `.html()` is better. I adapted some of this code from one of my other pages in which `.value` was working, but that one was collecting data from a form. – lolcatIV Sep 03 '13 at 13:00

2 Answers2

1

So I took your code over to jsFiddle and created a new fiddle and the first thing that came up in the console (F12 in your browser - at least in Chrome) was that the event wasn't actually firing.

So I moved your save_description() method into an event bind (there are other approaches to this):

document.getElementById('save').onclick = function () {
    var des = _("project_description").innerHTML;
    var ajax = ajaxObj("POST", "overview_page.php");
    //ajax.onreadystatechange = function() sorry this one shouldn't be here
    ajax.send("des=" + des);
}

Also note I changed the des = _("project_description") access to innerHTML.

This then seems to at least make the request with a value. After that, check your server-side script.

Ross
  • 3,022
  • 1
  • 17
  • 13
  • Would both `.innerHTML` and `.html()` work in this case? I think all my scripts are in the post? But to be honest ajax is probably the part I understand the least, so let me know if it looks like something is missing. – lolcatIV Sep 03 '13 at 13:12
  • Also, I am developing in Notepad++ which does not seem to do much error checking. – lolcatIV Sep 03 '13 at 13:37
  • 1
    Sorry about that lolcatIV, I spaced completely. In terms of what's happening with AJAX/XMLHttpRequests, most browsers provide developer tools you can use to watch when such requests are being made and the parameters being sent. – Ross Sep 03 '13 at 15:57
  • Thanks, I checked the console and it wasn't posting. I added your code and it gave me the console error: `Uncaught TypeError: Cannot set property 'onclick' of null ` . I then put your code inside `window.onload = function() { };` which allowed it to post. However there must be another issue because the database still is not updating. – lolcatIV Sep 04 '13 at 02:11
  • 1
    If the POST value is being received correctly (check with a `var_dump($_POST);`) and that's the entirety of your PHP then you'll need to use the [query method](http://uk3.php.net/manual/en/mysqli.query.php) to actually execute the `$sql` statement and make the change. – Ross Sep 04 '13 at 10:53
  • You were completely spot on again. I added `$query = mysqli_query($db_conx, $sql);` and it now appears to be updating the database correctly. Thanks. – lolcatIV Sep 05 '13 at 09:17
0

I will assume that your post is sending the "des" value.

BUT your code is in the "overview_page.php" file? If not, you are sending the "des" value to an not existing file or you have no code in that file. In this case your if(isset($_POST["des"])) never return true.

You need to have the overview_page.php file.

In case that you are not sure that your ajax code works as required use some Jquery post function into your "save_description()" function.

Drk_alien
  • 287
  • 6
  • 18
  • Thanks, yes you are right to assume that I am trying to send the "des" value. It was probably a poor choice for a variable name, I just named it after the "project_description" div which contains the text I am trying to save into the database. You are also right to assume that the main file is "overview_page.php", I added a label in my original post to make things clearer. – lolcatIV Sep 03 '13 at 11:59
  • Have you tried another ajax script...or jQuery script in order to post your data (just in case)? ...like those from here: http://stackoverflow.com/questions/5004233/capture-all-of-the-forms-data-and-submit-it-to-a-php-script-jquery-ajax-post – Drk_alien Sep 03 '13 at 13:59