1

I currently have a jQuery drag and drop, and want to insert the final results in a database. I want to use Javascript to get the values, but I keep getting the result 'NULL', instead of the actual content (generated by PHP/MySQL)

This is my JavaScript

function getValue() {
var ajaxRequest;  // The variable that makes Ajax possible!
try{

  // Opera 8.0+, Firefox, Safari
  ajaxRequest = new XMLHttpRequest();
    }catch (e){

  // Internet Explorer Browsers
  try{
     ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  }catch (e) {

     try{
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
     }catch (e){

        // Something went wrong
        alert("Your browser broke!");
        return false;
     }
  }
  }

var left1 = document.getElementById('left1').getAttribute('value');
var left2 = document.getElementById("left2").getAttribute('value'); 
var left3 = document.getElementById("left3").getAttribute('value');

 var queryString = "?left1=" + left1 ;
 queryString +=  "&left2=" + left2 + "&left3=" + left3;
  ajaxRequest.open("GET", "http://localhost:8888/ff/public_html/drag_and_drop.php" + queryString, true);
 ajaxRequest.send(null); 
}

PHP Code:

$dinner1 = $_GET['dinner1'];
$dinner2 = $_GET['dinner2'];
$dinner3 = $_GET['dinner3'];

$query2 = "INSERT INTO weekly_print (dinner1, dinner2, dinner3) VALUES ('".$dinner1."','".$dinner2."','".$dinner3."')";
            $result2 = mysql_query ($query2) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
            if (@mysql_num_rows($result2) != 0) {
                echo 'Success!!';

        }
}

I have tried using it without the getAttrubute('value');, however I kept getting [object HTMLDivElement] as a result.

These are similar questions, but they have not worked for me at all.

how get content of div via javascript
Get content of a DIV using JavaScript
JavaScript innerHTML not working

When using innerHTML I get the result '[object HTMLDivElement]'.

I know I need to change over to mysqli instead of mysql, its on a testing server at the moment, so please ignore that.

My HTML:

<div class="column" id="left1" style="border: 2px solid #a13f80;"></div>
<div class="column" id="left2" style="border: 2px solid #a13f80;"></div>
<div class="column" id="left3" style="border: 2px solid #a13f80;"></div>
Community
  • 1
  • 1
Fushy
  • 83
  • 2
  • 13
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Sep 07 '15 at 08:49
  • Div elements don't have values. Your div elements don't have value attributes (which would be invalid anyway) and they don't have any content. What are you trying to get? – Quentin Sep 07 '15 at 08:50

3 Answers3

2

you just need to get values instead of attibutes: document.getElementById('left1').value;

Replace your code with :

var left1 = document.getElementById('left1').value;
var left2 = document.getElementById("left2").value; 
var left3 = document.getElementById("left3").value;

And Convert your div structure to input fields as getting values from div and passing it to php is very non-standard method:

<input class="column" id="left1" style="border: 2px solid #a13f80;" />
<input class="column" id="left2" style="border: 2px solid #a13f80;" />
<input class="column" id="left3" style="border: 2px solid #a13f80;" />
Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58
1

Your divs are empty, thats why innerHTML is not working

PS: Can't comment to little points

Dennis
  • 397
  • 1
  • 5
  • 16
  • *innerText* is non–standard and not supported by all browsers in use. This should have been a comment, not an answer. – RobG Sep 07 '15 at 08:49
  • as previously mentioned var left1 = document.getElementById('left1').value; is not working, i get the result [object HTMLDivElement]. – Fushy Sep 07 '15 at 08:50
  • @Denni007 They are not empty, they are generated by PHP as they come out of a database. – Fushy Sep 07 '15 at 08:55
  • Oh, I can comment my own answer, that's interesting. Sorry but I need 50 reputation vor comments. – Dennis Sep 07 '15 at 08:56
  • @Fushy How does the HTML looks like after fill? Can you show the problem with something like codepen? – Dennis Sep 07 '15 at 08:57
  • @Denni007 Here is the link to codepen http://codepen.io/anon/pen/QjbpXX The aim is to drag and drop the content into the boxes, and then insert to mysql (Can't do that on codepen, but you get what i am trying to do) – Fushy Sep 08 '15 at 01:13
  • @Denni007 I solved my issue, my AJAX was not passing the javascript variable to php correctly. I solved it by changing my ajax to: $.ajax({ url: 'http://localhost:8888/ff/public_html/drag_and_drop.php', data: {x: left1}, type: 'GET' }); – Fushy Sep 08 '15 at 03:33
  • Nice to see, that you solved your issue. But you must learn to reduce your problems to a minimum, show the fork http://codepen.io/anon/pen/XmbowX and you see innerHTML is nice working on a div ;) – Dennis Sep 09 '15 at 08:57
-1

USe innerText or textContent attribute to get div text.

left1div = document.getElementById('left1');
var left1divval = left1div.textContent || left1div.innerText;   
alert(left1divval);

See Working Example

Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15