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 thedocument.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
2 Answers
Reset to default 2This 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条)