html - How to call a javascript function on submit to assign a value to an input? (ACE Editor) - Stack Overflow

My goal is to send the code inside the ACE Editor to as a variable to the send.php PHP file. I tried th

My goal is to send the code inside the ACE Editor to as a variable to the send.php PHP file. I tried this in many different methods but I can't assign the code inside the editor to the form input.


JavaScript

This javascript function should assign a value to an element with the id="code", which is: <input type="hidden" id="code" value="" />.

editor.getSession().getValue(); returns the code that is inside the editor.

<head>
 <script>

   function getVal() {
   document.getElementById('code').value = editor.getSession().getValue();
   }

 </script>
</head>

HTML

Now, <form onsubmit="getVal();" should execute function getVal() when a user submits the form, so that the code input will have a value when sending the inputs to the send.php file.

<body>

  <div>
     <form onsubmit="getVal();" method="post" action="send.php">

        <label for="Name">From:</label>
        <input type="text" name="Name" id="Name" />
        <label for="Address">To:</label>
        <input type="text" name="Address" id="Address" />
        <label for="Subject">Subject:</label>
        <input type="text" name="Subject" id="Subject" />

        <input type="hidden" id="code" value="" />

        <div id="editor"> //ACE Editor
        </div>

        <input type="submit">

     </form>
  </div>

My goal is to send the code inside the ACE Editor to as a variable to the send.php PHP file. I tried this in many different methods but I can't assign the code inside the editor to the form input.


JavaScript

This javascript function should assign a value to an element with the id="code", which is: <input type="hidden" id="code" value="" />.

editor.getSession().getValue(); returns the code that is inside the editor.

<head>
 <script>

   function getVal() {
   document.getElementById('code').value = editor.getSession().getValue();
   }

 </script>
</head>

HTML

Now, <form onsubmit="getVal();" should execute function getVal() when a user submits the form, so that the code input will have a value when sending the inputs to the send.php file.

<body>

  <div>
     <form onsubmit="getVal();" method="post" action="send.php">

        <label for="Name">From:</label>
        <input type="text" name="Name" id="Name" />
        <label for="Address">To:</label>
        <input type="text" name="Address" id="Address" />
        <label for="Subject">Subject:</label>
        <input type="text" name="Subject" id="Subject" />

        <input type="hidden" id="code" value="" />

        <div id="editor"> //ACE Editor
        </div>

        <input type="submit">

     </form>
  </div>
Share Improve this question asked Oct 1, 2012 at 13:14 tempytempy 9073 gold badges14 silver badges23 bronze badges 4
  • Try to give the function in submit button, like onclick="return getVal();". Also check the hidden field should have the correct value then only retun true else return false. – Jobin Commented Oct 1, 2012 at 13:26
  • I did that, it still doesn't work. I added value to the input <input type="hidden" id="code" value="a" /> and checked if it has this value in the js function - it returns false. Maybe the problem is with the document.getElementById('code').value? – tempy Commented Oct 1, 2012 at 13:39
  • can you just rename the textbox id and also put some name for it bcoz you need to get the value in php right? this have no issue document.getElementById('code').value – Jobin Commented Oct 1, 2012 at 13:43
  • @JobinJose what textbox? – tempy Commented Oct 1, 2012 at 14:02
Add a ment  | 

2 Answers 2

Reset to default 2

This function is the one I needed in order to make this work:

function getVal()
{
  var editor = ace.edit("editor"); 
  // this line is necessary
  // "editor" is the id of the ACE editor div

  var code = editor.getSession().getValue();

  document.getElementById('code').value = code;
}

The code is correct, editor.getSession().getValue() is null so it writes nothing!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744846333a4596861.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信