4

Okai, so I have this div:

// DESCRIPTION AREA
$body_html .= "<div id='seq-desc-".$seq_id_d."' contenteditable='true' data-text='Det som skal skje...'>";
$body_html .= $seq_desc_d;
$body_html .= "</div>&nbsp;";

and this textarea:

$body_html .= "<textarea id='seq-desc-area-".$seq_id_d."' name='deta-".$seq_id_d."' style='display: none;'></textarea></td>";

In my form I use the following code to activate my Javascript code:

"<form action='planner_seq_save.php' id='save-".$seq_id_d."' name='save-".$seq_id_d."' method='POST' onsubmit='return getContent".$seq_id_d."'>";

getContent is defined like this:

function getContent'.$seq_id_d.'(){
    document.getElementById("seq-desc-area-'.$seq_id_d.'").value = document.getElementById("seq-desc-'.$seq_id_d.'").innerHTML;
}

How come I get an empty return in my database when using POST? I use $_POST['deta-(the id)'] to fetch my post.

Also I save my form using this code on a standard button. Could this make onsubmit not work?

onclick='document.forms['save-".$seq_id_d."'].submit();'

Been trying to find out what the problem is for a while now, and I really need someone elses opinion.

UPDATE:

Using console.log() I get no return within the function. So the function isn't running.

Full code can be found here

Gjert
  • 1,069
  • 1
  • 18
  • 48
  • is `getContent ` being called? have you tried debug using `console.log()` in `getContent `? – dann Dec 16 '15 at 09:56
  • @dann, console.log() isn't outputting anything. So the function isn't running. Could it be disturbed by the onclick function? – Gjert Dec 16 '15 at 10:08
  • i guess `onsubmit` event only triggered on user action, when you submit form by code (`form.submit()`), the event is not called. – dann Dec 16 '15 at 11:20

2 Answers2

1

It looks like the submit event is not triggered when triggering the submit() method, therefore the onsubmit handler is not called. See another question for more info.

So you can try removing the onsubmit handler and trigger the getContent function from the onclick's one:

onclick='submitForm(id)'

function submitForm(id){
    getContent(id);

    document.forms(id).submit();
}
Community
  • 1
  • 1
skip405
  • 6,119
  • 2
  • 25
  • 28
  • This can't the problem tough, I have used this lots of places. Got the inspiration from: http://stackoverflow.com/questions/7355583/can-div-with-contenteditable-true-be-passed-through-form – Gjert Dec 16 '15 at 10:04
  • Update: I tried, still not working. The function isn't running. – Gjert Dec 16 '15 at 10:10
  • @GjertIngarGjersund, please take a look – skip405 Dec 16 '15 at 10:47
0
  • rename function getContent-'.$seq_id_d.'() to getContent_'.$seq_id_d.'()
  • correct textarea id in function document.getElementById("seq-desc-area-'.$seq_id_d.'").value to document.getElementById("seq-deta-area-'.$seq_id_d.'").value
  • correct function call in onsubmit onsubmit='return getContent-".$seq_id_d."' to onsubmit='return getContent_".$seq_id_d."()'

suppose that is all :)

full code

<?php
$seq_id_d = 1;
var_dump($_REQUEST);
?>
<div id='seq-desc-<?php echo $seq_id_d; ?>' contenteditable='true' data-text='Det som skal skje...'>werwerwqrewrqwer</div>
<form id='save-<?php echo $seq_id_d; ?>' name='save-<?php echo $seq_id_d; ?>' method='POST' onsubmit='return getContent_<?php echo $seq_id_d; ?>()'>
<textarea id='seq-deta-area-<?php echo $seq_id_d; ?>' name='deta-<?php echo $seq_id_d; ?>' style='display: none;'></textarea>
<input type="submit" value="ok">
</form>
<script>
function getContent_<?php echo $seq_id_d; ?>(){
document.getElementById("seq-deta-area-<?php echo $seq_id_d; ?>").value = document.getElementById("seq-desc-<?php echo $seq_id_d; ?>").innerHTML;
}
</script>
Max P.
  • 5,579
  • 2
  • 13
  • 32
  • 1, already changed it. 2, this is a slip from my side, I have 3 fields that I wish to submit. However, just using 1 since they are all the same. So no problem there. 3. done, still not working – Gjert Dec 16 '15 at 10:20
  • It's not the problem. Se full code parse update on question. – Gjert Dec 16 '15 at 10:29
  • form onsubmit is with "()" `onsubmit='return getContent".$seq_id_d."()'`? – Max P. Dec 16 '15 at 10:32
  • Seems that forms onsubmit is not calling if form is submited by javascript. Add getContent to onclick `onclick=\"getContent".$seq_id_d."(); document.forms['save-".$seq_id_d."'].submit();\"` – Max P. Dec 16 '15 at 10:54